vitest 0.0.33 → 0.0.37

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.gh.md +67 -11
  2. package/dist/chunk-4OVO6RD6.js +1 -0
  3. package/dist/chunk-CUG6SVUZ.js +1 -0
  4. package/dist/chunk-F27UALKJ.js +1 -0
  5. package/dist/chunk-XPSQDVUG.js +1 -0
  6. package/dist/global-CKRMAXWF.js +1 -0
  7. package/dist/happy-dom-RNJZR3YC.js +1 -0
  8. package/dist/index.d.ts +73 -6
  9. package/dist/index.js +1 -4
  10. package/dist/jsdom-6QAZGE6T.js +1 -0
  11. package/dist/node/cli.d.ts +5 -1
  12. package/dist/node/cli.js +7 -86
  13. package/dist/node/entry.d.ts +1 -1
  14. package/dist/node/entry.js +19 -12
  15. package/dist/types-d3253f2d.d.ts +213 -0
  16. package/package.json +9 -4
  17. package/dist/constants.d.ts +0 -3
  18. package/dist/constants.js +0 -23
  19. package/dist/context.d.ts +0 -2
  20. package/dist/context.js +0 -4
  21. package/dist/integrations/chai/index.d.ts +0 -3
  22. package/dist/integrations/chai/index.js +0 -3
  23. package/dist/integrations/chai/jest-expect.d.ts +0 -2
  24. package/dist/integrations/chai/jest-expect.js +0 -67
  25. package/dist/integrations/chai/setup.d.ts +0 -2
  26. package/dist/integrations/chai/setup.js +0 -11
  27. package/dist/integrations/chai/snapshot/index.d.ts +0 -8
  28. package/dist/integrations/chai/snapshot/index.js +0 -27
  29. package/dist/integrations/chai/snapshot/manager.d.ts +0 -30
  30. package/dist/integrations/chai/snapshot/manager.js +0 -77
  31. package/dist/integrations/chai/snapshot/utils/jest-config-helper.d.ts +0 -3
  32. package/dist/integrations/chai/snapshot/utils/jest-config-helper.js +0 -39
  33. package/dist/integrations/chai/snapshot/utils/jest-reporters-lite.d.ts +0 -2
  34. package/dist/integrations/chai/snapshot/utils/jest-reporters-lite.js +0 -71
  35. package/dist/integrations/chai/snapshot/utils/jest-test-result-helper.d.ts +0 -6
  36. package/dist/integrations/chai/snapshot/utils/jest-test-result-helper.js +0 -66
  37. package/dist/integrations/chai/snapshot/utils/types.d.ts +0 -29
  38. package/dist/integrations/chai/snapshot/utils/types.js +0 -1
  39. package/dist/integrations/chai/types.d.ts +0 -3
  40. package/dist/integrations/chai/types.js +0 -1
  41. package/dist/integrations/global.d.ts +0 -1
  42. package/dist/integrations/global.js +0 -8
  43. package/dist/integrations/jsdom/index.d.ts +0 -5
  44. package/dist/integrations/jsdom/index.js +0 -21
  45. package/dist/integrations/jsdom/keys.d.ts +0 -1
  46. package/dist/integrations/jsdom/keys.js +0 -220
  47. package/dist/integrations/sinon.d.ts +0 -3
  48. package/dist/integrations/sinon.js +0 -5
  49. package/dist/node/node.d.ts +0 -23
  50. package/dist/node/node.js +0 -159
  51. package/dist/reporters/default.d.ts +0 -24
  52. package/dist/reporters/default.js +0 -149
  53. package/dist/reporters/error.d.ts +0 -9
  54. package/dist/reporters/error.js +0 -182
  55. package/dist/run/index.d.ts +0 -8
  56. package/dist/run/index.js +0 -248
  57. package/dist/suite.d.ts +0 -26
  58. package/dist/suite.js +0 -91
  59. package/dist/types.d.ts +0 -138
  60. package/dist/types.js +0 -1
  61. package/dist/utils/hook.d.ts +0 -5
  62. package/dist/utils/hook.js +0 -14
