feat: Introduce toRequireProperty custom assertion
This commit is contained in:
31
packages/backend/test/assertions/to-require-property.js
Normal file
31
packages/backend/test/assertions/to-require-property.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import { ValidationError } from 'objection';
|
||||||
|
|
||||||
|
export const toRequireProperty = async (model, requiredProperty) => {
|
||||||
|
try {
|
||||||
|
await model.query().insert({});
|
||||||
|
} catch (error) {
|
||||||
|
if (
|
||||||
|
error instanceof ValidationError &&
|
||||||
|
error.message.includes(
|
||||||
|
`${requiredProperty}: must have required property '${requiredProperty}'`
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return {
|
||||||
|
pass: true,
|
||||||
|
message: () =>
|
||||||
|
`Expected ${requiredProperty} to be required, and it was.`,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
return {
|
||||||
|
pass: false,
|
||||||
|
message: () =>
|
||||||
|
`Expected ${requiredProperty} to be required, but it was not found in the error message.`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
pass: false,
|
||||||
|
message: () =>
|
||||||
|
`Expected ${requiredProperty} to be required, but no ValidationError was thrown.`,
|
||||||
|
};
|
||||||
|
};
|
@@ -2,6 +2,7 @@ import { Model } from 'objection';
|
|||||||
import { client as knex } from '../../src/config/database.js';
|
import { client as knex } from '../../src/config/database.js';
|
||||||
import logger from '../../src/helpers/logger.js';
|
import logger from '../../src/helpers/logger.js';
|
||||||
import { vi } from 'vitest';
|
import { vi } from 'vitest';
|
||||||
|
import './insert-assertions.js';
|
||||||
|
|
||||||
global.beforeAll(async () => {
|
global.beforeAll(async () => {
|
||||||
global.knex = null;
|
global.knex = null;
|
||||||
|
8
packages/backend/test/setup/insert-assertions.js
Normal file
8
packages/backend/test/setup/insert-assertions.js
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import { expect } from 'vitest';
|
||||||
|
import { toRequireProperty } from '../assertions/to-require-property';
|
||||||
|
|
||||||
|
expect.extend({
|
||||||
|
async toRequireProperty(model, requiredProperty) {
|
||||||
|
return await toRequireProperty(model, requiredProperty);
|
||||||
|
},
|
||||||
|
});
|
Reference in New Issue
Block a user