Skip to content

Commit

Permalink
base tests: list out files
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 073ea68 commit eb9cd13
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions packages/schemas/src/lib/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ type SchemaSpec = {
type VersionData = {
version: string;
compatibleVersionGlob: string;
baseSchemaSpec: SchemaSpec;
credentialSchemaSpecs: SchemaSpec[];
};

function load(spec: VersionSpec): VersionData {
const schemaFiles = glob(`packages/schemas/src/lib/**/${spec.version}.ts`);

let baseSchema = null as SchemaSpec | null;
const credentialSchemaSpecs: SchemaSpec[] = [];

schemaFiles
Expand All @@ -50,25 +48,19 @@ function load(spec: VersionSpec): VersionData {
};

if (folderName === '0-base') {
baseSchema = schema;
// skip base. it's handled separately
} else {
credentialSchemaSpecs.push(schema);
}
});

if (baseSchema === null) {
// swallow
// a base can be used in different schema versions.
}

return {
...spec,
baseSchemaSpec: baseSchema as SchemaSpec,
credentialSchemaSpecs,
};
}

const VERSION_DATA: VersionData[] = [
const SCHEMA_VERSION_DATA: VersionData[] = [
{ version: '1-0-0', compatibleVersionGlob: '1-0-0' },
{ version: '1-1-0', compatibleVersionGlob: '1-[01]-0' },
{ version: '1-1-1', compatibleVersionGlob: '1-[01]-[01]' },
Expand Down Expand Up @@ -115,33 +107,40 @@ describe('Base schemas', () => {
);
};

test.each(baseVersionData)(
'base version $version should accept $compatibleVersionGlob credentials',
async ({ version, compatibleVersionGlob }) => {
// Import schema definition
const { schema } = await import(`../../lib/0-base/${version}`);
expect(schema.$id).toBeDefined();
expect(schema.$schema).toBeDefined();
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`);

// Compile schema
const validate = ajv.compile(schema);
expect(validate.errors).toBeNull();
beforeAll(async () => {
const { schema: _schema } = await import(`../../lib/0-base/${version}`);
schema = _schema;

const examplePaths = glob(`examples/${compatibleVersionGlob}/**/*.json`);
// Compile schema
validate = ajv.compile(schema);
});

for (const examplePath of examplePaths) {
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);
}
}
});
}
);
});

describe.each(VERSION_DATA)(
describe.each(SCHEMA_VERSION_DATA)(
'Schema $version should accept $compatibleVersionGlob certificates',
({ version, compatibleVersionGlob, credentialSchemaSpecs }) => {
test.each(credentialSchemaSpecs)(
Expand Down

0 comments on commit eb9cd13

Please sign in to comment.