vitest 0.0.11 → 0.0.12

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
@@ -37,6 +37,27 @@ describe('suite name', () => {
37
37
  $ npx vitest
38
38
  ```
39
39
 
40
+ ## Configuration
41
+
42
+ `vitest` will read your root `vite.config.ts` when it present to match with the plugins and setup as your Vite app. If you want to it to have a different configuration for testing, you could either:
43
+
44
+ - Create `vitest.config.ts`, which will have the higher priority
45
+ - Pass `--config` option to CLI, e.g. `vitest --config ./path/to/vitest.config.ts`
46
+ - Use `process.env.VITEST` to conditionally apply differnet configuration in `vite.config.ts`
47
+
48
+ To configure `vitest` itself, add `test` property in your Vite config
49
+
50
+ ```ts
51
+ // vite.config.ts
52
+ import { defineConfig } from 'vite'
53
+
54
+ export default defineConfig({
55
+ test: {
56
+ // ...
57
+ }
58
+ })
59
+ ```
60
+
40
61
  ## Filtering
41
62
 
42
63
  ### Skipping suites and tasks
@@ -103,10 +124,12 @@ describe('suite', () => {
103
124
  - [x] Reporter & Better output
104
125
  - [x] Task filter
105
126
  - [x] Mock
127
+ - [ ] Global Mode & Types
106
128
  - [ ] Parallel Executing
107
129
  - [ ] CLI Help
108
130
  - [ ] JSDom
109
131
  - [ ] Watch
132
+ - [ ] Source Map
110
133
  - [ ] Coverage
111
134
 
112
135
  ## Sponsors
package/bin/vitest.mjs CHANGED
@@ -7,6 +7,8 @@ import { run } from 'vite-node'
7
7
  import minimist from 'minimist'
8
8
  import { findUp } from 'find-up'
9
9
 
10
+ process.env.VITEST = 'true'
11
+
10
12
  const argv = minimist(process.argv.slice(2), {
11
13
  alias: {
12
14
  c: 'config',
package/dist/cli.js CHANGED
@@ -22,7 +22,7 @@ const argv = minimist(process.argv.slice(2), {
22
22
  const server = (_a = process === null || process === void 0 ? void 0 : process.__vite_node__) === null || _a === void 0 ? void 0 : _a.server;
23
23
  const viteConfig = (server === null || server === void 0 ? void 0 : server.config) || {};
24
24
  const testOptions = viteConfig.test || {};
25
- await run(Object.assign(Object.assign({}, testOptions), { updateSnapshot: argv.update, rootDir: argv.root || process.cwd() }));
25
+ await run(Object.assign(Object.assign({}, testOptions), { server, updateSnapshot: argv.update, rootDir: argv.root || process.cwd(), nameFilters: argv._ }));
26
26
  function help() {
27
27
  log('Help: finish help');
28
28
  }
package/dist/index.d.ts CHANGED
@@ -5,4 +5,4 @@ export * from './config';
5
5
  export * from './chai';
6
6
  export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks';
7
7
  export { sinon };
8
- export declare const mock: sinon.SinonMockStatic, spy: sinon.SinonSpyStatic;
8
+ export declare const mock: sinon.SinonMockStatic, spy: sinon.SinonSpyStatic, stub: sinon.SinonStubStatic;
package/dist/index.js CHANGED
@@ -5,4 +5,4 @@ export * from './config';
5
5
  export * from './chai';
6
6
  export { beforeAll, afterAll, beforeEach, afterEach, beforeFile, afterFile, beforeSuite, afterSuite } from './hooks';
7
7
  export { sinon };
8
- export const { mock, spy } = sinon;
8
+ export const { mock, spy, stub } = sinon;
@@ -48,13 +48,13 @@ export class DefaultReporter {
48
48
  var _a;
49
49
  // @ts-expect-error
50
50
  (_a = task.__ora) === null || _a === void 0 ? void 0 : _a.stop();
51
- if (task.status === 'pass') {
51
+ if (task.state === 'pass') {
52
52
  this.log(`${c.green(CHECK + task.name)}`);
53
53
  }
54
- else if (task.status === 'skip') {
54
+ else if (task.state === 'skip') {
55
55
  this.log(c.dim(c.yellow(`${DOT + task.name} (skipped)`)));
56
56
  }
57
- else if (task.status === 'todo') {
57
+ else if (task.state === 'todo') {
58
58
  this.log(c.dim(`${DOT + task.name} (todo)`));
59
59
  }
60
60
  else {
@@ -67,10 +67,11 @@ export class DefaultReporter {
67
67
  this.end = performance.now();
68
68
  const failedFiles = files.filter(i => i.error);
69
69
  const tasks = files.reduce((acc, file) => acc.concat(file.suites.flatMap(i => i.tasks)), []);
70
- const passed = tasks.filter(i => i.status === 'pass');
71
- const failed = tasks.filter(i => i.status === 'fail');
72
- const skipped = tasks.filter(i => i.status === 'skip');
73
- const todo = tasks.filter(i => i.status === 'todo');
70
+ const runable = tasks.filter(i => i.state === 'pass' || i.state === 'fail');
71
+ const passed = tasks.filter(i => i.state === 'pass');
72
+ const failed = tasks.filter(i => i.state === 'fail');
73
+ const skipped = tasks.filter(i => i.state === 'skip');
74
+ const todo = tasks.filter(i => i.state === 'todo');
74
75
  this.indent = 0;
75
76
  if (failedFiles.length) {
76
77
  this.error(c.bold(`\nFailed to parse ${failedFiles.length} files:`));
@@ -89,9 +90,9 @@ export class DefaultReporter {
89
90
  this.log();
90
91
  });
91
92
  }
92
- this.log(c.green(`Passed ${passed.length} / ${tasks.length}`));
93
+ this.log(c.green(`Passed ${passed.length} / ${runable.length}`));
93
94
  if (failed.length)
94
- this.log(c.red(`Failed ${failed.length} / ${tasks.length}`));
95
+ this.log(c.red(`Failed ${failed.length} / ${runable.length}`));
95
96
  if (skipped.length)
96
97
  this.log(c.yellow(`Skipped ${skipped.length}`));
97
98
  if (todo.length)
package/dist/run.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { File, Options, Task, RunnerContext } from './types';
1
+ import { File, Config, Task, RunnerContext } from './types';
2
2
  export declare function runTask(task: Task, ctx: RunnerContext): Promise<void>;
3
- export declare function collectFiles(files: string[]): Promise<File[]>;
3
+ export declare function collectFiles(paths: string[]): Promise<File[]>;
4
4
  export declare function runFile(file: File, ctx: RunnerContext): Promise<void>;
5
- export declare function run(options?: Options): Promise<void>;
5
+ export declare function run(config: Config): Promise<void>;
package/dist/run.js CHANGED
@@ -11,29 +11,23 @@ export async function runTask(task, ctx) {
11
11
  var _a, _b;
12
12
  const { reporter } = ctx;
13
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
+ if (task.mode === 'run') {
15
+ await beforeEachHook.fire(task);
22
16
  try {
23
17
  await task.fn();
24
- task.status = 'pass';
18
+ task.state = 'pass';
25
19
  }
26
20
  catch (e) {
27
- task.status = 'fail';
21
+ task.state = 'fail';
28
22
  task.error = e;
29
23
  }
24
+ await afterEachHook.fire(task);
30
25
  }
31
- await afterEachHook.fire(task);
32
26
  await ((_b = reporter.onTaskEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, task, ctx));
33
27
  }
34
- export async function collectFiles(files) {
35
- const result = [];
36
- for (const filepath of files) {
28
+ export async function collectFiles(paths) {
29
+ const files = [];
30
+ for (const filepath of paths) {
37
31
  const file = {
38
32
  filepath,
39
33
  suites: [],
@@ -54,13 +48,37 @@ export async function collectFiles(files) {
54
48
  file.collected = false;
55
49
  process.exitCode = 1;
56
50
  }
57
- result.push(file);
51
+ files.push(file);
52
+ }
53
+ const allSuites = files.reduce((suites, file) => suites.concat(file.suites), []);
54
+ interpretOnlyMode(allSuites);
55
+ allSuites.forEach((i) => {
56
+ if (i.mode === 'skip')
57
+ i.tasks.forEach(t => t.mode === 'run' && (t.state = 'skip'));
58
+ else
59
+ interpretOnlyMode(i.tasks);
60
+ });
61
+ return files;
62
+ }
63
+ /**
64
+ * If any items been marked as `only`, mark all other items as `skip`.
65
+ */
66
+ function interpretOnlyMode(items) {
67
+ if (items.some(i => i.mode === 'only')) {
68
+ items.forEach((i) => {
69
+ if (i.mode === 'run')
70
+ i.mode = 'skip';
71
+ else if (i.mode === 'only')
72
+ i.mode = 'run';
73
+ });
58
74
  }
59
- return result;
60
75
  }
61
76
  export async function runFile(file, ctx) {
62
77
  var _a, _b, _c, _d;
63
78
  const { reporter } = ctx;
79
+ const runableSuites = file.suites.filter(i => i.mode === 'run');
80
+ if (runableSuites.length === 0)
81
+ return;
64
82
  await ((_a = reporter.onFileBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, file, ctx));
65
83
  await beforeFileHook.fire(file);
66
84
  for (const suite of file.suites) {
@@ -74,42 +92,40 @@ export async function runFile(file, ctx) {
74
92
  await afterFileHook.fire(file);
75
93
  await ((_d = reporter.onFileEnd) === null || _d === void 0 ? void 0 : _d.call(reporter, file, ctx));
76
94
  }
77
- export async function run(options = {}) {
78
- var _a, _b, _c;
79
- const { rootDir = process.cwd() } = options;
95
+ export async function run(config) {
96
+ var _a, _b, _c, _d;
97
+ const { rootDir = process.cwd() } = config;
80
98
  // setup chai
81
99
  chai.use(await SnapshotPlugin({
82
100
  rootDir,
83
- update: options.updateSnapshot,
101
+ update: config.updateSnapshot,
84
102
  }));
85
103
  chai.use(SinonChai);
86
104
  // collect files
87
- const paths = await fg(options.includes || defaultIncludes, {
105
+ let paths = await fg(config.includes || defaultIncludes, {
88
106
  absolute: true,
89
- cwd: options.rootDir,
90
- ignore: options.excludes || defaultExcludes,
107
+ cwd: config.rootDir,
108
+ ignore: config.excludes || defaultExcludes,
91
109
  });
110
+ if ((_a = config.nameFilters) === null || _a === void 0 ? void 0 : _a.length)
111
+ paths = paths.filter(i => config.nameFilters.some(f => i.includes(f)));
92
112
  if (!paths.length) {
93
113
  console.error('No test files found');
94
114
  process.exitCode = 1;
95
115
  return;
96
116
  }
97
117
  const reporter = new DefaultReporter();
98
- await ((_a = reporter.onStart) === null || _a === void 0 ? void 0 : _a.call(reporter, options));
118
+ await ((_b = reporter.onStart) === null || _b === void 0 ? void 0 : _b.call(reporter, config));
99
119
  const files = await collectFiles(paths);
100
120
  const ctx = {
101
121
  files,
102
- mode: isOnlyMode(files) ? 'only' : 'all',
103
- userOptions: options,
122
+ config,
104
123
  reporter,
105
124
  };
106
- await ((_b = reporter.onCollected) === null || _b === void 0 ? void 0 : _b.call(reporter, ctx));
125
+ await ((_c = reporter.onCollected) === null || _c === void 0 ? void 0 : _c.call(reporter, ctx));
107
126
  await beforeAllHook.fire();
108
127
  for (const file of files)
109
128
  await runFile(file, ctx);
110
129
  await afterAllHook.fire();
111
- await ((_c = reporter.onFinished) === null || _c === void 0 ? void 0 : _c.call(reporter, ctx));
112
- }
113
- function isOnlyMode(files) {
114
- return !!files.find(file => file.suites.find(suite => suite.mode === 'only' || suite.tasks.find(t => t.mode === 'only')));
130
+ await ((_d = reporter.onFinished) === null || _d === void 0 ? void 0 : _d.call(reporter, ctx));
115
131
  }
package/dist/suite.js CHANGED
@@ -22,7 +22,7 @@ function createSuiteCollector(mode, suiteName, factory) {
22
22
  name,
23
23
  mode,
24
24
  suite: {},
25
- status: 'init',
25
+ state: mode !== 'run' ? mode : undefined,
26
26
  fn,
27
27
  });
28
28
  }
package/dist/types.d.ts CHANGED
@@ -1,21 +1,25 @@
1
+ import { ViteDevServer } from 'vite';
1
2
  export declare type Awaitable<T> = Promise<T> | T;
2
3
  export interface UserOptions {
3
4
  includes?: string[];
4
5
  excludes?: string[];
5
6
  }
6
- export interface Options extends UserOptions {
7
+ export interface Config extends UserOptions {
7
8
  rootDir?: string;
8
9
  updateSnapshot?: boolean;
10
+ nameFilters?: string[];
11
+ server: ViteDevServer;
12
+ watch?: boolean;
9
13
  }
10
14
  export declare type RunMode = 'run' | 'skip' | 'only' | 'todo';
11
- export declare type TaskStatus = 'init' | 'pass' | 'fail' | 'skip' | 'todo';
15
+ export declare type TaskState = RunMode | 'pass' | 'fail';
12
16
  export interface Task {
13
17
  name: string;
14
18
  mode: RunMode;
15
19
  suite: Suite;
16
20
  fn: () => Awaitable<void>;
17
21
  file?: File;
18
- status: TaskStatus;
22
+ state?: TaskState;
19
23
  error?: unknown;
20
24
  }
21
25
  export declare type TestFunction = () => Awaitable<void>;
@@ -47,8 +51,7 @@ export interface File {
47
51
  }
48
52
  export interface RunnerContext {
49
53
  files: File[];
50
- mode: 'all' | 'only';
51
- userOptions: Options;
54
+ config: Config;
52
55
  reporter: Reporter;
53
56
  }
54
57
  export interface GlobalContext {
@@ -56,7 +59,7 @@ export interface GlobalContext {
56
59
  currentSuite: SuiteCollector | null;
57
60
  }
58
61
  export interface Reporter {
59
- onStart: (userOptions: Options) => Awaitable<void>;
62
+ onStart: (userOptions: Config) => Awaitable<void>;
60
63
  onCollected: (ctx: RunnerContext) => Awaitable<void>;
61
64
  onFinished: (ctx: RunnerContext) => Awaitable<void>;
62
65
  onSuiteBegin: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
package/dist/types.js CHANGED
@@ -1,2 +1 @@
1
- /* eslint-disable no-use-before-define */
2
1
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
5
  "description": "",
6
6
  "keywords": [],
@@ -32,15 +32,6 @@
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",
@@ -68,5 +59,13 @@
68
59
  "sinon": "^12.0.1",
69
60
  "sinon-chai": "^3.7.0",
70
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"
71
70
  }
72
- }
71
+ }