vitest 0.0.29 → 0.0.30

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.
@@ -24,6 +24,7 @@ export declare class SnapshotManager {
24
24
  setTask(task: Task): void;
25
25
  setContext(context: Context): void;
26
26
  assert(received: unknown, message: string): void;
27
+ clear(): void;
27
28
  saveSnap(): void;
28
- report(): void;
29
+ report(): string[] | undefined;
29
30
  }
@@ -58,6 +58,9 @@ export class SnapshotManager {
58
58
  expect(actual.trim()).equals(expected ? expected.trim() : '', message || `Snapshot name: \`${key}\``);
59
59
  }
60
60
  }
61
+ clear() {
62
+ this.snapshotSummary = makeEmptySnapshotSummary(this.snapshotOptions);
63
+ }
61
64
  saveSnap() {
62
65
  if (!this.testFile || !this.snapshotState)
63
66
  return;
@@ -69,7 +72,6 @@ export class SnapshotManager {
69
72
  report() {
70
73
  const outputs = getSnapshotSummaryOutput(this.rootDir, this.snapshotSummary);
71
74
  if (outputs.length > 1)
72
- // eslint-disable-next-line no-console
73
- console.log(`\n${outputs.join('\n')}`);
75
+ return outputs;
74
76
  }
75
77
  }
package/dist/node/cli.js CHANGED
@@ -24,6 +24,7 @@ sade('vitest [filter]', true)
24
24
  const defaultInline = [
25
25
  'vue',
26
26
  '@vue',
27
+ 'diff',
27
28
  ];
28
29
  const __dirname = dirname(fileURLToPath(import.meta.url));
29
30
  const root = resolve(argv.root || process.cwd());
@@ -1,4 +1,4 @@
1
- import { InlineConfig, ViteDevServer } from 'vite';
1
+ import { InlineConfig, ViteDevServer, TransformResult } from 'vite';
2
2
  declare global {
3
3
  namespace NodeJS {
4
4
  interface Process {
@@ -6,6 +6,7 @@ declare global {
6
6
  server: ViteDevServer;
7
7
  watch?: boolean;
8
8
  moduleCache: Map<string, Promise<any>>;
9
+ modulesTransformResult: Map<string, TransformResult>;
9
10
  };
10
11
  }
11
12
  }
package/dist/node/node.js CHANGED
@@ -5,7 +5,8 @@ import vm from 'vm';
5
5
  import { createServer, mergeConfig } from 'vite';
6
6
  import c from 'picocolors';
7
7
  const { red, dim, yellow } = c;
8
- const __pendingModules__ = new Map();
8
+ const moduleCache = new Map();
9
+ const modulesTransformResult = new Map();
9
10
  export async function run(argv) {
10
11
  process.exitCode = 0;
11
12
  const root = argv.root || process.cwd();
@@ -22,7 +23,8 @@ export async function run(argv) {
22
23
  await server.pluginContainer.buildStart({});
23
24
  process.__vite_node__ = {
24
25
  server,
25
- moduleCache: __pendingModules__,
26
+ moduleCache,
27
+ modulesTransformResult,
26
28
  };
27
29
  try {
28
30
  await execute(files, server, argv);
@@ -104,6 +106,7 @@ async function execute(files, server, options) {
104
106
  const result = await transform(server, id);
105
107
  if (!result)
106
108
  throw new Error(`failed to load ${id}`);
109
+ modulesTransformResult.set(id, result);
107
110
  const url = pathToFileURL(fsPath);
108
111
  const exports = {};
109
112
  const context = {
@@ -130,10 +133,10 @@ async function execute(files, server, options) {
130
133
  const fsPath = toFilePath(id, server);
131
134
  if (options.shouldExternalize(fsPath, server))
132
135
  return import(fsPath);
133
- if (__pendingModules__.has(fsPath))
134
- return __pendingModules__.get(fsPath);
135
- __pendingModules__.set(fsPath, directRequest(id, fsPath, callstack));
136
- return await __pendingModules__.get(fsPath);
136
+ if (moduleCache.has(fsPath))
137
+ return moduleCache.get(fsPath);
138
+ moduleCache.set(fsPath, directRequest(id, fsPath, callstack));
139
+ return await moduleCache.get(fsPath);
137
140
  }
138
141
  function exportAll(exports, sourceModule) {
139
142
  // eslint-disable-next-line no-restricted-syntax
@@ -16,7 +16,7 @@ export declare class DefaultReporter implements Reporter {
16
16
  onStart(config: ResolvedConfig): void;
17
17
  onCollected(files: File[]): void;
18
18
  onTaskEnd(task: Task): void;
19
- onFinished(ctx: RunnerContext): Promise<void>;
19
+ onFinished(ctx: RunnerContext, files?: File[]): Promise<void>;
20
20
  onWatcherStart(ctx: RunnerContext): Promise<void>;
21
21
  onWatcherRerun(files: string[], trigger: string): Promise<void>;
22
22
  onSnapshotUpdate(): void;
@@ -3,6 +3,7 @@ import { performance } from 'perf_hooks';
3
3
  import { relative } from 'path';
4
4
  import c from 'picocolors';
5
5
  import Listr from 'listr';
6
+ import { printError } from './error';
6
7
  const CROSS = '✖ ';
7
8
  export class DefaultReporter {
8
9
  constructor() {
@@ -77,11 +78,16 @@ export class DefaultReporter {
77
78
  else
78
79
  (_b = this.taskMap.get(task)) === null || _b === void 0 ? void 0 : _b.resolve();
79
80
  }
80
- async onFinished(ctx) {
81
+ async onFinished(ctx, files = ctx.files) {
82
+ var _a;
81
83
  await this.listrPromise;
82
84
  this.end = performance.now();
83
85
  console.log();
84
- const { tasks, suites, files } = ctx;
86
+ const snapshot = ctx.snapshotManager.report();
87
+ if (snapshot)
88
+ console.log(snapshot.join('\n'));
89
+ const suites = files.flatMap(i => i.suites);
90
+ const tasks = suites.flatMap(i => i.tasks);
85
91
  const failedFiles = files.filter(i => i.error);
86
92
  const failedSuites = suites.filter(i => i.error);
87
93
  const runnable = tasks.filter(i => i.state === 'pass' || i.state === 'fail');
@@ -90,30 +96,30 @@ export class DefaultReporter {
90
96
  const skipped = tasks.filter(i => i.state === 'skip');
91
97
  const todo = tasks.filter(i => i.state === 'todo');
92
98
  if (failedFiles.length) {
93
- console.error(c.bold(`\nFailed to parse ${failedFiles.length} files:`));
94
- failedFiles.forEach((i) => {
95
- console.error(c.red(`\n- ${i.filepath}`));
96
- console.error(i.error || 'Unknown error');
99
+ console.error(c.red(c.bold(`\nFailed to parse ${failedFiles.length} files:`)));
100
+ for (const file of failedFiles)
101
+ console.error(c.red(`- ${file.filepath}`));
102
+ console.log();
103
+ for (const file of failedFiles) {
104
+ await printError(file.error);
97
105
  console.log();
98
- });
106
+ }
99
107
  }
100
108
  if (failedSuites.length) {
101
109
  console.error(c.bold(c.red(`\nFailed to run ${failedSuites.length} suites:`)));
102
- failedSuites.forEach((i) => {
103
- var _a;
104
- console.error(c.red(`\n- ${(_a = i.file) === null || _a === void 0 ? void 0 : _a.filepath} > ${i.name}`));
105
- console.error(i.error || 'Unknown error');
110
+ for (const suite of failedSuites) {
111
+ console.error(c.red(`\n- ${(_a = suite.file) === null || _a === void 0 ? void 0 : _a.filepath} > ${suite.name}`));
112
+ await printError(suite.error);
106
113
  console.log();
107
- });
114
+ }
108
115
  }
109
116
  if (failed.length) {
110
117
  console.error(c.bold(c.red(`\nFailed Tests (${failed.length})`)));
111
- failed.forEach((task) => {
112
- var _a;
113
- console.error(`\n${CROSS + c.inverse(c.red(' FAIL '))} ${[task.suite.name, task.name].filter(Boolean).join(' > ')} ${c.gray(c.dim(`${(_a = task.file) === null || _a === void 0 ? void 0 : _a.filepath}`))}`);
114
- console.error(task.error || 'Unknown error');
118
+ for (const task of failed) {
119
+ console.error(`${c.red(`\n${CROSS + c.inverse(' FAIL ')}`)} ${[task.suite.name, task.name].filter(Boolean).join(' > ')}`);
120
+ await printError(task.error);
115
121
  console.log();
116
- });
122
+ }
117
123
  }
118
124
  console.log(c.bold(c.green(`Passed ${passed.length} / ${runnable.length}`)));
119
125
  if (failed.length)
@@ -0,0 +1,9 @@
1
+ export declare function printError(error: unknown): Promise<void>;
2
+ interface Poisition {
3
+ line: number;
4
+ column: number;
5
+ }
6
+ export declare function posToNumber(source: string, pos: number | Poisition): number;
7
+ export declare function numberToPos(source: string, offset: number | Poisition): Poisition;
8
+ export declare function generateCodeFrame(source: string, start?: number | Poisition, end?: number, range?: number): string;
9
+ export {};
@@ -0,0 +1,182 @@
1
+ /* eslint-disable no-console */
2
+ import { promises as fs, existsSync } from 'fs';
3
+ import c from 'picocolors';
4
+ import * as diff from 'diff';
5
+ import { notNullish } from '@antfu/utils';
6
+ import { SourceMapConsumer } from 'source-map';
7
+ export async function printError(error) {
8
+ if (!(error instanceof Error)) {
9
+ console.error(error);
10
+ return;
11
+ }
12
+ const { modulesTransformResult } = process.__vite_node__;
13
+ const e = error;
14
+ let codeFramePrinted = false;
15
+ const stacks = parseStack(e.stack || '');
16
+ const nearest = stacks.find(stack => modulesTransformResult.has(stack.file));
17
+ if (nearest) {
18
+ const transformResult = modulesTransformResult.get(nearest.file);
19
+ const pos = await getOriginalPos(transformResult === null || transformResult === void 0 ? void 0 : transformResult.map, nearest);
20
+ if (pos && existsSync(nearest.file)) {
21
+ const sourceCode = await fs.readFile(nearest.file, 'utf-8');
22
+ console.error(`${c.red(`${c.bold(e.name)}: ${e.message}`)}`);
23
+ console.log(c.gray(`${nearest.file}:${pos.line}:${pos.column}`));
24
+ console.log(c.yellow(generateCodeFrame(sourceCode, pos)));
25
+ codeFramePrinted = true;
26
+ }
27
+ }
28
+ if (!codeFramePrinted)
29
+ console.error(e);
30
+ if (e.showDiff)
31
+ console.error(c.gray(generateDiff(stringify(e.actual), stringify(e.expected))));
32
+ }
33
+ function getOriginalPos(map, { line, column }) {
34
+ return new Promise((resolve) => {
35
+ if (!map)
36
+ return resolve(null);
37
+ SourceMapConsumer.with(map, null, (consumer) => {
38
+ const pos = consumer.originalPositionFor({ line, column });
39
+ if (pos.line != null && pos.column != null)
40
+ resolve(pos);
41
+ else
42
+ resolve(null);
43
+ });
44
+ });
45
+ }
46
+ const splitRE = /\r?\n/;
47
+ export function posToNumber(source, pos) {
48
+ if (typeof pos === 'number')
49
+ return pos;
50
+ const lines = source.split(splitRE);
51
+ const { line, column } = pos;
52
+ let start = 0;
53
+ for (let i = 0; i < line - 1; i++)
54
+ start += lines[i].length + 1;
55
+ return start + column;
56
+ }
57
+ export function numberToPos(source, offset) {
58
+ if (typeof offset !== 'number')
59
+ return offset;
60
+ if (offset > source.length) {
61
+ throw new Error(`offset is longer than source length! offset ${offset} > length ${source.length}`);
62
+ }
63
+ const lines = source.split(splitRE);
64
+ let counted = 0;
65
+ let line = 0;
66
+ let column = 0;
67
+ for (; line < lines.length; line++) {
68
+ const lineLength = lines[line].length + 1;
69
+ if (counted + lineLength >= offset) {
70
+ column = offset - counted + 1;
71
+ break;
72
+ }
73
+ counted += lineLength;
74
+ }
75
+ return { line: line + 1, column };
76
+ }
77
+ export function generateCodeFrame(source, start = 0, end, range = 2) {
78
+ start = posToNumber(source, start);
79
+ end = end || start;
80
+ const lines = source.split(splitRE);
81
+ let count = 0;
82
+ const res = [];
83
+ for (let i = 0; i < lines.length; i++) {
84
+ count += lines[i].length + 1;
85
+ if (count >= start) {
86
+ for (let j = i - range; j <= i + range || end > count; j++) {
87
+ if (j < 0 || j >= lines.length)
88
+ continue;
89
+ const line = j + 1;
90
+ res.push(`${c.gray(`${line}${' '.repeat(Math.max(3 - String(line).length, 0))}|`)} ${lines[j]}`);
91
+ const lineLength = lines[j].length;
92
+ if (j === i) {
93
+ // push underline
94
+ const pad = start - (count - lineLength) + 1;
95
+ const length = Math.max(1, end > count ? lineLength - pad : end - start);
96
+ res.push(`${c.gray(' |')} ${' '.repeat(pad)}${'^'.repeat(length)}`);
97
+ }
98
+ else if (j > i) {
99
+ if (end > count) {
100
+ const length = Math.max(Math.min(end - count, lineLength), 1);
101
+ res.push(`${c.gray(' |')} ${'^'.repeat(length)}`);
102
+ }
103
+ count += lineLength + 1;
104
+ }
105
+ }
106
+ break;
107
+ }
108
+ }
109
+ return res.join('\n');
110
+ }
111
+ function stringify(obj) {
112
+ // TODO: handle more types
113
+ return String(obj);
114
+ }
115
+ const stackFnCallRE = /at (.*) \((.+):(\d+):(\d+)\)$/;
116
+ const stackBarePathRE = /at ()(.+):(\d+):(\d+)$/;
117
+ function parseStack(stack) {
118
+ const lines = stack.split('\n');
119
+ const stackFrames = lines.map((raw) => {
120
+ const line = raw.trim();
121
+ const match = line.match(stackFnCallRE) || line.match(stackBarePathRE);
122
+ if (!match)
123
+ return null;
124
+ let file = match[2];
125
+ if (file.startsWith('file://'))
126
+ file = file.slice(7);
127
+ return {
128
+ method: match[1],
129
+ file: match[2],
130
+ line: parseInt(match[3]),
131
+ column: parseInt(match[4]),
132
+ };
133
+ });
134
+ return stackFrames.filter(notNullish);
135
+ }
136
+ /**
137
+ * Returns a diff between 2 strings with coloured ANSI output.
138
+ *
139
+ * @description
140
+ * The diff will be either inline or unified dependent on the value
141
+ * of `Base.inlineDiff`.
142
+ *
143
+ * @param {string} actual
144
+ * @param {string} expected
145
+ * @return {string} Diff
146
+ */
147
+ function generateDiff(actual, expected) {
148
+ const diffSize = 2048;
149
+ if (actual.length > diffSize)
150
+ actual = `${actual.substring(0, diffSize)} ... Lines skipped`;
151
+ if (expected.length > diffSize)
152
+ expected = `${expected.substring(0, diffSize)} ... Lines skipped`;
153
+ return unifiedDiff(actual, expected);
154
+ }
155
+ /**
156
+ * Returns unified diff between two strings with coloured ANSI output.
157
+ *
158
+ * @private
159
+ * @param {String} actual
160
+ * @param {String} expected
161
+ * @return {string} The diff.
162
+ */
163
+ function unifiedDiff(actual, expected) {
164
+ const indent = ' ';
165
+ function cleanUp(line) {
166
+ if (line[0] === '+')
167
+ return indent + c.green(`${line[0]} ${line.slice(1)}`);
168
+ if (line[0] === '-')
169
+ return indent + c.red(`${line[0]} ${line.slice(1)}`);
170
+ if (line.match(/@@/))
171
+ return '--';
172
+ if (line.match(/\\ No newline/))
173
+ return null;
174
+ return indent + line;
175
+ }
176
+ const msg = diff.createPatch('string', actual, expected);
177
+ const lines = msg.split('\n').splice(5);
178
+ return (`\n${indent}${c.red('- actual')}\n${indent}${c.green('+ expected')}\n\n${lines.map(cleanUp).filter(notBlank).join('\n')}`);
179
+ }
180
+ function notBlank(line) {
181
+ return typeof line !== 'undefined' && line !== null;
182
+ }
package/dist/run/index.js CHANGED
@@ -163,6 +163,7 @@ export async function run(config) {
163
163
  (await import('../integrations/jsdom')).setupJSDOM(globalThis);
164
164
  await ((_b = reporter.onStart) === null || _b === void 0 ? void 0 : _b.call(reporter, config));
165
165
  const filesMap = await collectFiles(testFilepaths);
166
+ const snapshotManager = getSnapshotManager();
166
167
  const ctx = {
167
168
  filesMap,
168
169
  get files() {
@@ -177,19 +178,19 @@ export async function run(config) {
177
178
  .reduce((tasks, suite) => tasks.concat(suite.tasks), []);
178
179
  },
179
180
  config,
180
- reporter: config.reporter,
181
+ reporter,
182
+ snapshotManager,
181
183
  };
182
184
  await runFiles(filesMap, ctx);
183
- const snapshot = getSnapshotManager();
184
- snapshot === null || snapshot === void 0 ? void 0 : snapshot.saveSnap();
185
- snapshot === null || snapshot === void 0 ? void 0 : snapshot.report();
185
+ snapshotManager.saveSnap();
186
186
  await ((_c = reporter.onFinished) === null || _c === void 0 ? void 0 : _c.call(reporter, ctx));
187
187
  if (config.watch)
188
188
  startWatcher(ctx);
189
189
  }
190
190
  export async function startWatcher(ctx) {
191
- var _a, _b;
192
- await ((_b = (_a = ctx.reporter).onWatcherStart) === null || _b === void 0 ? void 0 : _b.call(_a, ctx));
191
+ var _a;
192
+ const { reporter, snapshotManager, filesMap } = ctx;
193
+ await ((_a = reporter.onWatcherStart) === null || _a === void 0 ? void 0 : _a.call(reporter, ctx));
193
194
  let timer;
194
195
  const changedTests = new Set();
195
196
  const seen = new Set();
@@ -202,21 +203,20 @@ export async function startWatcher(ctx) {
202
203
  return;
203
204
  clearTimeout(timer);
204
205
  timer = setTimeout(async () => {
205
- var _a, _b, _c, _d;
206
+ var _a, _b, _c;
206
207
  if (changedTests.size === 0)
207
208
  return;
208
- const snapshot = getSnapshotManager();
209
+ snapshotManager.clear();
209
210
  const paths = Array.from(changedTests);
210
211
  changedTests.clear();
211
- await ((_b = (_a = ctx.reporter).onWatcherRerun) === null || _b === void 0 ? void 0 : _b.call(_a, paths, id, ctx));
212
+ await ((_a = reporter.onWatcherRerun) === null || _a === void 0 ? void 0 : _a.call(reporter, paths, id, ctx));
212
213
  paths.forEach(i => moduleCache.delete(i));
213
- const files = await collectFiles(paths);
214
- Object.assign(ctx.filesMap, files);
215
- await runFiles(files, ctx);
216
- // TODO: clear snapshot state
217
- snapshot === null || snapshot === void 0 ? void 0 : snapshot.saveSnap();
218
- snapshot === null || snapshot === void 0 ? void 0 : snapshot.report();
219
- await ((_d = (_c = ctx.reporter).onWatcherStart) === null || _d === void 0 ? void 0 : _d.call(_c, ctx));
214
+ const newFilesMap = await collectFiles(paths);
215
+ Object.assign(filesMap, newFilesMap);
216
+ await runFiles(newFilesMap, ctx);
217
+ snapshotManager.saveSnap();
218
+ await ((_b = reporter.onFinished) === null || _b === void 0 ? void 0 : _b.call(reporter, ctx, Object.values(newFilesMap)));
219
+ await ((_c = reporter.onWatcherStart) === null || _c === void 0 ? void 0 : _c.call(reporter, ctx));
220
220
  }, 100);
221
221
  });
222
222
  }
package/dist/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { SnapshotManager } from './integrations/chai/snapshot/manager';
1
2
  export declare type Awaitable<T> = Promise<T> | T;
2
3
  export interface UserOptions {
3
4
  /**
@@ -116,6 +117,7 @@ export interface RunnerContext {
116
117
  tasks: Task[];
117
118
  config: ResolvedConfig;
118
119
  reporter: Reporter;
120
+ snapshotManager: SnapshotManager;
119
121
  }
120
122
  export interface GlobalContext {
121
123
  suites: SuiteCollector[];
@@ -124,7 +126,7 @@ export interface GlobalContext {
124
126
  export interface Reporter {
125
127
  onStart?: (config: ResolvedConfig) => Awaitable<void>;
126
128
  onCollected?: (files: File[], ctx: RunnerContext) => Awaitable<void>;
127
- onFinished?: (ctx: RunnerContext) => Awaitable<void>;
129
+ onFinished?: (ctx: RunnerContext, files?: File[]) => Awaitable<void>;
128
130
  onSuiteBegin?: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
129
131
  onSuiteEnd?: (suite: Suite, ctx: RunnerContext) => Awaitable<void>;
130
132
  onFileBegin?: (file: File, ctx: RunnerContext) => Awaitable<void>;
@@ -133,5 +135,4 @@ export interface Reporter {
133
135
  onTaskEnd?: (task: Task, ctx: RunnerContext) => Awaitable<void>;
134
136
  onWatcherStart?: (ctx: RunnerContext) => Awaitable<void>;
135
137
  onWatcherRerun?: (files: string[], trigger: string, ctx: RunnerContext) => Awaitable<void>;
136
- onSnapshotUpdate?: () => Awaitable<void>;
137
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.29",
3
+ "version": "0.0.30",
4
4
  "description": "A blazing fast unit test framework powered by Vite",
5
5
  "keywords": [
6
6
  "vite",
@@ -50,11 +50,13 @@
50
50
  "watch": "tsc -p src/tsconfig.json --watch"
51
51
  },
52
52
  "dependencies": {
53
+ "@antfu/utils": "^0.3.0",
53
54
  "@jest/test-result": "^27.4.2",
54
55
  "@types/chai": "^4.2.22",
55
56
  "@types/sinon-chai": "^3.2.6",
56
57
  "chai": "^4.3.4",
57
58
  "chai-subset": "^1.6.0",
59
+ "diff": "^5.0.0",
58
60
  "fast-glob": "^3.2.7",
59
61
  "find-up": "^6.2.0",
60
62
  "jest-snapshot": "^27.4.2",
@@ -64,7 +66,8 @@
64
66
  "picocolors": "^1.0.0",
65
67
  "sade": "^1.7.4",
66
68
  "sinon": "^12.0.1",
67
- "sinon-chai": "^3.7.0"
69
+ "sinon-chai": "^3.7.0",
70
+ "source-map": "^0.7.3"
68
71
  },
69
72
  "peerDependencies": {
70
73
  "vite": "^2.7.0"
@@ -73,6 +76,7 @@
73
76
  "@antfu/eslint-config": "^0.12.1",
74
77
  "@antfu/ni": "^0.11.0",
75
78
  "@types/chai-subset": "^1.3.3",
79
+ "@types/diff": "^5.0.1",
76
80
  "@types/jsdom": "^16.2.13",
77
81
  "@types/listr": "^0.14.4",
78
82
  "@types/node": "^16.11.11",