Skip to content

Commit

Permalink
Merge pull request #168 from cap-js/fix-asserts
Browse files Browse the repository at this point in the history
Fixing assert tests
  • Loading branch information
danjoa authored May 16, 2024
2 parents 4dcc286 + 4c34f60 commit f541333
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 120 deletions.
73 changes: 11 additions & 62 deletions test/tests/error-handling-dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,8 @@ describe('graphql - error handling in development', () => {
extensions: {
code: '400',
details: [
{
code: '400',
message: 'Wert ist erforderlich',
target: 'inRange',
stacktrace: expect.any(Array)
},
{
code: '400',
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}',
target: 'oneOfEnumValues',
args: ['"foo"', '"high", "medium", "low"'],
stacktrace: expect.any(Array)
}
{ target: 'inRange', message: 'Wert ist erforderlich' },
{ target: 'oneOfEnumValues', message: expect.stringContaining('Value "foo" is invalid') }
]
}
}
Expand All @@ -155,53 +144,13 @@ describe('graphql - error handling in development', () => {
expect(response.data.errors[0].extensions.details[1].stacktrace[0]).not.toHaveLength(0) // Stacktrace exists and is not empty

const log = console.warn.mock.calls[0][1]
if (cds.env.features.cds_assert) {
expect(log).toMatchObject({
code: '400',
message: 'Multiple errors occurred. Please see the details for more information.',
details: [
{ code: '400', target: 'inRange', message: 'Value is required' },
{
code: '400',
target: 'oneOfEnumValues',
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}'
}
]
})
} else {
expect(log).toMatchObject({
code: '400',
message: 'Multiple errors occurred. Please see the details for more information.',
details: [
{
args: ['inRange'],
code: '400',
element: 'inRange',
entity: 'ValidationErrorsService.C',
message: 'Value is required',
numericSeverity: 4,
target: 'inRange',
type: 'cds.Integer',
value: undefined,
stack: expect.any(String)
},
{
args: ['"foo"', '"high", "medium", "low"'],
code: '400',
element: 'oneOfEnumValues',
entity: 'ValidationErrorsService.C',
enum: ['@assert.range', 'type', 'enum'],
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}',
numericSeverity: 4,
target: 'oneOfEnumValues',
type: 'cds.String',
value: 'foo',
stack: expect.any(String)
}
]
})
}

expect(log).toMatchObject({
code: '400',
details: [
{ target: 'inRange', message: 'Value is required' },
{ target: 'oneOfEnumValues', message: expect.stringContaining('Value "foo" is invalid') }
]
})
expect(console.warn.mock.calls[0][1]).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
})
})
Expand Down Expand Up @@ -399,15 +348,15 @@ describe('graphql - error handling in development', () => {
numericSeverity: 4,
status: 418,
target: 'some_field',
stack: expect.any(String)
// stack: expect.any(String) // doesn't work with Node 22
},
{
code: 'Some-Custom-Code2',
message: 'Some Custom Error Message 2',
numericSeverity: 4,
status: 500,
target: 'some_field',
stack: expect.any(String)
// stack: expect.any(String) // doesn't work with Node 22
}
]
})
Expand Down
72 changes: 14 additions & 58 deletions test/tests/error-handling-prod.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,7 @@ describe('graphql - error handling in production', () => {
expect(response.data).toMatchObject({ errors })
expect(response.data.errors[0].extensions).not.toHaveProperty('stacktrace') // No stacktrace in production
const log = console.warn.mock.calls[0][1] || JSON.parse(console.warn.mock.calls[0][0])

if (cds.env.features.cds_assert) {
expect(log).toMatchObject({ code: '400', target: 'notEmptyI', msg: 'Value is required' })
} else {
expect(log).toMatchObject({
code: '400',
element: 'notEmptyI',
entity: 'ValidationErrorsService.A',
msg: 'Value is required',
numericSeverity: 4,
target: 'notEmptyI',
type: 'cds.Integer'
})
}
expect(log).toMatchObject({ code: '400', target: 'notEmptyI', msg: 'Value is required' })
})

test('Multiple @mandatory validation errors', async () => {
Expand Down Expand Up @@ -149,7 +136,7 @@ describe('graphql - error handling in production', () => {
},
{
code: '400',
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}',
message: expect.stringContaining('Value "foo" is invalid'),
target: 'oneOfEnumValues'
}
]
Expand All @@ -163,49 +150,18 @@ describe('graphql - error handling in production', () => {
expect(response.data.errors[0].extensions.details[1]).not.toHaveProperty('stacktrace') // No stacktrace in production
const log = console.warn.mock.calls[0][1] || JSON.parse(console.warn.mock.calls[0][0])

if (cds.env.features.cds_assert) {
expect(log).toMatchObject({
code: '400',
msg: 'Multiple errors occurred. Please see the details for more information.',
details: [
{ code: '400', target: 'inRange', message: 'Value is required' },
{
code: '400',
target: 'oneOfEnumValues',
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}'
}
]
})
} else {
expect(log).toMatchObject({
code: '400',
msg: 'Multiple errors occurred. Please see the details for more information.',
details: [
{
args: ['inRange'],
code: '400',
element: 'inRange',
entity: 'ValidationErrorsService.C',
message: 'Value is required',
numericSeverity: 4,
target: 'inRange',
type: 'cds.Integer'
},
{
args: ['"foo"', '"high", "medium", "low"'],
code: '400',
element: 'oneOfEnumValues',
entity: 'ValidationErrorsService.C',
enum: ['@assert.range', 'type', 'enum'],
message: 'Value "foo" is invalid according to enum declaration {"high", "medium", "low"}',
numericSeverity: 4,
target: 'oneOfEnumValues',
type: 'cds.String',
value: 'foo'
}
]
})
}
expect(log).toMatchObject({
code: '400',
msg: 'Multiple errors occurred. Please see the details for more information.',
details: [
{ code: '400', target: 'inRange', message: 'Value is required' },
{
code: '400',
target: 'oneOfEnumValues',
message: expect.stringContaining('Value "foo" is invalid')
}
]
})

expect(log).not.toHaveProperty('stacktrace') // No stacktrace outside of error details
})
Expand Down

0 comments on commit f541333

Please sign in to comment.