Skip to content

Commit

Permalink
feat: create new @jest/schemas type (#12384)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB committed Feb 14, 2022
1 parent c961be9 commit c8a8456
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 47 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions packages/jest-schemas/.npmignore
@@ -0,0 +1,6 @@
**/__mocks__/**
**/__tests__/**
src
tsconfig.json
tsconfig.tsbuildinfo
api-extractor.json
3 changes: 3 additions & 0 deletions 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.
28 changes: 28 additions & 0 deletions 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"
}
}
36 changes: 36 additions & 0 deletions 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<typeof RawSnapshotFormat>;
9 changes: 9 additions & 0 deletions packages/jest-schemas/tsconfig.json
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "build"
},
"include": ["./src/**/*"],
"exclude": ["./**/__mocks__/**/*", "./**/__tests__/**/*"]
}
1 change: 1 addition & 0 deletions packages/jest-types/package.json
Expand Up @@ -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": "*",
Expand Down
10 changes: 4 additions & 6 deletions packages/jest-types/src/Config.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -228,7 +226,7 @@ export type InitialOptions = Partial<{
slowTestThreshold: number;
snapshotResolver: Path;
snapshotSerializers: Array<Path>;
snapshotFormat: PrettyFormatOptions;
snapshotFormat: SnapshotFormat;
errorOnDeprecated: boolean;
testEnvironment: string;
testEnvironmentOptions: Record<string, unknown>;
Expand Down Expand Up @@ -330,7 +328,7 @@ export type GlobalConfig = {
rootDir: Path;
silent?: boolean;
skipFilter: boolean;
snapshotFormat: PrettyFormatOptions;
snapshotFormat: SnapshotFormat;
errorOnDeprecated: boolean;
testFailureExitCode: number;
testNamePattern?: string;
Expand Down Expand Up @@ -393,7 +391,7 @@ export type ProjectConfig = {
slowTestThreshold: number;
snapshotResolver?: Path;
snapshotSerializers: Array<Path>;
snapshotFormat: PrettyFormatOptions;
snapshotFormat: SnapshotFormat;
testEnvironment: string;
testEnvironmentOptions: Record<string, unknown>;
testMatch: Array<Glob>;
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-types/tsconfig.json
Expand Up @@ -4,5 +4,6 @@
"rootDir": "src",
"outDir": "build"
},
"include": ["./src/**/*"]
"include": ["./src/**/*"],
"references": [{"path": "../jest-schemas"}]
}
1 change: 1 addition & 0 deletions packages/pretty-format/package.json
Expand Up @@ -20,6 +20,7 @@
},
"author": "James Kyle <me@thejameskyle.com>",
"dependencies": {
"@jest/schemas": "28.0.0-alpha.0",
"ansi-regex": "^5.0.1",
"ansi-styles": "^5.0.0",
"react-is": "^17.0.1"
Expand Down
49 changes: 10 additions & 39 deletions packages/pretty-format/src/types.ts
Expand Up @@ -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};
Expand All @@ -16,52 +18,21 @@ type Indent = (arg0: string) => string;
export type Refs = Array<unknown>;
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;
type RequiredOptions = Required<PrettyFormatOptions>;

export interface Options
extends Omit<RequiredOptions, 'compareKeys' | 'theme'> {
compareKeys: CompareKeys;
escapeRegex: boolean;
escapeString: boolean;
highlight: boolean;
indent: number;
maxDepth: number;
min: boolean;
plugins: Plugins;
printBasicPrototype: boolean;
printFunctionName: boolean;
theme: Theme;
};
theme: Required<RequiredOptions['theme']>;
}

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;
Expand Down
2 changes: 1 addition & 1 deletion packages/pretty-format/tsconfig.json
Expand Up @@ -6,5 +6,5 @@
},
"include": ["./src/**/*"],
"exclude": ["./**/__tests__/**/*"],
"references": [{"path": "../jest-util"}]
"references": [{"path": "../jest-schemas"}, {"path": "../jest-util"}]
}
17 changes: 17 additions & 0 deletions yarn.lock
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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": "*"
Expand Down

0 comments on commit c8a8456

Please sign in to comment.