diff --git a/test/common.ts b/test/common.ts new file mode 100644 index 0000000..fdd762c --- /dev/null +++ b/test/common.ts @@ -0,0 +1,33 @@ +import type { MatcherFunction } from 'expect' +import Ajv from 'ajv' +import type { Config } from 'ts-json-schema-generator' +import { createGenerator } from 'ts-json-schema-generator' +import { inspect } from 'util' + +const ajv = new Ajv() + +const ajvConfig: Config = { + path: 'src/**/*.ts', + tsconfig: 'tsconfig.json' +} + +const toMatchSchema: MatcherFunction<[type: string]> = (actual: any, type: string) => { + const schema = createGenerator({ ...ajvConfig, type }).createSchema(type) + const result = ajv.validate(schema, actual) + + return { + message: () => `expected ${type + (result ? ' not ' : ' ')}to match schema: ${inspect(ajv.errors)}`, + pass: result + } +} + +expect.extend({ toMatchSchema }) + +declare module 'expect' { + interface AsymmetricMatchers { + toMatchSchema: (type: string) => void + } + interface Matchers { + toMatchSchema: (type: string) => R + } +} diff --git a/test/static.test.ts b/test/static.test.ts index 28bea47..f825053 100644 --- a/test/static.test.ts +++ b/test/static.test.ts @@ -1,40 +1,22 @@ import { describe, expect } from '@jest/globals' -import type { Config } from 'ts-json-schema-generator' -import { createGenerator } from 'ts-json-schema-generator' import { Skolengo } from '../src/index' -import Ajv from 'ajv' - -const ajv = new Ajv() - -const ajvConfig: Config = { - path: 'src/**/*.ts', - tsconfig: 'tsconfig.json' -} +import './common' /** * Tests unitaires des endpoints ne nécessitant pas d'authentification */ -describe('Test Skolengo API types - Anonymous', () => { +describe('Test Skolengo API types - Public endpoints', () => { it('should getAppCurrentConfig return AppConfig type', async () => { - const type = 'AppCurrentConfig' const response = await Skolengo.getAppCurrentConfig() - const schema = createGenerator({ ...ajvConfig, type }).createSchema(type) - - const result = ajv.validate(schema, response) - if (!result) console.error(ajv.errors) - expect(result).toBe(true) + expect(response).toMatchSchema('AppCurrentConfig') }) it('should searchSchool return School type', async () => { - const type = 'School' const response = await Skolengo.searchSchool({ text: 'Lycée' }, 10) - const schema = createGenerator({ ...ajvConfig, type }).createSchema(type) - for (const school of response) { - const result = ajv.validate(schema, school) - if (!result) console.error(ajv.errors) - expect(result).toBe(true) + for (const school of response) { + expect(school).toMatchSchema('School') } }) }) diff --git a/test/user.test.ts b/test/user.test.ts index bcb5552..d11b318 100644 --- a/test/user.test.ts +++ b/test/user.test.ts @@ -1,23 +1,14 @@ import { describe, expect } from '@jest/globals' -import type { Config } from 'ts-json-schema-generator' -import { createGenerator } from 'ts-json-schema-generator' import { Skolengo } from '../src/index' -import Ajv from 'ajv' +import './common' const SKOLENGO_TOKENSET = process.env.SKOLENGO_TOKENSET const describeAuthenticated = SKOLENGO_TOKENSET !== undefined ? describe : describe.skip -const ajv = new Ajv() - -const ajvConfig: Config = { - path: 'src/**/*.ts', - tsconfig: 'tsconfig.json' -} - /** * Tests unitaires des endpoints qui nécessitent une authentification */ -describeAuthenticated('Test Skolengo API types - User logged in', () => { +describeAuthenticated('Test Skolengo API types - Authenticated user', () => { let user: Skolengo let userPermissions: string[] = [] @@ -29,38 +20,23 @@ describeAuthenticated('Test Skolengo API types - User logged in', () => { }) it('should getUserInfo return User type', async () => { - const type = 'User' const response = await user.getUserInfo() - const schema = createGenerator({ ...ajvConfig, type }).createSchema(type) - - const result = ajv.validate(schema, response) - if (!result) console.error(ajv.errors) - expect(result).toBe(true) + expect(response).toMatchSchema('User') }) it('should getEvaluationSettings return EvaluationSettings type', async () => { if (!userPermissions.includes('READ_EVALUATIONS')) return - const type = 'EvaluationSettings' const response = await user.getEvaluationSettings() - const schema = createGenerator({ ...ajvConfig, type }).createSchema(type) for (const evaluationSettings of response) { - const result = ajv.validate(schema, evaluationSettings) - if (!result) console.error(ajv.errors) - - expect(result).toBe(true) + expect(evaluationSettings).toMatchSchema('EvaluationSettings') } }) it('should getUsersMailSettings return UsersMailSettings type', async () => { - const type = 'UsersMailSettings' const response = await user.getUsersMailSettings() - const schema = createGenerator({ ...ajvConfig, type }).createSchema(type) - - const result = ajv.validate(schema, response) - if (!result) console.error(ajv.errors) - expect(result).toBe(true) + expect(response).toMatchSchema('UsersMailSettings') }) })