@@ -0,0 +1,213 @@
1
+ import { Awaitable } from '@antfu/utils';
2
+ import { SnapshotStateType, SnapshotResolver } from 'jest-snapshot';
3
+ import { SnapshotStateOptions } from 'jest-snapshot/build/State';
4
+
5
+ interface UncheckedSnapshot {
6
+ filePath: string;
7
+ keys: Array<string>;
8
+ }
9
+ interface SnapshotSummary {
10
+ added: number;
11
+ didUpdate: boolean;
12
+ failure: boolean;
13
+ filesAdded: number;
14
+ filesRemoved: number;
15
+ filesRemovedList: Array<string>;
16
+ filesUnmatched: number;
17
+ filesUpdated: number;
18
+ matched: number;
19
+ total: number;
20
+ unchecked: number;
21
+ uncheckedKeysByFile: Array<UncheckedSnapshot>;
22
+ unmatched: number;
23
+ updated: number;
24
+ }
25
+
26
+ interface Context {
27
+ file: string;
28
+ title?: string;
29
+ fullTitle?: string;
30
+ }
31
+ declare class SnapshotManager {
32
+ snapshotState: SnapshotStateType | null;
33
+ snapshotSummary: SnapshotSummary;
34
+ snapshotOptions: SnapshotStateOptions;
35
+ context: Context | null;
36
+ testFile: string;
37
+ snapshotResolver: SnapshotResolver | null;
38
+ rootDir: string;
39
+ constructor({ rootDir, update, snapshotResolver, }: {
40
+ rootDir: string;
41
+ update?: boolean;
42
+ snapshotResolver?: SnapshotResolver | null;
43
+ });
44
+ onFileChanged(): void;
45
+ setTask(task: Task): void;
46
+ setContext(context: Context): void;
47
+ assert(received: unknown, message: string): void;
48
+ clear(): void;
49
+ saveSnap(): void;
50
+ report(): string[] | undefined;
51
+ }
52
+
53
+ interface UserOptions {
54
+ /**
55
+ * Include globs for test files
56
+ *
57
+ * @default ['**\/*.test.ts']
58
+ */
59
+ includes?: string[];
60
+ /**
61
+ * Exclude globs for test files
62
+ * @default ['**\/node_modules\/**']
63
+ */
64
+ excludes?: string[];
65
+ /**
66
+ * Handling for dependencies inlining or externalizing
67
+ */
68
+ deps?: {
69
+ external?: (string | RegExp)[];
70
+ inline?: (string | RegExp)[];
71
+ };
72
+ /**
73
+ * Register apis globally
74
+ *
75
+ * @default false
76
+ */
77
+ global?: boolean;
78
+ /**
79
+ * Use `jsdom` or `happy-dom` to mock browser APIs
80
+ *
81
+ * @default false
82
+ */
83
+ dom?: boolean | 'jsdom' | 'happy-dom';
84
+ /**
85
+ * Run tests files in parallel
86
+ *
87
+ * @default false
88
+ */
89
+ parallel?: boolean;
90
+ /**
91
+ * Update snapshot files
92
+ *
93
+ * @default false
94
+ */
95
+ update?: boolean;
96
+ /**
97
+ * Watch mode
98
+ *
99
+ * @default false
100
+ */
101
+ watch?: boolean;
102
+ /**
103
+ * Project root
104
+ */
105
+ root?: string;
106
+ /**
107
+ * Custom reporter for output
108
+ */
109
+ reporter?: Reporter;
110
+ }
111
+ interface ResolvedConfig extends Required<UserOptions> {
112
+ filters?: string[];
113
+ config?: string;
114
+ }
115
+ declare type RunMode = 'run' | 'skip' | 'only' | 'todo';
116
+ declare type TaskState = RunMode | 'pass' | 'fail';
117
+ interface ConcurrentOptions {
118
+ timeout: number;
119
+ }
120
+ interface Task {
121
+ name: string;
122
+ mode: RunMode;
123
+ concurrent?: ConcurrentOptions;
124
+ suite: Suite;
125
+ fn: () => Awaitable<void>;
126
+ file?: File;
127
+ state?: TaskState;
128
+ error?: unknown;
129
+ }
130
+ declare type TestFunction = () => Awaitable<void>;
131
+ interface ConcurrentCollector {
132
+ (name: string, fn: TestFunction, timeout?: number): void;
133
+ only: (name: string, fn: TestFunction, timeout?: number) => void;
134
+ skip: (name: string, fn: TestFunction, timeout?: number) => void;
135
+ todo: (name: string) => void;
136
+ }
137
+ interface OnlyCollector {
138
+ (name: string, fn: TestFunction): void;
139
+ concurrent: (name: string, fn: TestFunction, timeout?: number) => void;
140
+ }
141
+ interface SkipCollector {
142
+ (name: string, fn: TestFunction): void;
143
+ concurrent: (name: string, fn: TestFunction, timeout?: number) => void;
144
+ }
145
+ interface TodoCollector {
146
+ (name: string): void;
147
+ concurrent: (name: string) => void;
148
+ }
149
+ interface TestCollector {
150
+ (name: string, fn: TestFunction): void;
151
+ concurrent: ConcurrentCollector;
152
+ only: OnlyCollector;
153
+ skip: SkipCollector;
154
+ todo: TodoCollector;
155
+ }
156
+ declare type HookListener<T extends any[]> = (...args: T) => Awaitable<void>;
157
+ interface Suite {
158
+ name: string;
159
+ mode: RunMode;
160
+ tasks: Task[];
161
+ file?: File;
162
+ error?: unknown;
163
+ status?: TaskState;
164
+ hooks: {
165
+ beforeAll: HookListener<[Suite]>[];
166
+ afterAll: HookListener<[Suite]>[];
167
+ beforeEach: HookListener<[Task, Suite]>[];
168
+ afterEach: HookListener<[Task, Suite]>[];
169
+ };
170
+ }
171
+ interface SuiteCollector {
172
+ readonly name: string;
173
+ readonly mode: RunMode;
174
+ test: TestCollector;
175
+ collect: (file?: File) => Promise<Suite>;
176
+ clear: () => void;
177
+ on: <T extends keyof Suite['hooks']>(name: T, ...fn: Suite['hooks'][T]) => void;
178
+ }
179
+ declare type TestFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable<void>;
180
+ interface File {
181
+ filepath: string;
182
+ suites: Suite[];
183
+ collected: boolean;
184
+ error?: unknown;
185
+ }
186
+ interface RunnerContext {
187
+ filesMap: Record<string, File>;
188
+ files: File[];
189
+ suites: Suite[];
190
+ tasks: Task[];
191
+ config: ResolvedConfig;
192
+ reporter: Reporter;
193
+ snapshotManager: SnapshotManager;
194
+ }
195
+ interface GlobalContext {
196
+ suites: SuiteCollector[];
197
+ currentSuite: SuiteCollector | null;
198
+ }
199
+ interface Reporter {
200
+ onStart?: (config: ResolvedConfig) => Awaitable<void>;
201
+ onCollected?: (files: File[], ctx: RunnerContext) => Awaitable<void>;
202
+ onFinished?: (ctx: RunnerContext, files?: File[]) => Awaitable<void>;
203
+ onSuiteBegin?: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
204
+ onSuiteEnd?: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
205
+ onFileBegin?: (file: File, ctx: RunnerContext) => Awaitable<void>;
206
+ onFileEnd?: (file: File, ctx: RunnerContext) => Awaitable<void>;
207
+ onTaskBegin?: (task: Task, ctx: RunnerContext) => Awaitable<void>;
208
+ onTaskEnd?: (task: Task, ctx: RunnerContext) => Awaitable<void>;
209
+ onWatcherStart?: (ctx: RunnerContext) => Awaitable<void>;
210
+ onWatcherRerun?: (files: string[], trigger: string, ctx: RunnerContext) => Awaitable<void>;
211
+ }
212
+
213
+ export { ConcurrentOptions as C, File as F, GlobalContext as G, HookListener as H, ResolvedConfig as R, SuiteCollector as S, TestFunction as T, UserOptions as U, TestFactory as a, Suite as b, RunMode as c, TaskState as d, Task as e, TestCollector as f, RunnerContext as g, Reporter as h };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.33",
3
+ "version": "0.0.37",
4
4
  "description": "A blazing fast unit test framework powered by Vite",
