From 7823adabc9c97861e47271e87bb02103a9a86c88 Mon Sep 17 00:00:00 2001 From: Matt Scheurich Date: Thu, 26 Sep 2019 01:31:18 +0200 Subject: [PATCH] Updated types for jest-json-schema@2.1 (#38465) * updated jest-json-schema types with latest version of Ajv and support for extendAjv in matchersWithOptions * updated jest-json-schema version in header * removed patch version from header --- types/jest-json-schema/index.d.ts | 8 ++-- .../jest-json-schema-tests.ts | 37 +++++++++++++++++-- types/jest-json-schema/package.json | 2 +- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/types/jest-json-schema/index.d.ts b/types/jest-json-schema/index.d.ts index b2133526dc..cc9636a36d 100644 --- a/types/jest-json-schema/index.d.ts +++ b/types/jest-json-schema/index.d.ts @@ -1,19 +1,21 @@ -// Type definitions for jest-json-schema 1.2 +// Type definitions for jest-json-schema 2.1 // Project: https://github.com/americanexpress/jest-json-schema#readme // Definitions by: Igor Korolev +// Matt Scheurich // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 3.0 /// -import * as ajv from "ajv"; +import * as ajv from 'ajv'; declare global { namespace jest { interface Matchers { + toBeValidSchema(): R; toMatchSchema(schema: object): R; } } } export const matchers: jest.ExpectExtendMap; -export function matchersWithOptions(options: ajv.Options): jest.ExpectExtendMap; +export function matchersWithOptions(options: ajv.Options, extendAjv?: (ajv: ajv.Ajv) => void): jest.ExpectExtendMap; diff --git a/types/jest-json-schema/jest-json-schema-tests.ts b/types/jest-json-schema/jest-json-schema-tests.ts index a66c54a7e9..114c7d758e 100644 --- a/types/jest-json-schema/jest-json-schema-tests.ts +++ b/types/jest-json-schema/jest-json-schema-tests.ts @@ -1,13 +1,42 @@ -import { matchers } from 'jest-json-schema'; +import { matchersWithOptions } from 'jest-json-schema'; -expect.extend(matchers); +expect.extend( + matchersWithOptions( + { + formats: { + test: /^test$/, + }, + }, + ajv => { + ajv.addKeyword('test', { + validate: (schema: any, data: string) => { + return schema && data === 'test'; + }, + metaSchema: { + type: 'boolean', + }, + }); + }, + ), +); -it('validates my json', () => { +it('validates schema', () => { + expect({ + test: true, + format: 'test', + }).toBeValidSchema(); +}); + +it('schema matches data', () => { const schema = { properties: { hello: { type: 'string' }, + test: { + test: true, + format: 'test', + }, }, required: ['hello'], }; - expect({ hello: 'world' }).toMatchSchema(schema); + expect({ hello: 'world', test: 'test' }).toMatchSchema(schema); }); diff --git a/types/jest-json-schema/package.json b/types/jest-json-schema/package.json index ee16c03768..dce885a94f 100644 --- a/types/jest-json-schema/package.json +++ b/types/jest-json-schema/package.json @@ -1,6 +1,6 @@ { "private": true, "dependencies": { - "ajv": "^4.11.5" + "ajv": "^6.10.2" } }