vitest 0.0.6 → 0.0.10

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/README.md CHANGED
@@ -6,11 +6,14 @@ A blazing fast test runner powered by Vite.
6
6
 
7
7
  ## Features
8
8
 
9
- - Vite's transformer, resolver, and plugin system. Powered by [vite-node](https://github.com/antfu/vite-node).
10
- - Jest Snapshot.
11
- - Chai for assertions.
12
- - Async suite / test.
13
- - ESM friendly, top level await.
9
+ - [Vite](https://vitejs.dev/)'s config, transformers, resolvers, and plugins. Powered by [vite-node](https://github.com/antfu/vite-node)
10
+ - [Jest Snapshot](https://jestjs.io/docs/snapshot-testing)
11
+ - [Chai](https://www.chaijs.com/) for assertions
12
+ - [Sinon](https://sinonjs.org/) for mocking
13
+ - Async suite / test, top level await
14
+ - ESM friendly
15
+ - Out-of-box TypeScript support
16
+ - Suite and Test filtering (skip, only, todo)
14
17
 
15
18
  ```ts
16
19
  import { it, describe, expect, assert } from 'vitest'
@@ -34,12 +37,74 @@ describe('suite name', () => {
34
37
  $ npx vitest
35
38
  ```
36
39
 
40
+ ## Filtering
41
+
42
+ ### Skipping suites and tasks
43
+
44
+ Use `.skip` to avoid running certain suites or tests
45
+
46
+ ```ts
47
+ describe.skip('skipped suite', () => {
48
+ it('task', () => {
49
+ // Suite skipped, no error
50
+ assert.equal(Math.sqrt(4), 3)
51
+ })
52
+ })
53
+
54
+ describe('suite', () => {
55
+ it.skip('skipped task', () => {
56
+ // Task skipped, no error
57
+ assert.equal(Math.sqrt(4), 3)
58
+ })
59
+ })
60
+ ```
61
+
62
+ ### Selecting suites and tests to run
63
+
64
+ Use `.only` to only run certain suites or tests
65
+
66
+ ```ts
67
+ // Only this suite (and others marked with only) are run
68
+ describe.only('suite', () => {
69
+ it('task', () => {
70
+ assert.equal(Math.sqrt(4), 3)
71
+ })
72
+ })
73
+
74
+ describe('another suite', () => {
75
+ it('skipped task', () => {
76
+ // Task skipped, as tests are running in Only mode
77
+ assert.equal(Math.sqrt(4), 3)
78
+ })
79
+
80
+ it.only('task', () => {
81
+ // Only this task (and others marked with only) are run
82
+ assert.equal(Math.sqrt(4), 2)
83
+ })
84
+ })
85
+ ```
86
+
87
+ ### Unimplemented suites and tests
88
+
89
+ Use `.todo` to stub suites and tests that should be implemented
90
+
91
+ ```ts
92
+ // An entry will be shown in the report for this suite
93
+ describe.todo('unimplemented suite')
94
+
95
+ // An entry will be shown in the report for this task
96
+ describe('suite', () => {
97
+ it.todo('unimplemented task')
98
+ })
99
+ ```
100
+
37
101
  ## TODO
38
102
 
39
- - [ ] Reporter & Better output
103
+ - [x] Reporter & Better output
104
+ - [x] Task filter
105
+ - [x] Mock
106
+ - [ ] Parallel Executing
40
107
  - [ ] CLI Help
41
- - [ ] Task filter
42
- - [ ] Mock
43
108
  - [ ] JSDom
44
109
  - [ ] Watch
45
110
  - [ ] Coverage
@@ -0,0 +1,2 @@
1
+ export declare const defaultIncludes: string[];
2
+ export declare const defaultExcludes: string[];
@@ -0,0 +1,2 @@
1
+ export const defaultIncludes = ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'];
2
+ export const defaultExcludes = ['**/node_modules/**', '**/dist/**'];
File without changes
@@ -0,0 +1 @@
1
+ "use strict";
package/dist/hooks.d.ts CHANGED
@@ -1,11 +1,10 @@
1
- import { Suite, Task } from './types';
2
- import { TaskResult } from '.';
3
- export declare const beforeHook: {
1
+ import { File, Suite, Task } from './types';
2
+ export declare const beforeAllHook: {
4
3
  on(fn: (...args: any[]) => void | Promise<void>): void;
5
4
  fire(...args: any[]): Promise<void>;
6
5
  clear(): void;
7
6
  };
8
- export declare const afterHook: {
7
+ export declare const afterAllHook: {
9
8
  on(fn: (...args: any[]) => void | Promise<void>): void;
10
9
  fire(...args: any[]): Promise<void>;
11
10
  clear(): void;
@@ -16,18 +15,18 @@ export declare const beforeEachHook: {
16
15
  clear(): void;
17
16
  };
18
17
  export declare const afterEachHook: {
19
- on(fn: (args_0: Task, args_1: TaskResult) => void | Promise<void>): void;
20
- fire(args_0: Task, args_1: TaskResult): Promise<void>;
18
+ on(fn: (args_0: Task) => void | Promise<void>): void;
19
+ fire(args_0: Task): Promise<void>;
21
20
  clear(): void;
22
21
  };
23
22
  export declare const beforeFileHook: {
24
- on(fn: (args_0: string) => void | Promise<void>): void;
25
- fire(args_0: string): Promise<void>;
23
+ on(fn: (args_0: File) => void | Promise<void>): void;
24
+ fire(args_0: File): Promise<void>;
26
25
  clear(): void;
27
26
  };
28
27
  export declare const afterFileHook: {
29
- on(fn: (args_0: string) => void | Promise<void>): void;
30
- fire(args_0: string): Promise<void>;
28
+ on(fn: (args_0: File) => void | Promise<void>): void;
29
+ fire(args_0: File): Promise<void>;
31
30
  clear(): void;
32
31
  };
33
32
  export declare const beforeSuiteHook: {
@@ -40,11 +39,11 @@ export declare const afterSuiteHook: {
40
39
  fire(args_0: Suite): Promise<void>;
41
40
  clear(): void;
42
41
  };
43
- export declare const before: (fn: (...args: any[]) => void | Promise<void>) => void;
44
- export declare const after: (fn: (...args: any[]) => void | Promise<void>) => void;
42
+ export declare const beforeAll: (fn: (...args: any[]) => void | Promise<void>) => void;
43
+ export declare const afterAll: (fn: (...args: any[]) => void | Promise<void>) => void;
45
44
  export declare const beforeEach: (fn: (args_0: Task) => void | Promise<void>) => void;
46
- export declare const afterEach: (fn: (args_0: Task, args_1: TaskResult) => void | Promise<void>) => void;
47
- export declare const beforeFile: (fn: (args_0: string) => void | Promise<void>) => void;
48
- export declare const afterFile: (fn: (args_0: string) => 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;
49
48
  export declare const beforeSuite: (fn: (args_0: Suite) => void | Promise<void>) => void;
50
49
  export declare const afterSuite: (fn: (args_0: Suite) => void | Promise<void>) => void;
package/dist/hooks.js CHANGED
@@ -1,14 +1,14 @@
1
1
  import { createHook } from './utils/hook';
2
- export const beforeHook = createHook();
3
- export const afterHook = createHook();
2
+ export const beforeAllHook = createHook();
3
+ export const afterAllHook = createHook();
4
4
  export const beforeEachHook = createHook();
5
5
  export const afterEachHook = createHook();
6
6
  export const beforeFileHook = createHook();
7
7
  export const afterFileHook = createHook();
8
8
  export const beforeSuiteHook = createHook();
9
9
  export const afterSuiteHook = createHook();
10
- export const before = beforeHook.on;
11
- export const after = afterHook.on;
10
+ export const beforeAll = beforeAllHook.on;
11
+ export const afterAll = afterAllHook.on;
12
12
  export const beforeEach = beforeEachHook.on;
13
13
  export const afterEach = afterEachHook.on;
14
14
  export const beforeFile = beforeFileHook.on;
package/dist/index.d.ts CHANGED
@@ -1,4 +1,8 @@
1
+ import sinon from 'sinon';
1
2
  export * from './types';
2
3
  export * from './suite';
3
4
  export * from './config';
4
5
  export * from './chai';
6
+ export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks';
7
+ export { sinon };
8
+ export declare const mock: sinon.SinonMockStatic, spy: sinon.SinonSpyStatic;
package/dist/index.js CHANGED
@@ -1,4 +1,8 @@
1
+ import sinon from 'sinon';
1
2
  export * from './types';
2
3
  export * from './suite';
3
4
  export * from './config';
4
5
  export * from './chai';
6
+ export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks';
7
+ export { sinon };
8
+ export const { mock, spy } = sinon;
@@ -0,0 +1,19 @@
1
+ import { File, Reporter, RunnerContext, Suite, Task } from '../types';
2
+ export declare class DefaultReporter implements Reporter {
3
+ indent: number;
4
+ start: number;
5
+ end: number;
6
+ onStart(): void;
7
+ onCollected(): void;
8
+ onFinished({ files }: RunnerContext): void;
9
+ onSuiteBegin(suite: Suite): void;
10
+ onSuiteEnd(suite: Suite): void;
11
+ onFileBegin(file: File): void;
12
+ onFileEnd(): void;
13
+ onTaskBegin(task: Task): void;
14
+ onTaskEnd(task: Task): void;
15
+ private getIndent;
16
+ private log;
17
+ private error;
18
+ onSnapshotUpdate(): void;
19
+ }
@@ -0,0 +1,95 @@
1
+ import { relative } from 'path';
2
+ import { performance } from 'perf_hooks';
3
+ import c from 'picocolors';
4
+ import ora from 'ora';
5
+ const DOT = '· ';
6
+ export class DefaultReporter {
7
+ constructor() {
8
+ this.indent = 0;
9
+ this.start = 0;
10
+ this.end = 0;
11
+ }
12
+ onStart() {
13
+ this.indent = 0;
14
+ }
15
+ onCollected() {
16
+ this.start = performance.now();
17
+ }
18
+ onFinished({ files }) {
19
+ this.end = performance.now();
20
+ const tasks = files.reduce((acc, file) => acc.concat(file.suites.flatMap(i => i.tasks)), []);
21
+ const passed = tasks.filter(i => i.status === 'pass');
22
+ const failed = tasks.filter(i => i.status === 'fail');
23
+ const skipped = tasks.filter(i => i.status === 'skip');
24
+ const todo = tasks.filter(i => i.status === 'todo');
25
+ this.indent = 0;
26
+ this.log(c.green(`Passed ${passed.length} / ${tasks.length}`));
27
+ if (skipped.length)
28
+ this.log(c.yellow(`Skipped ${skipped.length}`));
29
+ if (todo.length)
30
+ this.log(c.dim(`Todo ${todo.length}`));
31
+ if (failed.length)
32
+ this.log(c.red(`Failed ${failed.length} / ${tasks.length}`));
33
+ this.log(`Time ${(this.end - this.start).toFixed(2)}ms`);
34
+ }
35
+ onSuiteBegin(suite) {
36
+ if (suite.name) {
37
+ this.indent += 1;
38
+ const name = DOT + suite.name;
39
+ if (suite.mode === 'skip')
40
+ this.log(c.dim(c.yellow(`${name} (skipped)`)));
41
+ else if (suite.mode === 'todo')
42
+ this.log(c.dim(`${name} (todo)`));
43
+ else
44
+ this.log(name);
45
+ }
46
+ }
47
+ onSuiteEnd(suite) {
48
+ if (suite.name)
49
+ this.indent -= 1;
50
+ }
51
+ onFileBegin(file) {
52
+ this.log(`- ${relative(process.cwd(), file.filepath)} ${c.dim(`(${file.suites.flatMap(i => i.tasks).length} tests)`)}`);
53
+ }
54
+ onFileEnd() {
55
+ this.log();
56
+ }
57
+ onTaskBegin(task) {
58
+ this.indent += 1;
59
+ // @ts-expect-error
60
+ task.__ora = ora({ text: task.name, prefixText: this.getIndent().slice(1), spinner: 'arc' }).start();
61
+ }
62
+ onTaskEnd(task) {
63
+ var _a;
64
+ // @ts-expect-error
65
+ (_a = task.__ora) === null || _a === void 0 ? void 0 : _a.stop();
66
+ if (task.status === 'pass') {
67
+ this.log(`${c.green(`✔ ${task.name}`)}`);
68
+ }
69
+ else if (task.status === 'skip') {
70
+ this.log(c.dim(c.yellow(`${DOT + task.name} (skipped)`)));
71
+ }
72
+ else if (task.status === 'todo') {
73
+ this.log(c.dim(`${DOT + task.name} (todo)`));
74
+ }
75
+ else {
76
+ this.error(`${c.red(`⤫ ${c.inverse(c.red(' FAIL '))} ${task.name}`)}`);
77
+ this.error(String(task.error), 1);
78
+ process.exitCode = 1;
79
+ }
80
+ this.indent -= 1;
81
+ }
82
+ getIndent(offest = 0) {
83
+ return ' '.repeat((this.indent + offest) * 2);
84
+ }
85
+ log(msg = '', indentOffset = 0) {
86
+ // eslint-disable-next-line no-console
87
+ console.log(`${this.getIndent(indentOffset)}${msg}`);
88
+ }
89
+ error(msg = '', indentOffset = 0) {
90
+ // eslint-disable-next-line no-console
91
+ console.error(c.red(`${this.getIndent(indentOffset)}${msg}`));
92
+ }
93
+ onSnapshotUpdate() {
94
+ }
95
+ }
package/dist/run.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { File, Options, Task, TaskResult } from './types';
2
- export declare function runTasks(tasks: Task[]): Promise<TaskResult[]>;
3
- export declare function parseFile(filepath: string): Promise<File>;
4
- export declare function runFile(filepath: string): Promise<void>;
1
+ import { File, Options, Task, RunnerContext } from './types';
2
+ export declare function runTask(task: Task, ctx: RunnerContext): Promise<void>;
3
+ export declare function collectFiles(files: string[]): Promise<File[]>;
4
+ export declare function runFile(file: File, ctx: RunnerContext): Promise<void>;
5
5
  export declare function run(options?: Options): Promise<void>;
package/dist/run.js CHANGED
@@ -1,94 +1,108 @@
1
- import { relative } from 'path';
2
- import c from 'picocolors';
3
1
  import chai from 'chai';
4
2
  import fg from 'fast-glob';
3
+ import SinonChai from 'sinon-chai';
5
4
  import { clearContext, defaultSuite } from './suite';
6
5
  import { context } from './context';
7
- import { afterEachHook, afterFileHook, afterHook, afterSuiteHook, beforeEachHook, beforeFileHook, beforeHook, beforeSuiteHook } from './hooks';
8
- import { SnapshotPlugin } from './snapshot/index';
9
- export async function runTasks(tasks) {
10
- const results = [];
11
- for (const task of tasks) {
12
- const result = { task };
13
- await beforeEachHook.fire(task);
6
+ import { afterEachHook, afterFileHook, afterAllHook, afterSuiteHook, beforeEachHook, beforeFileHook, beforeAllHook, beforeSuiteHook } from './hooks';
7
+ import { SnapshotPlugin } from './snapshot';
8
+ import { DefaultReporter } from './reporters/default';
9
+ import { defaultIncludes, defaultExcludes } from './constants';
10
+ export async function runTask(task, ctx) {
11
+ var _a, _b;
12
+ const { reporter } = ctx;
13
+ await ((_a = reporter.onTaskBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, task, ctx));
14
+ await beforeEachHook.fire(task);
15
+ if (task.suite.mode === 'skip' || task.mode === 'skip') {
16
+ task.status = 'skip';
17
+ }
18
+ else if (task.suite.mode === 'todo' || task.mode === 'todo') {
19
+ task.status = 'todo';
20
+ }
21
+ else {
14
22
  try {
15
23
  await task.fn();
24
+ task.status = 'pass';
16
25
  }
17
26
  catch (e) {
18
- result.error = e;
27
+ task.status = 'fail';
28
+ task.error = e;
19
29
  }
20
- results.push(result);
21
- await afterEachHook.fire(task, result);
22
30
  }
23
- return results;
31
+ await afterEachHook.fire(task);
32
+ await ((_b = reporter.onTaskEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, task, ctx));
24
33
  }
25
- // TODO: REPORTER
26
- const { log } = console;
27
- export async function parseFile(filepath) {
28
- clearContext();
29
- await import(filepath);
30
- const suites = [defaultSuite, ...context.suites];
31
- const tasks = await Promise.all(suites.map(async (suite) => {
32
- await beforeSuiteHook.fire(suite);
33
- context.currentSuite = suite;
34
- return [suite, await suite.collect()];
35
- }));
36
- const file = {
37
- filepath,
38
- suites,
39
- tasks,
40
- };
41
- file.tasks.forEach(([, tasks]) => tasks.forEach(task => task.file = file));
42
- return file;
43
- }
44
- export async function runFile(filepath) {
45
- await beforeFileHook.fire(filepath);
46
- const file = await parseFile(filepath);
47
- for (const [suite, tasks] of file.tasks) {
48
- let indent = 1;
49
- if (suite.name) {
50
- log(' '.repeat(indent * 2) + suite.name);
51
- indent += 1;
52
- }
53
- const result = await runTasks(tasks);
54
- for (const r of result) {
55
- if (r.error === undefined) {
56
- log(`${' '.repeat(indent * 2)}${c.inverse(c.green(' PASS '))} ${c.green(r.task.name)}`);
57
- }
58
- else {
59
- console.error(`${' '.repeat(indent * 2)}${c.inverse(c.red(' FAIL '))} ${c.red(r.task.name)}`);
60
- console.error(' '.repeat((indent + 2) * 2) + c.red(String(r.error)));
61
- process.exitCode = 1;
62
- }
34
+ export async function collectFiles(files) {
35
+ const result = [];
36
+ for (const filepath of files) {
37
+ clearContext();
38
+ await import(filepath);
39
+ const collectors = [defaultSuite, ...context.suites];
40
+ const suites = [];
41
+ const file = {
42
+ filepath,
43
+ suites: [],
44
+ };
45
+ for (const c of collectors) {
46
+ context.currentSuite = c;
47
+ suites.push(await c.collect(file));
63
48
  }
64
- if (suite.name)
65
- indent -= 1;
49
+ file.suites = suites;
50
+ result.push(file);
51
+ }
52
+ return result;
53
+ }
54
+ export async function runFile(file, ctx) {
55
+ var _a, _b, _c, _d;
56
+ const { reporter } = ctx;
57
+ await ((_a = reporter.onFileBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, file, ctx));
58
+ await beforeFileHook.fire(file);
59
+ for (const suite of file.suites) {
60
+ await ((_b = reporter.onSuiteBegin) === null || _b === void 0 ? void 0 : _b.call(reporter, suite, ctx));
61
+ await beforeSuiteHook.fire(suite);
62
+ for (const t of suite.tasks)
63
+ await runTask(t, ctx);
66
64
  await afterSuiteHook.fire(suite);
65
+ await ((_c = reporter.onSuiteEnd) === null || _c === void 0 ? void 0 : _c.call(reporter, suite, ctx));
67
66
  }
68
- await afterFileHook.fire(filepath);
67
+ await afterFileHook.fire(file);
68
+ await ((_d = reporter.onFileEnd) === null || _d === void 0 ? void 0 : _d.call(reporter, file, ctx));
69
69
  }
70
70
  export async function run(options = {}) {
71
+ var _a, _b, _c;
71
72
  const { rootDir = process.cwd() } = options;
72
- chai.use(SnapshotPlugin({
73
+ // setup chai
74
+ chai.use(await SnapshotPlugin({
73
75
  rootDir,
74
76
  update: options.updateSnapshot,
75
77
  }));
76
- const files = await fg(options.includes || ['**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], {
78
+ chai.use(SinonChai);
79
+ // collect files
80
+ const paths = await fg(options.includes || defaultIncludes, {
77
81
  absolute: true,
78
82
  cwd: options.rootDir,
79
- ignore: options.excludes || ['**/node_modules/**', '**/dist/**'],
83
+ ignore: options.excludes || defaultExcludes,
80
84
  });
81
- if (!files.length) {
85
+ if (!paths.length) {
82
86
  console.error('No test files found');
83
87
  process.exitCode = 1;
84
88
  return;
85
89
  }
86
- await beforeHook.fire();
87
- for (const file of files) {
88
- log(`${relative(process.cwd(), file)}`);
89
- await runFile(file);
90
- log();
91
- }
92
- await afterHook.fire();
93
- log();
90
+ const reporter = new DefaultReporter();
91
+ await ((_a = reporter.onStart) === null || _a === void 0 ? void 0 : _a.call(reporter, options));
92
+ const files = await collectFiles(paths);
93
+ const ctx = {
94
+ files,
95
+ mode: isOnlyMode(files) ? 'only' : 'all',
96
+ userOptions: options,
97
+ reporter,
98
+ };
99
+ await ((_b = reporter.onCollected) === null || _b === void 0 ? void 0 : _b.call(reporter, ctx));
100
+ await beforeAllHook.fire();
101
+ for (const file of files)
102
+ await runFile(file, ctx);
103
+ await afterAllHook.fire();
104
+ await ((_c = reporter.onFinished) === null || _c === void 0 ? void 0 : _c.call(reporter, ctx));
105
+ }
106
+ function isOnlyMode(files) {
107
+ return !!files.find(file => file.suites.find(suite => suite.mode === 'only' || suite.tasks.find(t => t.mode === 'only')));
94
108
  }
@@ -5,5 +5,5 @@ export interface SnapshotOptions {
5
5
  rootDir: string;
6
6
  update?: boolean;
7
7
  }
8
- export declare function SnapshotPlugin(options: SnapshotOptions): ChaiPlugin;
8
+ export declare function SnapshotPlugin(options: SnapshotOptions): Promise<ChaiPlugin>;
9
9
  export {};
@@ -1,21 +1,19 @@
1
1
  import Snap from 'jest-snapshot';
2
- import { after, before, beforeEach } from '../hooks';
2
+ import { afterAll, beforeEach } from '../hooks';
3
3
  import { SnapshotManager } from './manager';
4
4
  const { addSerializer } = Snap;
5
5
  let _manager;
6
- export function SnapshotPlugin(options) {
6
+ export async function SnapshotPlugin(options) {
7
7
  const { rootDir } = options;
8
8
  _manager = new SnapshotManager({
9
9
  rootDir,
10
10
  update: options.update,
11
11
  });
12
+ _manager.snapshotResolver = await Snap.buildSnapshotResolver({
13
+ transform: [],
14
+ rootDir,
15
+ });
12
16
  return function (chai, utils) {
13
- before(async () => {
14
- _manager.snapshotResolver = await Snap.buildSnapshotResolver({
15
- transform: [],
16
- rootDir,
17
- });
18
- });
19
17
  beforeEach((task) => {
20
18
  var _a;
21
19
  _manager.setContext({
@@ -24,7 +22,7 @@ export function SnapshotPlugin(options) {
24
22
  fullTitle: [task.suite.name, task.name].filter(Boolean).join(' > '),
25
23
  });
26
24
  });
27
- after(() => {
25
+ afterAll(() => {
28
26
  _manager.saveSnap();
29
27
  _manager.report();
30
28
  });
package/dist/suite.d.ts CHANGED
@@ -1,7 +1,22 @@
1
- import { Suite } from './types';
2
- export declare const defaultSuite: Suite;
3
- export declare const test: (name: string, fn: () => Promise<void> | void) => void;
4
- export declare function clearContext(): void;
5
- export declare function suite(suiteName: string, factory?: (test: Suite['test']) => Promise<void> | void): Suite;
1
+ import { SuiteCollector, TestFactory, TestFunction } from './types';
2
+ export declare const defaultSuite: SuiteCollector;
3
+ export declare const test: {
4
+ (name: string, fn: TestFunction): void;
5
+ skip(name: string, fn: TestFunction): void;
6
+ only(name: string, fn: TestFunction): void;
7
+ todo(name: string): void;
8
+ };
9
+ export declare function suite(suiteName: string, factory?: TestFactory): SuiteCollector;
10
+ export declare namespace suite {
11
+ var skip: (suiteName: string, factory?: TestFactory | undefined) => SuiteCollector;
12
+ var only: (suiteName: string, factory?: TestFactory | undefined) => SuiteCollector;
13
+ var todo: (suiteName: string) => SuiteCollector;
14
+ }
6
15
  export declare const describe: typeof suite;
7
- export declare const it: (name: string, fn: () => Promise<void> | void) => void;
16
+ export declare const it: {
17
+ (name: string, fn: TestFunction): void;
18
+ skip(name: string, fn: TestFunction): void;
19
+ only(name: string, fn: TestFunction): void;
20
+ todo(name: string): void;
21
+ };
22
+ export declare function clearContext(): void;
package/dist/suite.js CHANGED
@@ -1,41 +1,75 @@
1
1
  import { context } from './context';
2
2
  export const defaultSuite = suite('');
3
- export const test = (name, fn) => (context.currentSuite || defaultSuite).test(name, fn);
4
- export function clearContext() {
5
- context.suites.length = 0;
6
- defaultSuite.clear();
3
+ function getCurrentSuite() {
4
+ return context.currentSuite || defaultSuite;
7
5
  }
8
- export function suite(suiteName, factory) {
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) {
9
11
  const queue = [];
10
12
  const factoryQueue = [];
11
- const suite = {
13
+ const collector = {
12
14
  name: suiteName,
15
+ mode,
13
16
  test,
14
17
  collect,
15
18
  clear,
16
19
  };
17
- function test(name, fn) {
18
- const task = {
19
- suite,
20
+ function collectTask(name, fn, mode) {
21
+ queue.push({
20
22
  name,
23
+ mode,
24
+ suite: {},
25
+ status: 'init',
21
26
  fn,
22
- };
23
- queue.push(task);
27
+ });
28
+ }
29
+ function test(name, fn) {
30
+ collectTask(name, fn, mode);
24
31
  }
32
+ test.skip = (name, fn) => collectTask(name, fn, 'skip');
33
+ test.only = (name, fn) => collectTask(name, fn, 'only');
34
+ test.todo = (name) => collectTask(name, () => { }, 'todo');
25
35
  function clear() {
26
36
  queue.length = 0;
27
37
  factoryQueue.length = 0;
28
38
  }
29
- async function collect() {
39
+ async function collect(file) {
30
40
  factoryQueue.length = 0;
31
41
  if (factory)
32
42
  await factory(test);
33
- return [...factoryQueue, ...queue];
43
+ const tasks = [...factoryQueue, ...queue];
44
+ const suite = {
45
+ name: collector.name,
46
+ mode: collector.mode,
47
+ tasks,
48
+ file,
49
+ };
50
+ tasks.forEach((task) => {
51
+ task.suite = suite;
52
+ if (file)
53
+ task.file = file;
54
+ });
55
+ return suite;
34
56
  }
35
- context.currentSuite = suite;
36
- context.suites.push(suite);
37
- return suite;
57
+ context.currentSuite = collector;
58
+ context.suites.push(collector);
59
+ return collector;
60
+ }
61
+ export function suite(suiteName, factory) {
62
+ return createSuiteCollector('run', suiteName, factory);
38
63
  }
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);
39
67
  // alias
40
68
  export const describe = suite;
41
69
  export const it = test;
70
+ // utils
71
+ export function clearContext() {
72
+ context.suites.length = 0;
73
+ defaultSuite.clear();
74
+ context.currentSuite = defaultSuite;
75
+ }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export declare type Awaitable<T> = Promise<T> | T;
1
2
  export interface UserOptions {
2
3
  includes?: string[];
3
4
  excludes?: string[];
@@ -6,28 +7,61 @@ export interface Options extends UserOptions {
6
7
  rootDir?: string;
7
8
  updateSnapshot?: boolean;
8
9
  }
10
+ export declare type RunMode = 'run' | 'skip' | 'only' | 'todo';
11
+ export declare type TaskStatus = 'init' | 'pass' | 'fail' | 'skip' | 'todo';
9
12
  export interface Task {
10
13
  name: string;
14
+ mode: RunMode;
11
15
  suite: Suite;
12
- fn: () => Promise<void> | void;
16
+ fn: () => Awaitable<void>;
13
17
  file?: File;
14
- }
15
- export interface TaskResult {
16
- task: Task;
18
+ status: TaskStatus;
17
19
  error?: unknown;
18
20
  }
21
+ export declare type TestFunction = () => Awaitable<void>;
22
+ export interface TestCollector {
23
+ (name: string, fn: TestFunction): void;
24
+ only: (name: string, fn: TestFunction) => void;
25
+ skip: (name: string, fn: TestFunction) => void;
26
+ todo: (name: string) => void;
27
+ }
19
28
  export interface Suite {
20
29
  name: string;
21
- test: (name: string, fn: () => Promise<void> | void) => void;
22
- collect: () => Promise<Task[]>;
30
+ mode: RunMode;
31
+ tasks: Task[];
32
+ file?: File;
33
+ }
34
+ export interface SuiteCollector {
35
+ name: string;
36
+ mode: RunMode;
37
+ test: TestCollector;
38
+ collect: (file?: File) => Promise<Suite>;
23
39
  clear: () => void;
24
40
  }
41
+ export declare type TestFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable<void>;
25
42
  export interface File {
26
43
  filepath: string;
27
44
  suites: Suite[];
28
- tasks: [Suite, Task[]][];
45
+ }
46
+ export interface RunnerContext {
47
+ files: File[];
48
+ mode: 'all' | 'only';
49
+ userOptions: Options;
50
+ reporter: Reporter;
29
51
  }
30
52
  export interface GlobalContext {
31
- suites: Suite[];
32
- currentSuite: Suite | null;
53
+ suites: SuiteCollector[];
54
+ currentSuite: SuiteCollector | null;
55
+ }
56
+ export interface Reporter {
57
+ onStart: (userOptions: Options) => Awaitable<void>;
58
+ onCollected: (ctx: RunnerContext) => Awaitable<void>;
59
+ onFinished: (ctx: RunnerContext) => Awaitable<void>;
60
+ onSuiteBegin: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
61
+ onSuiteEnd: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
62
+ onFileBegin: (file: File, ctx: RunnerContext) => Awaitable<void>;
63
+ onFileEnd: (file: File, ctx: RunnerContext) => Awaitable<void>;
64
+ onTaskBegin: (task: Task, ctx: RunnerContext) => Awaitable<void>;
65
+ onTaskEnd: (task: Task, ctx: RunnerContext) => Awaitable<void>;
66
+ onSnapshotUpdate: () => Awaitable<void>;
33
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.6",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "keywords": [],
@@ -32,20 +32,12 @@
32
32
  "bin": {
33
33
  "vitest": "./bin/vitest.mjs"
34
34
  },
35
- "scripts": {
36
- "build": "tsc",
37
- "watch": "tsc --watch",
38
- "lint": "eslint \"{src,test}/**/*.ts\"",
39
- "prepublishOnly": "nr build",
40
- "release": "bumpp --commit --push --tag && pnpm publish",
41
- "test": "node bin/vitest.mjs --dev",
42
- "test:update": "nr test -u"
43
- },
44
35
  "devDependencies": {
45
36
  "@antfu/eslint-config": "^0.11.1",
46
37
  "@antfu/ni": "^0.11.0",
47
38
  "@types/minimist": "^1.2.2",
48
39
  "@types/node": "^16.11.11",
40
+ "@types/sinon": "^10.0.6",
49
41
  "bumpp": "^7.1.1",
50
42
  "eslint": "^8.3.0",
51
43
  "esno": "^0.12.1",
@@ -55,13 +47,25 @@
55
47
  "dependencies": {
56
48
  "@jest/test-result": "^27.4.2",
57
49
  "@types/chai": "^4.2.22",
50
+ "@types/sinon-chai": "^3.2.6",
58
51
  "chai": "^4.3.4",
59
52
  "fast-glob": "^3.2.7",
60
53
  "find-up": "^6.2.0",
61
54
  "jest-snapshot": "^27.4.2",
62
55
  "jest-util": "^27.4.2",
63
56
  "minimist": "^1.2.5",
57
+ "ora": "^6.0.1",
64
58
  "picocolors": "^1.0.0",
65
- "vite-node": "^0.1.9"
59
+ "sinon": "^12.0.1",
60
+ "sinon-chai": "^3.7.0",
61
+ "vite-node": "^0.1.10"
62
+ },
63
+ "scripts": {
64
+ "build": "tsc",
65
+ "watch": "tsc --watch",
66
+ "lint": "eslint \"{src,test}/**/*.ts\"",
67
+ "release": "bumpp --commit --push --tag && pnpm publish",
68
+ "test": "node bin/vitest.mjs --dev",
69
+ "test:update": "nr test -u"
66
70
  }
67
- }
71
+ }