5
5
  "keywords": [
6
6
  "vite",
@@ -8,6 +8,9 @@
8
8
  "test",
9
9
  "jest"
10
10
  ],
11
+ "engines": {
12
+ "node": ">=16.0.0"
13
+ },
11
14
  "homepage": "https://github.com/antfu/vitest#readme",
12
15
  "bugs": {
13
16
  "url": "https://github.com/antfu/vitest/issues"
@@ -40,7 +43,8 @@
40
43
  ],
41
44
  "scripts": {
42
45
  "prepare": "esmo scripts/generate-types.ts",
43
- "build": "rimraf dist && tsc -p src/tsconfig.json",
46
+ "build": "tsup --dts --minify",
47
+ "dev": "tsup --watch src",
44
48
  "lint": "eslint \"{src,test}/**/*.ts\"",
45
49
  "prepublishOnly": "nr build",
46
50
  "release": "bumpp --commit --push --tag && esmo scripts/publish.ts",
@@ -48,8 +52,7 @@
48
52
  "test:core": "node bin/vitest.mjs --dev -r test/core",
49
53
  "test:vue": "node bin/vitest.mjs --dev -r test/vue",
50
54
  "test:react": "node bin/vitest.mjs --dev -r test/react",
51
- "coverage": "node bin/vitest.mjs --dev -r test/core --coverage",
52
- "watch": "tsc -p src/tsconfig.json --watch"
55
+ "coverage": "node bin/vitest.mjs --dev -r test/core --coverage"
53
56
  },
