From ad28745d0daf80f08e4327098c711d35b824376c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Sun, 5 May 2024 12:40:38 +0200 Subject: [PATCH] test: generate the JSON Schema beforehand to optimize test execution --- test/common.ts | 11 +++++------ test/static.test.ts | 8 +++----- test/user.test.ts | 36 +++++++++++++++++------------------- 3 files changed, 25 insertions(+), 30 deletions(-) diff --git a/test/common.ts b/test/common.ts index 08064dd..581ca72 100644 --- a/test/common.ts +++ b/test/common.ts @@ -1,5 +1,4 @@ import type { MatcherFunction } from 'expect' -import type { Schema } from 'ajv' import Ajv from 'ajv' import type { Config } from 'ts-json-schema-generator' import { createGenerator } from 'ts-json-schema-generator' @@ -12,10 +11,10 @@ const ajvConfig: Config = { tsconfig: 'tsconfig.json' } -export const createSchema = (type: string): Schema => createGenerator({ ...ajvConfig, type }).createSchema(type) +const schemaGenerator = createGenerator({ ...ajvConfig, type: '*' }) -const toMatchSchema: MatcherFunction<[type: string, schema: Schema]> = (actual: any, type: string, schema: Schema) => { - const result = ajv.validate(schema, actual) +const toMatchSchema: MatcherFunction<[type: string]> = (actual: any, type: string) => { + const result = ajv.validate(schemaGenerator.createSchema(type), actual) return { message: () => `expected ${type + (result ? ' not ' : ' ')}to match schema: ${inspect(ajv.errors)}`, @@ -27,9 +26,9 @@ expect.extend({ toMatchSchema }) declare module 'expect' { interface AsymmetricMatchers { - toMatchSchema: (type: string, schema: Schema) => void + toMatchSchema: (type: string) => void } interface Matchers { - toMatchSchema: (type: string, schema: Schema) => R + toMatchSchema: (type: string) => R } } diff --git a/test/static.test.ts b/test/static.test.ts index da77f77..910a131 100644 --- a/test/static.test.ts +++ b/test/static.test.ts @@ -1,8 +1,6 @@ import { describe, expect } from '@jest/globals' import { Skolengo } from '../src/index' -import { createSchema } from './common' - -const schema = createSchema('*') +import './common' /** * Tests d'intégration des endpoints ne nécessitant pas d'authentification @@ -11,14 +9,14 @@ describe('Test Skolengo API types - Public endpoints', () => { it('should getAppCurrentConfig return AppConfig type', async () => { const response = await Skolengo.getAppCurrentConfig() - expect(response).toMatchSchema('AppCurrentConfig', schema) + expect(response).toMatchSchema('AppCurrentConfig') }) it('should searchSchool return School type', async () => { const response = await Skolengo.searchSchool({ text: 'Lycée' }, 50) for (const school of response) { - expect(school).toMatchSchema('School', schema) + expect(school).toMatchSchema('School') } }) }) diff --git a/test/user.test.ts b/test/user.test.ts index f2d1920..3fffbcb 100644 --- a/test/user.test.ts +++ b/test/user.test.ts @@ -5,13 +5,11 @@ import type { Agenda, HomeworkAssignment } from '../src/models/Calendar' import type { AbsenceFile } from '../src/models/SchoolLife' import type { SchoolInfo } from '../src/models/School' import { Skolengo } from '../src/index' -import { createSchema } from './common' +import './common' const SKOLENGO_TOKENSET = process.env.SKOLENGO_TOKENSET const describeAuthenticated = SKOLENGO_TOKENSET !== undefined ? describe : describe.skip -const schema = createSchema('*') - /** * Tests d'intégration des endpoints qui nécessitent une authentification */ @@ -24,12 +22,12 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', () it('should match type User', async () => { const response = await user.getUserInfo() - expect(response).toMatchSchema('User', schema) + expect(response).toMatchSchema('User') }) it('should match type AbsenceReason[]', async () => { const reasons = await user.getAbsenceReasons() - for (const reason of reasons) expect(reason).toMatchSchema('AbsenceReason', schema) + for (const reason of reasons) expect(reason).toMatchSchema('AbsenceReason') }) /** @@ -43,13 +41,13 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', () }) it('should match type SchoolInfo[]', () => { - for (const info of schoolInfoList) expect(info).toMatchSchema('SchoolInfo', schema) + for (const info of schoolInfoList) expect(info).toMatchSchema('SchoolInfo') }) it('should match the SchoolInfo', async () => { for (const info of schoolInfoList.slice(0, 2)) { const infoDetail = await user.getSchoolInfo(info.id) - expect(infoDetail).toMatchSchema('SchoolInfo', schema) + expect(infoDetail).toMatchSchema('SchoolInfo') } }) }) @@ -68,23 +66,23 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', () }) it('should match type EvaluationSettings', () => { - for (const evaluationSettings of evaluationSettingsList) expect(evaluationSettings).toMatchSchema('EvaluationSettings', schema) + for (const evaluationSettings of evaluationSettingsList) expect(evaluationSettings).toMatchSchema('EvaluationSettings') }) it('should match type Evaluation[]', () => { - for (const evaluation of evaluationList) expect(evaluation).toMatchSchema('Evaluation', schema) + for (const evaluation of evaluationList) expect(evaluation).toMatchSchema('Evaluation') }) it('should match type EvaluationDetail[]', async () => { for (const evaluation of evaluationList) { const evaluationDetail = await user.getEvaluationDetail(undefined, evaluation.evaluations[0].id) - expect(evaluationDetail).toMatchSchema('EvaluationDetail', schema) + expect(evaluationDetail).toMatchSchema('EvaluationDetail') } }) it('should match type Attachment[]', async () => { const response = await user.getPeriodicReportsFiles(undefined, 2) - for (const attachment of response) expect(attachment).toMatchSchema('Attachment', schema) + for (const attachment of response) expect(attachment).toMatchSchema('Attachment') }) }) @@ -94,7 +92,7 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', () describe('Test of the MSG module', () => { it('should match type UsersMailSettings', async () => { const response = await user.getUsersMailSettings() - expect(response).toMatchSchema('UsersMailSettings', schema) + expect(response).toMatchSchema('UsersMailSettings') }) }) @@ -113,30 +111,30 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', () }) it('should match type Agenda[]', () => { - for (const agenda of agendaList) expect(agenda).toMatchSchema('Agenda', schema) + for (const agenda of agendaList) expect(agenda).toMatchSchema('Agenda') }) it('should match type Lesson[]', () => { for (const lesson of agendaList[0].lessons) { - expect(lesson).toMatchSchema('Lesson', schema) + expect(lesson).toMatchSchema('Lesson') } }) it('should match type Lesson', async () => { for (const lesson of agendaList[0].lessons) { const lessonDetail = await user.getLesson(undefined, lesson.id) - expect(lessonDetail).toMatchSchema('Lesson', schema) + expect(lessonDetail).toMatchSchema('Lesson') } }) it('should match type HomeWorkAssignment[]', () => { - for (const homework of homeworkList) expect(homework).toMatchSchema('HomeworkAssignment', schema) + for (const homework of homeworkList) expect(homework).toMatchSchema('HomeworkAssignment') }) it('should match type HomeWorkAssignment', async () => { for (const homework of homeworkList) { const homeworkDetail = await user.getHomeworkAssignment(undefined, homework.id) - expect(homeworkDetail).toMatchSchema('HomeworkAssignment', schema) + expect(homeworkDetail).toMatchSchema('HomeworkAssignment') } }) }) @@ -152,13 +150,13 @@ describeAuthenticated('Test of the Skolengo API types - Authenticated user', () }) it('should match type AbsenceFile[]', () => { - for (const file of absenceFiles) expect(file).toMatchSchema('AbsenceFile', schema) + for (const file of absenceFiles) expect(file).toMatchSchema('AbsenceFile') }) it('should match type AbsenceFile', async () => { for (const file of absenceFiles) { const fileDetail = await user.getAbsenceFile(file.id) - expect(fileDetail).toMatchSchema('AbsenceFile', schema) + expect(fileDetail).toMatchSchema('AbsenceFile') } }) })