Skip to content

Commit

Permalink
split tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jonathan Alvarez <[email protected]>
  • Loading branch information
jonalvarezz committed Jun 5, 2024
1 parent fb4f9dd commit 523eda7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 59 deletions.
1 change: 1 addition & 0 deletions packages/schemas/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
export default {
displayName: 'schemas',
preset: '../../jest.preset.js',
verbose: true,
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
Expand Down
73 changes: 73 additions & 0 deletions packages/schemas/src/__tests__/base.test.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>['errors'];
constructor(path: string, errors: ValidateFunction<unknown>['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);
}
});
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -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)(
Expand Down

0 comments on commit 523eda7

Please sign in to comment.