vitest 0.0.16 → 0.0.17

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.
package/dist/constants.js CHANGED
@@ -1,22 +1,23 @@
1
1
  export const defaultIncludes = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'];
2
2
  export const defaultExcludes = ['**/node_modules/**', '**/dist/**'];
3
3
  export const globalApis = [
4
+ // suite
4
5
  'suite',
5
6
  'test',
6
7
  'describe',
7
8
  'it',
9
+ // chai
10
+ 'chai',
8
11
  'expect',
9
12
  'assert',
13
+ // sinon
14
+ 'sinon',
10
15
  'spy',
11
16
  'mock',
12
17
  'stub',
13
- 'sinon',
18
+ // hooks
14
19
  'beforeAll',
15
20
  'afterAll',
16
21
  'beforeEach',
17
22
  'afterEach',
18
- 'beforeFile',
19
- 'afterFile',
20
- 'beforeSuite',
21
- 'afterSuite',
22
23
  ];
package/dist/index.d.ts CHANGED
@@ -3,4 +3,3 @@ export * from './suite';
3
3
  export * from './config';
4
4
  export * from './integrations/chai';
5
5
  export * from './integrations/sinon';
6
- export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks';
package/dist/index.js CHANGED
@@ -3,4 +3,3 @@ export * from './suite';
3
3
  export * from './config';
4
4
  export * from './integrations/chai';
5
5
  export * from './integrations/sinon';
6
- export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks';
@@ -1,8 +1,10 @@
1
1
  import { ChaiPlugin } from '../types';
2
+ import { SnapshotManager } from './manager';
2
3
  export interface SnapshotOptions {
3
4
  rootDir: string;
4
5
  update?: boolean;
5
6
  }
7
+ export declare function getSnapshotManager(): SnapshotManager;
6
8
  export declare function SnapshotPlugin(options: SnapshotOptions): Promise<ChaiPlugin>;
