-
Notifications
You must be signed in to change notification settings - Fork 757
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: use JSON object schemas for validation
- Loading branch information
Showing
12 changed files
with
1,119 additions
and
19,067 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import ajvKeywords from 'ajv-keywords'; | ||
import { matchersWithOptions } from 'jest-json-schema'; | ||
|
||
import accountSchemas from './schemas/account.json'; | ||
import libSchemas from './schemas/lib.json'; | ||
import providerSchemas from './schemas/provider.json'; | ||
import sequencerSchemas from './schemas/sequencer.json'; | ||
|
||
const schemas = [accountSchemas, sequencerSchemas, providerSchemas, libSchemas]; | ||
const jestJsonMatchers = matchersWithOptions({ schemas }, (ajv: any) => { | ||
// @ts-ignore | ||
ajv.addKeyword({ | ||
keyword: 'isBigInt', | ||
type: 'object', | ||
validate: (_schema: any, data: any) => { | ||
return typeof data === 'bigint' && data < 2n ** 64n && data >= 0n; | ||
}, | ||
errors: true, | ||
}); | ||
// This uses the `ajv-keywords` library to add pre-made custom keywords to the Ajv instance. | ||
ajvKeywords(ajv, ['typeof', 'instanceof']); | ||
}); | ||
|
||
export const initializeMatcher = (expect: jest.Expect) => { | ||
expect.extend(jestJsonMatchers); | ||
expect.extend({ | ||
toMatchSchemaRef(received: object, name: string) { | ||
const schema = schemas.find((s) => Object.keys(s.definitions).includes(name)); | ||
const $ref = `${schema?.$id}#/definitions/${name}`; | ||
return jestJsonMatchers.toMatchSchema.call(this, received, { $ref }); | ||
}, | ||
}); | ||
expect(accountSchemas).toBeValidSchema(); | ||
expect(sequencerSchemas).toBeValidSchema(); | ||
expect(providerSchemas).toBeValidSchema(); | ||
expect(libSchemas).toBeValidSchema(); | ||
}; | ||
|
||
declare global { | ||
namespace jest { | ||
interface Matchers<R> { | ||
toMatchSchemaRef(name: string): R; | ||
} | ||
} | ||
} |
Oops, something went wrong.