54
57
  "dependencies": {
55
58
  "@antfu/utils": "^0.3.0",
@@ -62,6 +65,7 @@
62
65
  "diff": "^5.0.0",
63
66
  "fast-glob": "^3.2.7",
64
67
  "find-up": "^6.2.0",
68
+ "happy-dom": "^2.24.4",
65
69
  "jest-snapshot": "^27.4.2",
66
70
  "jest-util": "^27.4.2",
67
71
  "jsdom": "^19.0.0",
@@ -90,6 +94,7 @@
90
94
  "esno": "^0.12.1",
91
95
  "npm-run-all": "^4.1.5",
92
96
  "rimraf": "^3.0.2",
97
+ "tsup": "^5.10.3",
93
98
  "typescript": "^4.5.2",
94
99
  "vite": "^2.7.1"
95
100
  }
@@ -1,3 +0,0 @@
1
- export declare const defaultIncludes: string[];
2
- export declare const defaultExcludes: string[];
3
- export declare const globalApis: string[];
package/dist/constants.js DELETED
@@ -1,23 +0,0 @@
1
- export const defaultIncludes = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'];
2
- export const defaultExcludes = ['**/node_modules/**', '**/dist/**'];
3
- export const globalApis = [
4
- // suite
5
- 'suite',
6
- 'test',
7
- 'describe',
8
- 'it',
9
- // chai
10
- 'chai',
11
- 'expect',
12
- 'assert',
13
- // sinon
14
- 'sinon',
15
- 'spy',
16
- 'mock',
17
- 'stub',
18
- // hooks
19
- 'beforeAll',
20
- 'afterAll',
21
- 'beforeEach',
22
- 'afterEach',
23
- ];
package/dist/context.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { GlobalContext } from './types';
2
- export declare const context: GlobalContext;
package/dist/context.js DELETED
@@ -1,4 +0,0 @@
1
- export const context = {
2
- suites: [],
3
- currentSuite: null,
4
- };
@@ -1,3 +0,0 @@
1
- import chai from 'chai';
2
- export { assert, should, expect } from 'chai';
3
- export { chai };
@@ -1,3 +0,0 @@
1
- import chai from 'chai';
2
- export { assert, should, expect } from 'chai';
3
- export { chai };
@@ -1,2 +0,0 @@
1
- import { ChaiPlugin } from './types';
2
- export declare function JestChaiExpect(): ChaiPlugin;
@@ -1,67 +0,0 @@
1
- // Jest Expect Compact
2
- // TODO: add more https://jestjs.io/docs/expect
3
- export function JestChaiExpect() {
4
- return (chai, utils) => {
5
- const proto = chai.Assertion.prototype;
6
- utils.addMethod(proto, 'toEqual', function (expected) {
7
- return this.eql(expected);
8
- });
9
- utils.addMethod(proto, 'toStrictEqual', function (expected) {
10
- return this.equal(expected);
11
- });
12
- utils.addMethod(proto, 'toBe', function (expected) {
13
- return this.equal(expected);
14
- });
15
- utils.addMethod(proto, 'toMatchObject', function (expected) {
16
- return this.containSubset(expected);
17
- });
18
- utils.addMethod(proto, 'toMatch', function (expected) {
19
- if (typeof expected === 'string')
20
- return this.include(expected);
21
- else
22
- return this.match(expected);
23
- });
24
- utils.addMethod(proto, 'toContain', function (item) {
25
- return this.contain(item);
26
- });
27
- utils.addMethod(proto, 'toBeTruthy', function () {
28
- const obj = utils.flag(this, 'object');
29
- this.assert(Boolean(obj), 'expected #{this} to be truthy', 'expected #{this} to not be truthy', obj);
30
- });
31
- utils.addMethod(proto, 'toBeFalsy', function () {
32
- const obj = utils.flag(this, 'object');
33
- this.assert(!obj, 'expected #{this} to be falsy', 'expected #{this} to not be falsy', obj);
34
- });
35
- utils.addMethod(proto, 'toBeNaN', function () {
36
- return this.be.NaN;
37
- });
38
- utils.addMethod(proto, 'toBeUndefined', function () {
39
- return this.be.undefined;
40
- });
41
- utils.addMethod(proto, 'toBeNull', function () {
42
- return this.be.null;
43
- });
44
- utils.addMethod(proto, 'toBeDefined', function () {
45
- return this.not.be.undefined;
46
- });
47
- utils.addMethod(proto, 'toBeInstanceOf', function (obj) {
48
- return this.instanceOf(obj);
49
- });
50
- // mock
51
- utils.addMethod(proto, 'toHaveBeenCalledTimes', function (number) {
52
- return this.callCount(number);
53
- });
54
- utils.addMethod(proto, 'toHaveBeenCalledOnce', function () {
55
- return this.callCount(1);
56
- });
57
- utils.addMethod(proto, 'toHaveBeenCalled', function () {
58
- return this.called;
59
- });
60
- utils.addMethod(proto, 'toHaveBeenCalled', function () {
61
- return this.called;
62
- });
63
- utils.addMethod(proto, 'toHaveBeenCalledWith', function (...args) {
64
- return this.calledWith(...args);
65
- });
66
- };
67
- }
@@ -1,2 +0,0 @@
1
- import { ResolvedConfig } from 'vitest';
2
- export declare function setupChai(config: ResolvedConfig): Promise<void>;
@@ -1,11 +0,0 @@
1
- import chai from 'chai';
2
- import SinonChai from 'sinon-chai';
3
- import Subset from 'chai-subset';
4
- import { JestChaiExpect } from './jest-expect';
5
- import { SnapshotPlugin } from './snapshot';
6
- export async function setupChai(config) {
7
- chai.use(SinonChai);
8
- chai.use(JestChaiExpect());
9
- chai.use(Subset);
10
- chai.use(await SnapshotPlugin(config));
11
- }
@@ -1,8 +0,0 @@
1
- import { ChaiPlugin } from '../types';
2
- import { SnapshotManager } from './manager';
3
- export interface SnapshotOptions {
4
- root: string;
5
- update?: boolean;
6
- }
7
- export declare function getSnapshotManager(): SnapshotManager;
8
- export declare function SnapshotPlugin(options: SnapshotOptions): Promise<ChaiPlugin>;
@@ -1,27 +0,0 @@
1
- import Snap from 'jest-snapshot';
2
- import { SnapshotManager } from './manager';
3
- const { addSerializer } = Snap;
4
- let _manager;
5
- export function getSnapshotManager() {
6
- return _manager;
7
- }
8
- export async function SnapshotPlugin(options) {
9
- const { root: rootDir } = options;
10
- _manager = new SnapshotManager({
11
- rootDir,
12
- update: options.update,
13
- });
14
- _manager.snapshotResolver = await Snap.buildSnapshotResolver({
15
- transform: [],
16
- rootDir,
17
- });
18
- return function (chai, utils) {
19
- for (const key of ['matchSnapshot', 'toMatchSnapshot']) {
20
- utils.addMethod(chai.Assertion.prototype, key, function (message) {
21
- const expected = utils.flag(this, 'object');
22
- _manager.assert(expected, message);
23
- });
24
- }
25
- chai.expect.addSnapshotSerializer = addSerializer;
26
- };
27
- }
@@ -1,30 +0,0 @@
1
- import type { SnapshotStateType, SnapshotResolver } from 'jest-snapshot';
2
- import type { SnapshotStateOptions } from 'jest-snapshot/build/State';
3
- import { Task } from '../../../types';
4
- import { SnapshotSummary } from './utils/types';
5
- export interface Context {
6
- file: string;
7
- title?: string;
8
- fullTitle?: string;
9
- }
10
- export declare class SnapshotManager {
11
- snapshotState: SnapshotStateType | null;
12
- snapshotSummary: SnapshotSummary;
13
- snapshotOptions: SnapshotStateOptions;
14
- context: Context | null;
15
- testFile: string;
16
- snapshotResolver: SnapshotResolver | null;
17
- rootDir: string;
18
- constructor({ rootDir, update, snapshotResolver, }: {
19
- rootDir: string;
20
- update?: boolean;
21
- snapshotResolver?: SnapshotResolver | null;
22
- });
23
- onFileChanged(): void;
24
- setTask(task: Task): void;
25
- setContext(context: Context): void;
26
- assert(received: unknown, message: string): void;
27
- clear(): void;
28
- saveSnap(): void;
29
- report(): string[] | undefined;
30
- }
@@ -1,77 +0,0 @@
1
- import chai from 'chai';
2
- import Snap from 'jest-snapshot';
3
- import { packSnapshotState, addSnapshotResult, makeEmptySnapshotSummary, } from './utils/jest-test-result-helper';
4
- import { getSnapshotSummaryOutput } from './utils/jest-reporters-lite';
5
- const { expect } = chai;
6
- const { SnapshotState } = Snap;
7
- export class SnapshotManager {
8
- constructor({ rootDir, update, snapshotResolver = null, }) {
9
- this.snapshotState = null;
10
- this.context = null;
11
- this.testFile = '';
12
- this.rootDir = rootDir;
13
- this.snapshotResolver = snapshotResolver;
14
- const env = process.env;
15
- const CI = !!env.CI;
16
- const UPDATE_SNAPSHOT = update || env.UPDATE_SNAPSHOT;
17
- this.snapshotOptions = {
18
- updateSnapshot: CI && !UPDATE_SNAPSHOT
19
- ? 'none'
20
- : UPDATE_SNAPSHOT
21
- ? 'all'
22
- : 'new',
23
- };
24
- this.snapshotSummary = makeEmptySnapshotSummary(this.snapshotOptions);
25
- }
26
- onFileChanged() {
27
- if (!this.context)
28
- return;
29
- if (this.snapshotState !== null)
30
- this.saveSnap();
31
- this.testFile = this.context.file;
32
- this.snapshotState = new SnapshotState(this.snapshotResolver.resolveSnapshotPath(this.testFile), this.snapshotOptions);
33
- }
34
- setTask(task) {
35
- var _a;
36
- this.setContext({
37
- file: ((_a = task.file) === null || _a === void 0 ? void 0 : _a.filepath) || task.name,
38
- title: task.name,
39
- fullTitle: [task.suite.name, task.name].filter(Boolean).join(' > '),
40
- });
41
- }
42
- setContext(context) {
43
- if (!context.title || !context.file)
44
- return;
45
- this.context = context;
46
- if (this.testFile !== context.file)
47
- this.onFileChanged();
48
- }
49
- assert(received, message) {
50
- if (!this.snapshotState || !this.context)
51
- return;
52
- const { actual, expected, key, pass } = this.snapshotState.match({
53
- testName: this.context.fullTitle || this.context.title || this.context.file,
54
- received,
55
- isInline: false,
56
- });
57
- if (!pass) {
58
- expect(actual.trim()).equals(expected ? expected.trim() : '', message || `Snapshot name: \`${key}\``);
59
- }
60
- }
61
- clear() {
62
- this.snapshotSummary = makeEmptySnapshotSummary(this.snapshotOptions);
63
- }
64
- saveSnap() {
65
- if (!this.testFile || !this.snapshotState)
66
- return;
67
- const packedSnapshotState = packSnapshotState(this.snapshotState);
68
- addSnapshotResult(this.snapshotSummary, packedSnapshotState, this.testFile);
69
- this.testFile = '';
70
- this.snapshotState = null;
71
- }
72
- report() {
73
- const outputs = getSnapshotSummaryOutput(this.rootDir, this.snapshotSummary);
74
- if (outputs.length > 1)
75
- return outputs;
76
- }
77
- }
@@ -1,3 +0,0 @@
1
- export declare const replaceRootDirInPath: (rootDir: string, filePath: string) => string;
2
- export declare function replaceRootDirInObject<T>(rootDir: string, config: T): T;
3
- export declare function _replaceRootDirTags<T>(rootDir: string, config: T): T;
@@ -1,39 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- import path from 'path';
8
- export const replaceRootDirInPath = (rootDir, filePath) => {
9
- if (!/^<rootDir>/.test(filePath))
10
- return filePath;
11
- return path.resolve(rootDir, path.normalize(`./${filePath.substr('<rootDir>'.length)}`));
12
- };
13
- export function replaceRootDirInObject(rootDir, config) {
14
- const newConfig = {};
15
- for (const configKey of Object.keys(config)) {
16
- newConfig[configKey]
17
- = configKey === 'rootDir'
18
- ? config[configKey]
19
- : _replaceRootDirTags(rootDir, config[configKey]);
20
- }
21
- return newConfig;
22
- }
23
- export function _replaceRootDirTags(rootDir, config) {
24
- if (config == null)
25
- return config;
26
- switch (typeof config) {
27
- case 'object':
28
- if (Array.isArray(config)) {
29
- /// can be string[] or {}[]
30
- return config.map(item => _replaceRootDirTags(rootDir, item));
31
- }
32
- if (config instanceof RegExp)
33
- return config;
34
- return replaceRootDirInObject(rootDir, config);
35
- case 'string':
36
- return replaceRootDirInPath(rootDir, config);
37
- }
38
- return config;
39
- }
@@ -1,2 +0,0 @@
1
- import { SnapshotSummary } from './types';
2
- export declare const getSnapshotSummaryOutput: (rootDir: string, snapshots: SnapshotSummary) => Array<string>;
@@ -1,71 +0,0 @@
1
- /**
2
- * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
3
- *
4
- * This source code is licensed under the MIT license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- */
7
- import path, { isAbsolute } from 'path';
8
- import { pluralize } from 'jest-util';
9
- import slash from 'slash';
10
- import c from 'picocolors';
11
- const formatTestPath = (rootDir, testPath) => {
12
- if (isAbsolute(testPath))
13
- testPath = path.relative(rootDir, testPath);
14
- const dirname = path.dirname(testPath);
15
- const basename = path.basename(testPath);
16
- return slash(c.dim(dirname + path.sep) + c.bold(basename));
17
- };
18
- const ARROW = ' \u203A ';
19
- const DOWN_ARROW = ' \u21B3 ';
20
- const DOT = ' \u2022 ';
21
- const FAIL_COLOR = (v) => c.bold(c.red(v));
22
- const OBSOLETE_COLOR = (v) => c.bold(c.yellow(v));
23
- const SNAPSHOT_ADDED = (v) => c.bold(c.green(v));
24
- const SNAPSHOT_NOTE = c.dim;
25
- const SNAPSHOT_REMOVED = (v) => c.bold(c.green(v));
26
- const SNAPSHOT_SUMMARY = c.bold;
27
- const SNAPSHOT_UPDATED = (v) => c.bold(c.green(v));
28
- const updateCommand = 're-run mocha with `--update` to update them';
29
- export const getSnapshotSummaryOutput = (rootDir, snapshots) => {
30
- const summary = [];
31
- summary.push(SNAPSHOT_SUMMARY('Snapshot Summary'));
32
- if (snapshots.added) {
33
- summary.push(`${SNAPSHOT_ADDED(`${ARROW + pluralize('snapshot', snapshots.added)} written `)}from ${pluralize('test suite', snapshots.filesAdded)}.`);
34
- }
35
- if (snapshots.unmatched) {
36
- summary.push(`${FAIL_COLOR(`${ARROW}${pluralize('snapshot', snapshots.unmatched)} failed`)} from ${pluralize('test suite', snapshots.filesUnmatched)}. ${SNAPSHOT_NOTE(`Inspect your code changes or ${updateCommand} to update them.`)}`);
37
- }
38
- if (snapshots.updated) {
39
- summary.push(`${SNAPSHOT_UPDATED(`${ARROW + pluralize('snapshot', snapshots.updated)} updated `)}from ${pluralize('test suite', snapshots.filesUpdated)}.`);
40
- }
41
- if (snapshots.filesRemoved) {
42
- if (snapshots.didUpdate) {
43
- summary.push(`${SNAPSHOT_REMOVED(`${ARROW}${pluralize('snapshot file', snapshots.filesRemoved)} removed `)}from ${pluralize('test suite', snapshots.filesRemoved)}.`);
44
- }
45
- else {
46
- summary.push(`${OBSOLETE_COLOR(`${ARROW}${pluralize('snapshot file', snapshots.filesRemoved)} obsolete `)}from ${pluralize('test suite', snapshots.filesRemoved)}. ${SNAPSHOT_NOTE(`To remove ${snapshots.filesRemoved === 1 ? 'it' : 'them all'}, ${updateCommand}.`)}`);
47
- }
48
- }
49
- if (snapshots.filesRemovedList && snapshots.filesRemovedList.length) {
50
- const [head, ...tail] = snapshots.filesRemovedList;
51
- summary.push(` ${DOWN_ARROW} ${DOT}${formatTestPath(rootDir, head)}`);
52
- tail.forEach((key) => {
53
- summary.push(` ${DOT}${formatTestPath(rootDir, key)}`);
54
- });
55
- }
56
- if (snapshots.unchecked) {
57
- if (snapshots.didUpdate) {
58
- summary.push(`${SNAPSHOT_REMOVED(`${ARROW}${pluralize('snapshot', snapshots.unchecked)} removed `)}from ${pluralize('test suite', snapshots.uncheckedKeysByFile.length)}.`);
59
- }
60
- else {
61
- summary.push(`${OBSOLETE_COLOR(`${ARROW}${pluralize('snapshot', snapshots.unchecked)} obsolete `)}from ${pluralize('test suite', snapshots.uncheckedKeysByFile.length)}. ${SNAPSHOT_NOTE(`To remove ${snapshots.unchecked === 1 ? 'it' : 'them all'}, ${updateCommand}.`)}`);
62
- }
63
- snapshots.uncheckedKeysByFile.forEach((uncheckedFile) => {
64
- summary.push(` ${DOWN_ARROW}${formatTestPath(rootDir, uncheckedFile.filePath)}`);
65
- uncheckedFile.keys.forEach((key) => {
66
- summary.push(` ${DOT}${key}`);
67
- });
68
- });
69
- }
70
- return summary;
71
- };
@@ -1,6 +0,0 @@
1
- import { SnapshotStateType } from 'jest-snapshot';
2
- import { SnapshotStateOptions } from 'jest-snapshot/build/State';
3
- import { SnapshotSummary, SnapshotResult } from './types';
4
- export declare const makeEmptySnapshotSummary: (options: SnapshotStateOptions) => SnapshotSummary;
5
- export declare const packSnapshotState: (snapshotState: SnapshotStateType) => SnapshotResult;
6
- export declare const addSnapshotResult: (snapshotSummary: SnapshotSummary, snapshotResult: SnapshotResult, testFilePath: string) => void;