Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matcher custom Jest pour les tests d'intéragtion #43

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test/common.ts
Original file line number Diff line number Diff line change
@@ -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<R> {
toMatchSchema: (type: string) => R
}
}
28 changes: 5 additions & 23 deletions test/static.test.ts
Original file line number Diff line number Diff line change
@@ -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')
}
})
})
34 changes: 5 additions & 29 deletions test/user.test.ts
Original file line number Diff line number Diff line change
@@ -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[] = []

Expand All @@ -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')
})
})
Loading