diff --git a/packages/schemas/jest.config.ts b/packages/schemas/jest.config.ts index 5fcd79e..f89e67e 100644 --- a/packages/schemas/jest.config.ts +++ b/packages/schemas/jest.config.ts @@ -2,6 +2,7 @@ export default { displayName: 'schemas', preset: '../../jest.preset.js', + verbose: true, globals: { 'ts-jest': { tsconfig: '/tsconfig.spec.json', diff --git a/packages/schemas/src/__tests__/base.test.ts b/packages/schemas/src/__tests__/base.test.ts new file mode 100644 index 0000000..855ee06 --- /dev/null +++ b/packages/schemas/src/__tests__/base.test.ts @@ -0,0 +1,73 @@ +import { describe, expect, test } from '@jest/globals'; + +import { readFileSync } from 'fs'; +import { globSync as glob } from 'fast-glob'; + +import { ajv } from '../lib/ajv'; +import { ValidateFunction } from 'ajv'; + +type VersionSpec = { + version: string; + compatibleVersionGlob: string; +}; + +class ValidationError extends Error { + public readonly path: string; + public readonly errors: ValidateFunction['errors']; + constructor(path: string, errors: ValidateFunction['errors']) { + super(`Failed at validating ${path}: , ${JSON.stringify(errors)}`); + this.path = path; + this.errors = errors; + } +} + +const BASE_VERSION_DATA: VersionSpec[] = [ + { version: '1-0-0', compatibleVersionGlob: '1-0-0' }, + { version: '1-1-0', compatibleVersionGlob: '*-*-*' }, +]; + +const BASE_SCHEMA_EXCEPTIONS = [ + { + comment: '20-token-holding-amount@1-1-0 allows empty assertions', + version: '1-1-0', + path: 'examples/1-1-0/20-token-holding-amount-list/20-token-holding-amount-list-empty.json', + }, +]; + +const isException = (version: string, path: string) => { + return BASE_SCHEMA_EXCEPTIONS.some( + (exception) => exception.version === version && exception.path === path + ); +}; + +describe.each(BASE_VERSION_DATA)( + 'Base Schema version $version should accept $compatibleVersionGlob credentials', + ({ version, compatibleVersionGlob }) => { + let schema: any; + let validate: ValidateFunction; + const examplePaths = glob(`examples/${compatibleVersionGlob}/**/*.json`); + + beforeAll(async () => { + const { schema: _schema } = await import(`../lib/0-base/${version}`); + schema = _schema; + + // Compile schema + validate = ajv.compile(schema); + }); + + test('is valid', () => { + expect(schema.$id).toBeDefined(); + expect(schema.$schema).toBeDefined(); + expect(validate.errors).toBeNull(); + }); + + test.each(examplePaths)('%s', async (examplePath: string) => { + const example = JSON.parse(readFileSync(examplePath, 'utf8')); + const isValid = validate(example); + + if (!isValid && !isException(version, examplePath)) { + throw new ValidationError(examplePath, validate.errors); + } + }); + } +); diff --git a/packages/schemas/src/__tests__/index.test.ts b/packages/schemas/src/__tests__/schemas.test.ts similarity index 65% rename from packages/schemas/src/__tests__/index.test.ts rename to packages/schemas/src/__tests__/schemas.test.ts index a7b7b66..a77a874 100644 --- a/packages/schemas/src/__tests__/index.test.ts +++ b/packages/schemas/src/__tests__/schemas.test.ts @@ -79,65 +79,12 @@ class ValidationError extends Error { const schemasWithNoExamples: string[] = []; afterAll(() => { - if (schemasWithNoExamples.length > 0) { - console.warn( - '[warning]: The following schemas have no examples:', - schemasWithNoExamples - ); - } -}); - -describe('Base schemas', () => { - const baseVersionData: VersionSpec[] = [ - { version: '1-0-0', compatibleVersionGlob: '1-0-0' }, - { version: '1-1-0', compatibleVersionGlob: '*-*-*' }, - ]; - - const exceptions = [ - { - comment: '20-token-holding-amount@1-1-0 allows empty assertions', - version: '1-1-0', - path: 'examples/1-1-0/20-token-holding-amount-list/20-token-holding-amount-list-empty.json', - }, - ]; - - const isException = (version: string, path: string) => { - return exceptions.some( - (exception) => exception.version === version && exception.path === path - ); - }; - - describe.each(baseVersionData)( - 'Base Schema version $version should accept $compatibleVersionGlob credentials', - ({ version, compatibleVersionGlob }) => { - let schema: any; - let validate: ValidateFunction; - const examplePaths = glob(`examples/${compatibleVersionGlob}/**/*.json`); - - beforeAll(async () => { - const { schema: _schema } = await import(`../lib/0-base/${version}`); - schema = _schema; - - // Compile schema - validate = ajv.compile(schema); - }); - - test('is valid', () => { - expect(schema.$id).toBeDefined(); - expect(schema.$schema).toBeDefined(); - expect(validate.errors).toBeNull(); - }); - - test.each(examplePaths)('%s', async (examplePath: string) => { - const example = JSON.parse(readFileSync(examplePath, 'utf8')); - const isValid = validate(example); - - if (!isValid && !isException(version, examplePath)) { - throw new ValidationError(examplePath, validate.errors); - } - }); - } - ); + // if (schemasWithNoExamples.length > 0) { + // console.warn( + // '[warning]: The following schemas have no examples:', + // schemasWithNoExamples + // ); + // } }); describe.each(SCHEMA_VERSION_DATA)(