7
9
  declare global {
8
10
  namespace Chai {
@@ -1,8 +1,10 @@
1
1
  import Snap from 'jest-snapshot';
2
- import { afterAll, beforeEach } from '../../../hooks';
3
2
  import { SnapshotManager } from './manager';
4
3
  const { addSerializer } = Snap;
5
4
  let _manager;
5
+ export function getSnapshotManager() {
6
+ return _manager;
7
+ }
6
8
  export async function SnapshotPlugin(options) {
7
9
  const { rootDir } = options;
8
10
  _manager = new SnapshotManager({
@@ -14,18 +16,6 @@ export async function SnapshotPlugin(options) {
14
16
  rootDir,
15
17
  });
16
18
  return function (chai, utils) {
17
- beforeEach((task) => {
18
- var _a;
19
- _manager.setContext({
20
- file: ((_a = task.file) === null || _a === void 0 ? void 0 : _a.filepath) || task.name,
21
- title: task.name,
22
- fullTitle: [task.suite.name, task.name].filter(Boolean).join(' > '),
23
- });
24
- });
25
- afterAll(() => {
26
- _manager.saveSnap();
27
- _manager.report();
28
- });
29
19
  for (const key of ['matchSnapshot', 'toMatchSnapshot']) {
30
20
  utils.addMethod(chai.Assertion.prototype, key, function (message) {
31
21
  const expected = utils.flag(this, 'object');
@@ -1,5 +1,6 @@
1
1
  import type { SnapshotStateType, SnapshotResolver } from 'jest-snapshot';
2
2
  import type { SnapshotStateOptions } from 'jest-snapshot/build/State';
3
+ import { Task } from '../../../types';
3
4
  import { SnapshotSummary } from './utils/types';
4
5
  export interface Context {
5
6
  file: string;
@@ -20,6 +21,7 @@ export declare class SnapshotManager {
20
21
  snapshotResolver?: SnapshotResolver | null;
21
22
  });
22
23
  onFileChanged(): void;
24
+ setTask(task: Task): void;
23
25
  setContext(context: Context): void;
24
26
  assert(received: unknown, message: string): void;
25
27
  saveSnap(): void;
@@ -31,6 +31,14 @@ export class SnapshotManager {
31
31
  this.testFile = this.context.file;
32
32
  this.snapshotState = new SnapshotState(this.snapshotResolver.resolveSnapshotPath(this.testFile), this.snapshotOptions);
33
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
+ }
34
42
  setContext(context) {
35
43
  if (!context.title || !context.file)
36
44
  return;
package/dist/run.js CHANGED
@@ -2,15 +2,19 @@ import fg from 'fast-glob';
2
2
  import { setupChai } from './integrations/chai/setup';
3
3
  import { clearContext, defaultSuite } from './suite';
4
4
  import { context } from './context';
5
- import { afterEachHook, afterFileHook, afterAllHook, afterSuiteHook, beforeEachHook, beforeFileHook, beforeAllHook, beforeSuiteHook } from './hooks';
6
5
  import { DefaultReporter } from './reporters/default';
7
6
  import { defaultIncludes, defaultExcludes } from './constants';
7
+ import { getSnapshotManager } from './integrations/chai/snapshot';
8
+ async function callHook(suite, name, args) {
9
+ await Promise.all(suite.hooks[name].map(fn => fn(...args)));
10
+ }
8
11
  export async function runTask(task, ctx) {
9
- var _a, _b;
12
+ var _a, _b, _c;
10
13
  const { reporter } = ctx;
11
- await ((_a = reporter.onTaskBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, task, ctx));
14
+ (_a = getSnapshotManager()) === null || _a === void 0 ? void 0 : _a.setTask(task);
15
+ await ((_b = reporter.onTaskBegin) === null || _b === void 0 ? void 0 : _b.call(reporter, task, ctx));
12
16
  if (task.mode === 'run') {
13
- await beforeEachHook.fire(task);
17
+ await callHook(task.suite, 'afterEach', [task, task.suite]);
14
18
  try {
15
19
  await task.fn();
16
20
  task.state = 'pass';
@@ -19,9 +23,9 @@ export async function runTask(task, ctx) {
19
23
  task.state = 'fail';
20
24
  task.error = e;
21
25
  }
22
- await afterEachHook.fire(task);
26
+ await callHook(task.suite, 'afterEach', [task, task.suite]);
23
27
  }
24
- await ((_b = reporter.onTaskEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, task, ctx));
28
+ await ((_c = reporter.onTaskEnd) === null || _c === void 0 ? void 0 : _c.call(reporter, task, ctx));
25
29
  }
26
30
  export async function collectFiles(paths) {
27
31
  const files = [];
@@ -78,19 +82,17 @@ export async function runFile(file, ctx) {
78
82
  if (runableSuites.length === 0)
79
83
  return;
80
84
  await ((_a = reporter.onFileBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, file, ctx));
81
- await beforeFileHook.fire(file);
82
85
  // TODO: support toggling parallel or serial
83
86
  await Promise.all(file.suites.map(async (suite) => {
84
87
  var _a, _b;
85
88
  await ((_a = reporter.onSuiteBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, suite, ctx));
86
- await beforeSuiteHook.fire(suite);
89
+ await callHook(suite, 'beforeAll', [suite]);
87
90
  await Promise.all(suite.tasks.map(i => runTask(i, ctx)));
88
91
  // for (const t of suite.tasks)
89
92
  // await runTask(t, ctx)
90
- await afterSuiteHook.fire(suite);
93
+ await callHook(suite, 'afterAll', [suite]);
91
94
  await ((_b = reporter.onSuiteEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, suite, ctx));
92
95
  }));
93
- await afterFileHook.fire(file);
94
96
  await ((_b = reporter.onFileEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, file, ctx));
95
97
  }
96
98
  export async function run(config) {
@@ -123,9 +125,10 @@ export async function run(config) {
123
125
  reporter,
124
126
  };
125
127
  await ((_c = reporter.onCollected) === null || _c === void 0 ? void 0 : _c.call(reporter, ctx));
126
- await beforeAllHook.fire();
127
128
  for (const file of files)
128
129
  await runFile(file, ctx);
129
- await afterAllHook.fire();
130
+ const snapshot = getSnapshotManager();
131
+ snapshot === null || snapshot === void 0 ? void 0 : snapshot.saveSnap();
132
+ snapshot === null || snapshot === void 0 ? void 0 : snapshot.report();
130
133
  await ((_d = reporter.onFinished) === null || _d === void 0 ? void 0 : _d.call(reporter, ctx));
131
134
  }
package/dist/suite.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SuiteCollector, TestFactory, TestFunction } from './types';
1
+ import { SuiteCollector, TestFactory, TestFunction, Suite } from './types';
2
2
  export declare const defaultSuite: SuiteCollector;
3
3
  export declare const test: {
4
4
  (name: string, fn: TestFunction): void;
@@ -19,4 +19,8 @@ export declare const it: {
19
19
  only(name: string, fn: TestFunction): void;
20
20
  todo(name: string): void;
21
21
  };
22
+ export declare const beforeAll: (fn: Suite['hooks']['beforeAll'][0]) => void;
23
+ export declare const afterAll: (fn: Suite['hooks']['afterAll'][0]) => void;
24
+ export declare const beforeEach: (fn: Suite['hooks']['beforeEach'][0]) => void;
25
+ export declare const afterEach: (fn: Suite['hooks']['afterEach'][0]) => void;
22
26
  export declare function clearContext(): void;
package/dist/suite.js CHANGED
@@ -3,20 +3,30 @@ export const defaultSuite = suite('');
3
3
  function getCurrentSuite() {
4
4
  return context.currentSuite || defaultSuite;
5
5
  }
6
- export const test = (name, fn) => getCurrentSuite().test(name, fn);
7
- test.skip = (name, fn) => getCurrentSuite().test.skip(name, fn);
8
- test.only = (name, fn) => getCurrentSuite().test.only(name, fn);
9
- test.todo = (name) => getCurrentSuite().test.todo(name);
10
- function createSuiteCollector(mode, suiteName, factory) {
6
+ function createSuiteCollector(name, factory = () => { }, mode) {
11
7
  const queue = [];
12
8
  const factoryQueue = [];
9
+ const suiteBase = {
10
+ name,
11
+ mode,
12
+ hooks: {
13
+ beforeAll: [],
14
+ afterAll: [],
15
+ beforeEach: [],
16
+ afterEach: [],
17
+ },
18
+ };
13
19
  const collector = {
14
- name: suiteName,
20
+ name,
15
21
  mode,
16
22
  test,
17
23
  collect,
18
24
  clear,
25
+ on: addHook,
19
26
  };
27
+ function addHook(name, ...fn) {
28
+ suiteBase.hooks[name].push(...fn);
29
+ }
20
30
  function collectTask(name, fn, mode) {
21
31
  queue.push({
22
32
  name,
@@ -41,12 +51,8 @@ function createSuiteCollector(mode, suiteName, factory) {
41
51
  if (factory)
42
52
  await factory(test);
43
53
  const tasks = [...factoryQueue, ...queue];
44
- const suite = {
45
- name: collector.name,
46
- mode: collector.mode,
47
- tasks,
48
- file,
49
- };
54
+ const suite = Object.assign(Object.assign({}, suiteBase), { tasks,
55
+ file });
50
56
  tasks.forEach((task) => {
51
57
  task.suite = suite;
52
58
  if (file)
@@ -58,15 +64,25 @@ function createSuiteCollector(mode, suiteName, factory) {
58
64
  context.suites.push(collector);
59
65
  return collector;
60
66
  }
67
+ // apis
68
+ export const test = (name, fn) => getCurrentSuite().test(name, fn);
69
+ test.skip = (name, fn) => getCurrentSuite().test.skip(name, fn);
70
+ test.only = (name, fn) => getCurrentSuite().test.only(name, fn);
71
+ test.todo = (name) => getCurrentSuite().test.todo(name);
61
72
  export function suite(suiteName, factory) {
62
- return createSuiteCollector('run', suiteName, factory);
73
+ return createSuiteCollector(suiteName, factory, 'run');
63
74
  }
64
- suite.skip = (suiteName, factory) => createSuiteCollector('skip', suiteName, factory);
65
- suite.only = (suiteName, factory) => createSuiteCollector('only', suiteName, factory);
66
- suite.todo = (suiteName) => createSuiteCollector('todo', suiteName);
75
+ suite.skip = (suiteName, factory) => createSuiteCollector(suiteName, factory, 'skip');
76
+ suite.only = (suiteName, factory) => createSuiteCollector(suiteName, factory, 'only');
77
+ suite.todo = (suiteName) => createSuiteCollector(suiteName, undefined, 'todo');
67
78
  // alias
68
79
  export const describe = suite;
69
80
  export const it = test;
81
+ // hooks
82
+ export const beforeAll = (fn) => getCurrentSuite().on('beforeAll', fn);
83
+ export const afterAll = (fn) => getCurrentSuite().on('afterAll', fn);
84
+ export const beforeEach = (fn) => getCurrentSuite().on('beforeEach', fn);
85
+ export const afterEach = (fn) => getCurrentSuite().on('afterEach', fn);
70
86
  // utils
71
87
  export function clearContext() {
72
88
  context.suites.length = 0;
package/dist/types.d.ts CHANGED
@@ -41,18 +41,26 @@ export interface TestCollector {
41
41
  skip: (name: string, fn: TestFunction) => void;
42
42
  todo: (name: string) => void;
43
43
  }
44
+ export declare type HookListener<T extends any[]> = (...args: T) => Awaitable<void>;
44
45
  export interface Suite {
45
46
  name: string;
46
47
  mode: RunMode;
47
48
  tasks: Task[];
48
49
  file?: File;
50
+ hooks: {
51
+ beforeAll: HookListener<[Suite]>[];
52
+ afterAll: HookListener<[Suite]>[];
53
+ beforeEach: HookListener<[Task, Suite]>[];
54
+ afterEach: HookListener<[Task, Suite]>[];
55
+ };
49
56
  }
50
57
  export interface SuiteCollector {
51
- name: string;
52
- mode: RunMode;
58
+ readonly name: string;
59
+ readonly mode: RunMode;
53
60
  test: TestCollector;
54
61
  collect: (file?: File) => Promise<Suite>;
55
62
  clear: () => void;
63
+ on: <T extends keyof Suite['hooks']>(name: T, ...fn: Suite['hooks'][T]) => void;
56
64
  }
57
65
  export declare type TestFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable<void>;
58
66
  export interface File {
package/global.d.ts CHANGED
@@ -3,19 +3,16 @@ declare global {
3
3
  const test: typeof import('vitest')['test']
4
4
  const describe: typeof import('vitest')['describe']
5
5
  const it: typeof import('vitest')['it']
6
+ const chai: typeof import('vitest')['chai']
6
7
  const expect: typeof import('vitest')['expect']
7
8
  const assert: typeof import('vitest')['assert']
9
+ const sinon: typeof import('vitest')['sinon']
8
10
  const spy: typeof import('vitest')['spy']
9
11
  const mock: typeof import('vitest')['mock']
10
12
  const stub: typeof import('vitest')['stub']
11
- const sinon: typeof import('vitest')['sinon']
12
13
  const beforeAll: typeof import('vitest')['beforeAll']
13
14
  const afterAll: typeof import('vitest')['afterAll']
14
15
  const beforeEach: typeof import('vitest')['beforeEach']
15
16
  const afterEach: typeof import('vitest')['afterEach']
16
- const beforeFile: typeof import('vitest')['beforeFile']
17
- const afterFile: typeof import('vitest')['afterFile']
18
- const beforeSuite: typeof import('vitest')['beforeSuite']
19
- const afterSuite: typeof import('vitest')['afterSuite']
20
17
  }
21
18
  export {}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/antfu/vitest#readme",
@@ -33,16 +33,6 @@
33
33
  "bin",
34
34
  "*.d.ts"
35
35
  ],
36
- "scripts": {
37
- "prepare": "esmo scripts/generate-types.ts",
38
- "build": "rimraf dist && tsc -p src/tsconfig.json",
39
- "lint": "eslint \"{src,test}/**/*.ts\"",
40
- "prepublishOnly": "nr build",
41
- "release": "bumpp --commit --push --tag && pnpm publish",
42
- "test": "node bin/vitest.mjs --dev",
43
- "test:update": "nr test -u",
44
- "watch": "tsc -p src/tsconfig.json --watch"
45
- },
46
36
  "dependencies": {
47
37
  "@jest/test-result": "^27.4.2",
48
38
  "@types/chai": "^4.2.22",
@@ -74,5 +64,13 @@
74
64
  "rimraf": "^3.0.2",
75
65
  "typescript": "^4.5.2",
76
66
  "vite": "^2.6.14"
67
+ },
68
+ "scripts": {
69
+ "build": "rimraf dist && tsc -p src/tsconfig.json",
70
+ "lint": "eslint \"{src,test}/**/*.ts\"",
71
+ "release": "bumpp --commit --push --tag && pnpm publish",
72
+ "test": "node bin/vitest.mjs --dev",
73
+ "test:update": "nr test -u",
74
+ "watch": "tsc -p src/tsconfig.json --watch"
77
75
  }
78
- }
76
+ }
package/dist/hooks.d.ts DELETED
@@ -1,49 +0,0 @@
1
- import { File, Suite, Task } from './types';
2
- export declare const beforeAllHook: {
3
- on(fn: (...args: any[]) => void | Promise<void>): void;
4
- fire(...args: any[]): Promise<void>;
5
- clear(): void;
6
- };
7
- export declare const afterAllHook: {
8
- on(fn: (...args: any[]) => void | Promise<void>): void;
9
- fire(...args: any[]): Promise<void>;
10
- clear(): void;
11
- };
12
- export declare const beforeEachHook: {
13
- on(fn: (args_0: Task) => void | Promise<void>): void;
14
- fire(args_0: Task): Promise<void>;
15
- clear(): void;
16
- };
17
- export declare const afterEachHook: {
18
- on(fn: (args_0: Task) => void | Promise<void>): void;
19
- fire(args_0: Task): Promise<void>;
20
- clear(): void;
21
- };
22
- export declare const beforeFileHook: {
23
- on(fn: (args_0: File) => void | Promise<void>): void;
24
- fire(args_0: File): Promise<void>;
25
- clear(): void;
26
- };
27
- export declare const afterFileHook: {
28
- on(fn: (args_0: File) => void | Promise<void>): void;
29
- fire(args_0: File): Promise<void>;
30
- clear(): void;
31
- };
32
- export declare const beforeSuiteHook: {
33
- on(fn: (args_0: Suite) => void | Promise<void>): void;
34
- fire(args_0: Suite): Promise<void>;
35
- clear(): void;
36
- };
37
- export declare const afterSuiteHook: {
38
- on(fn: (args_0: Suite) => void | Promise<void>): void;
39
- fire(args_0: Suite): Promise<void>;
40
- clear(): void;
41
- };
42
- export declare const beforeAll: (fn: (...args: any[]) => void | Promise<void>) => void;
43
- export declare const afterAll: (fn: (...args: any[]) => void | Promise<void>) => void;
44
- export declare const beforeEach: (fn: (args_0: Task) => void | Promise<void>) => void;
45
- export declare const afterEach: (fn: (args_0: Task) => void | Promise<void>) => void;
46
- export declare const beforeFile: (fn: (args_0: File) => void | Promise<void>) => void;
47
- export declare const afterFile: (fn: (args_0: File) => void | Promise<void>) => void;
48
- export declare const beforeSuite: (fn: (args_0: Suite) => void | Promise<void>) => void;
49
- export declare const afterSuite: (fn: (args_0: Suite) => void | Promise<void>) => void;
package/dist/hooks.js DELETED
@@ -1,17 +0,0 @@
1
- import { createHook } from './utils/hook';
2
- export const beforeAllHook = createHook();
3
- export const afterAllHook = createHook();
4
- export const beforeEachHook = createHook();
5
- export const afterEachHook = createHook();
6
- export const beforeFileHook = createHook();
7
- export const afterFileHook = createHook();
8
- export const beforeSuiteHook = createHook();
9
- export const afterSuiteHook = createHook();
10
- export const beforeAll = beforeAllHook.on;
11
- export const afterAll = afterAllHook.on;
12
- export const beforeEach = beforeEachHook.on;
13
- export const afterEach = afterEachHook.on;
14
- export const beforeFile = beforeFileHook.on;
15
- export const afterFile = afterFileHook.on;
16
- export const beforeSuite = beforeSuiteHook.on;
17
- export const afterSuite = afterSuiteHook.on;