From 9e22c3e80e797a4ce8028fc594a02df4c14ef4ce Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 14 Feb 2022 09:29:05 +0100 Subject: [PATCH 1/3] feat: create new `@jest/schemas` type --- packages/jest-schemas/.npmignore | 6 ++++ packages/jest-schemas/package.json | 28 ++++++++++++++++ packages/jest-schemas/src/index.ts | 36 ++++++++++++++++++++ packages/jest-schemas/tsconfig.json | 9 +++++ packages/jest-types/package.json | 1 + packages/jest-types/src/Config.ts | 10 +++--- packages/jest-types/tsconfig.json | 3 +- packages/pretty-format/package.json | 1 + packages/pretty-format/src/types.ts | 49 +++++----------------------- packages/pretty-format/tsconfig.json | 2 +- yarn.lock | 17 ++++++++++ 11 files changed, 114 insertions(+), 48 deletions(-) create mode 100644 packages/jest-schemas/.npmignore create mode 100644 packages/jest-schemas/package.json create mode 100644 packages/jest-schemas/src/index.ts create mode 100644 packages/jest-schemas/tsconfig.json diff --git a/packages/jest-schemas/.npmignore b/packages/jest-schemas/.npmignore new file mode 100644 index 000000000000..70febcbacbcb --- /dev/null +++ b/packages/jest-schemas/.npmignore @@ -0,0 +1,6 @@ +**/__mocks__/** +**/__tests__/** +src +tsconfig.json +tsconfig.tsbuildinfo +api-extractor.json diff --git a/packages/jest-schemas/package.json b/packages/jest-schemas/package.json new file mode 100644 index 000000000000..1d32b1a24fb9 --- /dev/null +++ b/packages/jest-schemas/package.json @@ -0,0 +1,28 @@ +{ + "name": "@jest/schemas", + "version": "28.0.0-alpha.0", + "repository": { + "type": "git", + "url": "https://github.com/facebook/jest.git", + "directory": "packages/jest-schemas" + }, + "license": "MIT", + "main": "./build/index.js", + "types": "./build/index.d.ts", + "exports": { + ".": { + "types": "./build/index.d.ts", + "default": "./build/index.js" + }, + "./package.json": "./package.json" + }, + "dependencies": { + "@sinclair/typebox": "^0.23.3" + }, + "engines": { + "node": "^12.13.0 || ^14.15.0 || ^16.13.0 || >=17.0.0" + }, + "publishConfig": { + "access": "public" + } +} diff --git a/packages/jest-schemas/src/index.ts b/packages/jest-schemas/src/index.ts new file mode 100644 index 000000000000..b3f0a32eebe8 --- /dev/null +++ b/packages/jest-schemas/src/index.ts @@ -0,0 +1,36 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +import {Static, Type} from '@sinclair/typebox'; + +const RawSnapshotFormat = Type.Partial( + Type.Object({ + callToJSON: Type.Readonly(Type.Boolean()), + escapeRegex: Type.Readonly(Type.Boolean()), + escapeString: Type.Readonly(Type.Boolean()), + highlight: Type.Readonly(Type.Boolean()), + indent: Type.Readonly(Type.Number({minimum: 0})), + maxDepth: Type.Readonly(Type.Number({minimum: 0})), + min: Type.Readonly(Type.Boolean()), + printBasicPrototype: Type.Readonly(Type.Boolean()), + printFunctionName: Type.Readonly(Type.Boolean()), + theme: Type.Readonly( + Type.Partial( + Type.Object({ + comment: Type.Readonly(Type.String()), + content: Type.Readonly(Type.String()), + prop: Type.Readonly(Type.String()), + tag: Type.Readonly(Type.String()), + value: Type.Readonly(Type.String()), + }), + ), + ), + }), +); + +export const SnapshotFormat = Type.Strict(RawSnapshotFormat); +export type SnapshotFormat = Static; diff --git a/packages/jest-schemas/tsconfig.json b/packages/jest-schemas/tsconfig.json new file mode 100644 index 000000000000..92ebe977c1ae --- /dev/null +++ b/packages/jest-schemas/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src", + "outDir": "build" + }, + "include": ["./src/**/*"], + "exclude": ["./**/__mocks__/**/*", "./**/__tests__/**/*"] +} diff --git a/packages/jest-types/package.json b/packages/jest-types/package.json index 011e2d6261fa..25f8c7489dda 100644 --- a/packages/jest-types/package.json +++ b/packages/jest-types/package.json @@ -20,6 +20,7 @@ "./package.json": "./package.json" }, "dependencies": { + "@jest/schemas": "28.0.0-alpha.0", "@types/istanbul-lib-coverage": "^2.0.0", "@types/istanbul-reports": "^3.0.0", "@types/node": "*", diff --git a/packages/jest-types/src/Config.ts b/packages/jest-types/src/Config.ts index fa0d2a205595..c35af9220496 100644 --- a/packages/jest-types/src/Config.ts +++ b/packages/jest-types/src/Config.ts @@ -8,6 +8,7 @@ import type {ForegroundColor} from 'chalk'; import type {ReportOptions} from 'istanbul-reports'; import type {Arguments} from 'yargs'; +import type {SnapshotFormat} from '@jest/schemas'; type CoverageProvider = 'babel' | 'v8'; @@ -60,9 +61,6 @@ export interface ConfigGlobals { [K: string]: unknown; } -// This interface gets filled out when pretty-format is included -export interface PrettyFormatOptions {} - export type DefaultOptions = { automock: boolean; bail: number; @@ -228,7 +226,7 @@ export type InitialOptions = Partial<{ slowTestThreshold: number; snapshotResolver: Path; snapshotSerializers: Array; - snapshotFormat: PrettyFormatOptions; + snapshotFormat: SnapshotFormat; errorOnDeprecated: boolean; testEnvironment: string; testEnvironmentOptions: Record; @@ -330,7 +328,7 @@ export type GlobalConfig = { rootDir: Path; silent?: boolean; skipFilter: boolean; - snapshotFormat: PrettyFormatOptions; + snapshotFormat: SnapshotFormat; errorOnDeprecated: boolean; testFailureExitCode: number; testNamePattern?: string; @@ -393,7 +391,7 @@ export type ProjectConfig = { slowTestThreshold: number; snapshotResolver?: Path; snapshotSerializers: Array; - snapshotFormat: PrettyFormatOptions; + snapshotFormat: SnapshotFormat; testEnvironment: string; testEnvironmentOptions: Record; testMatch: Array; diff --git a/packages/jest-types/tsconfig.json b/packages/jest-types/tsconfig.json index 12688a2879d6..515871de443d 100644 --- a/packages/jest-types/tsconfig.json +++ b/packages/jest-types/tsconfig.json @@ -4,5 +4,6 @@ "rootDir": "src", "outDir": "build" }, - "include": ["./src/**/*"] + "include": ["./src/**/*"], + "references": [{"path": "../jest-schemas"}] } diff --git a/packages/pretty-format/package.json b/packages/pretty-format/package.json index 691a15f1c828..3af8cdc186e9 100644 --- a/packages/pretty-format/package.json +++ b/packages/pretty-format/package.json @@ -20,6 +20,7 @@ }, "author": "James Kyle ", "dependencies": { + "@jest/schemas": "28.0.0-alpha.0", "ansi-regex": "^5.0.1", "ansi-styles": "^5.0.0", "react-is": "^17.0.1" diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index 4723bef1b380..6b72417177da 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ +import type {SnapshotFormat} from '@jest/schemas'; + export type Colors = { comment: {close: string; open: string}; content: {close: string; open: string}; @@ -16,52 +18,19 @@ type Indent = (arg0: string) => string; export type Refs = Array; type Print = (arg0: unknown) => string; -export type Theme = { - comment: string; - content: string; - prop: string; - tag: string; - value: string; -}; - -type ThemeReceived = { - comment?: string; - content?: string; - prop?: string; - tag?: string; - value?: string; -}; +export type Theme = Options['theme']; export type CompareKeys = ((a: string, b: string) => number) | undefined; -export type Options = { - callToJSON: boolean; - compareKeys: CompareKeys; - escapeRegex: boolean; - escapeString: boolean; - highlight: boolean; - indent: number; - maxDepth: number; - min: boolean; - plugins: Plugins; - printBasicPrototype: boolean; - printFunctionName: boolean; - theme: Theme; -}; +type RequiredOptions = Required; + +export interface Options extends Omit { + theme: Required; +} -export interface PrettyFormatOptions { - callToJSON?: boolean; +export interface PrettyFormatOptions extends SnapshotFormat { compareKeys?: CompareKeys; - escapeRegex?: boolean; - escapeString?: boolean; - highlight?: boolean; - indent?: number; - maxDepth?: number; - min?: boolean; plugins?: Plugins; - printBasicPrototype?: boolean; - printFunctionName?: boolean; - theme?: ThemeReceived; } export type OptionsReceived = PrettyFormatOptions; diff --git a/packages/pretty-format/tsconfig.json b/packages/pretty-format/tsconfig.json index e894ea391e2d..b91ffb6b1f33 100644 --- a/packages/pretty-format/tsconfig.json +++ b/packages/pretty-format/tsconfig.json @@ -6,5 +6,5 @@ }, "include": ["./src/**/*"], "exclude": ["./**/__tests__/**/*"], - "references": [{"path": "../jest-util"}] + "references": [{"path": "../jest-schemas"}, {"path": "../jest-util"}] } diff --git a/yarn.lock b/yarn.lock index dd932401915a..3c5d3323f16c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2753,6 +2753,14 @@ __metadata: languageName: unknown linkType: soft +"@jest/schemas@28.0.0-alpha.0, @jest/schemas@workspace:packages/jest-schemas": + version: 0.0.0-use.local + resolution: "@jest/schemas@workspace:packages/jest-schemas" + dependencies: + "@sinclair/typebox": ^0.23.3 + languageName: unknown + linkType: soft + "@jest/source-map@^28.0.0-alpha.0, @jest/source-map@workspace:packages/jest-source-map": version: 0.0.0-use.local resolution: "@jest/source-map@workspace:packages/jest-source-map" @@ -2847,6 +2855,7 @@ __metadata: version: 0.0.0-use.local resolution: "@jest/types@workspace:packages/jest-types" dependencies: + "@jest/schemas": 28.0.0-alpha.0 "@tsd/typescript": ~4.5.5 "@types/istanbul-lib-coverage": ^2.0.0 "@types/istanbul-reports": ^3.0.0 @@ -4359,6 +4368,13 @@ __metadata: languageName: node linkType: hard +"@sinclair/typebox@npm:^0.23.3": + version: 0.23.3 + resolution: "@sinclair/typebox@npm:0.23.3" + checksum: c8d961c8af1b701e67641770376010634730076f0412dda34b01f1d6f54d98916414335b51929285ade43ab85bb14a372bbc287575ce4b1e0179d8af808ec4d7 + languageName: node + linkType: hard + "@sindresorhus/is@npm:^0.14.0": version: 0.14.0 resolution: "@sindresorhus/is@npm:0.14.0" @@ -17802,6 +17818,7 @@ __metadata: version: 0.0.0-use.local resolution: "pretty-format@workspace:packages/pretty-format" dependencies: + "@jest/schemas": 28.0.0-alpha.0 "@types/react": "*" "@types/react-is": ^17.0.0 "@types/react-test-renderer": "*" From 8b2ac167137b6d232a4dc9819c3f0ef4be399f94 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 14 Feb 2022 09:37:04 +0100 Subject: [PATCH 2/3] changelog --- CHANGELOG.md | 1 + packages/jest-schemas/README.md | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 packages/jest-schemas/README.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c890a8f2fe5..884d023a78ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ - `[jest-environment-node]` [**BREAKING**] Add default `node` and `node-addon` conditions to `exportConditions` for `node` environment ([#11924](https://github.com/facebook/jest/pull/11924)) - `[@jest/expect-utils]` New module exporting utils for `expect` ([#12323](https://github.com/facebook/jest/pull/12323)) - `[jest-resolver]` [**BREAKING**] Add support for `package.json` `exports` ([11961](https://github.com/facebook/jest/pull/11961)) +- `[@jes/schemas]` New module for JSON schemas for Jest's config ([#12384](https://github.com/facebook/jest/pull/12384)) - `[jest-worker]` [**BREAKING**] Allow only absolute `workerPath` ([#12343](https://github.com/facebook/jest/pull/12343)) ### Fixes diff --git a/packages/jest-schemas/README.md b/packages/jest-schemas/README.md new file mode 100644 index 000000000000..b2a1d122925e --- /dev/null +++ b/packages/jest-schemas/README.md @@ -0,0 +1,3 @@ +# `@jest/schemas` + +Experimental and currently incomplete module for JSON schemas for [Jest's](https://jestjs.io/) configuration. From 3e13ce48aaa1d90db60ab4daca8fb27a2f4172b8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 14 Feb 2022 09:43:38 +0100 Subject: [PATCH 3/3] can be undefined --- packages/pretty-format/src/types.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/pretty-format/src/types.ts b/packages/pretty-format/src/types.ts index 6b72417177da..c26b77aee0f7 100644 --- a/packages/pretty-format/src/types.ts +++ b/packages/pretty-format/src/types.ts @@ -24,7 +24,9 @@ export type CompareKeys = ((a: string, b: string) => number) | undefined; type RequiredOptions = Required; -export interface Options extends Omit { +export interface Options + extends Omit { + compareKeys: CompareKeys; theme: Required; }