vitest 0.0.17 → 0.0.21

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
@@ -156,8 +156,8 @@ describe('suite', () => {
156
156
  - [x] Mock
157
157
  - [x] Global Mode & Types
158
158
  - [ ] Parallel Executing
159
- - [ ] CLI Help
160
- - [ ] JSDom
159
+ - [ ] CLI Help (Use yargs)
160
+ - [x] JSDom
161
161
  - [ ] Watch
162
162
  - [ ] Source Map
163
163
  - [ ] Coverage
package/dist/index.d.ts CHANGED
@@ -3,3 +3,29 @@ export * from './suite';
3
3
  export * from './config';
4
4
  export * from './integrations/chai';
5
5
  export * from './integrations/sinon';
6
+ declare global {
7
+ namespace Chai {
8
+ interface Assertion {
9
+ toMatchSnapshot(message?: string): Assertion;
10
+ matchSnapshot(message?: string): Assertion;
11
+ toEqual(expected: any): void;
12
+ toStrictEqual(expected: any): void;
13
+ toBe(expected: any): void;
14
+ toContain(item: any): void;
15
+ toBeTruthy(): void;
16
+ toBeFalsy(): void;
17
+ toBeNaN(): void;
18
+ toBeUndefined(): void;
19
+ toBeNull(): void;
20
+ toBeDefined(): void;
21
+ toBeInstanceOf(c: any): void;
22
+ toBeCalledTimes(n: number): void;
23
+ toBeCalledOnce(): void;
24
+ toBeCalled(): void;
25
+ toHaveBeenCalled(): void;
26
+ }
27
+ interface ExpectStatic {
28
+ addSnapshotSerializer: import('pretty-format').Plugin;
29
+ }
30
+ }
31
+ }
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ /* eslint-disable @typescript-eslint/no-namespace */
1
2
  export * from './types';
2
3
  export * from './suite';
3
4
  export * from './config';
@@ -1,21 +1,2 @@
1
1
  import { ChaiPlugin } from './types';
2
2
  export declare function JestChaiExpect(): ChaiPlugin;
3
- declare global {
4
- namespace Chai {
5
- interface Assertion {
6
- toEqual(expected: any): void;
7
- toStrictEqual(expected: any): void;
8
- toBe(expected: any): void;
9
- toContain(item: any): void;
10
- toBeTruthy(): void;
11
- toBeFalsy(): void;
12
- toBeNaN(): void;
13
- toBeUndefined(): void;
14
- toBeNull(): void;
15
- toBeDefined(): void;
16
- }
17
- interface ExpectStatic {
18
- addSnapshotSerializer: import('pretty-format').Plugin;
19
- }
20
- }
21
- }
@@ -10,7 +10,7 @@ export function JestChaiExpect() {
10
10
  return this.equal(expected);
11
11
  });
12
12
  utils.addMethod(proto, 'toBe', function (expected) {
13
- return this.be(expected);
13
+ return this.equal(expected);
14
14
  });
15
15
  utils.addMethod(proto, 'toContain', function (item) {
16
16
  return this.contain(item);
@@ -19,7 +19,7 @@ export function JestChaiExpect() {
19
19
  const obj = utils.flag(this, 'object');
20
20
  this.assert(Boolean(obj), 'expected #{this} to be truthy', 'expected #{this} to not be truthy', obj);
21
21
  });
22
- utils.addMethod(proto, 'toFalsy', function () {
22
+ utils.addMethod(proto, 'toBeFalsy', function () {
23
23
  const obj = utils.flag(this, 'object');
24
24
  this.assert(!obj, 'expected #{this} to be falsy', 'expected #{this} to not be falsy', obj);
25
25
  });
@@ -35,5 +35,21 @@ export function JestChaiExpect() {
35
35
  utils.addMethod(proto, 'toBeDefined', function () {
36
36
  return this.not.be.undefined;
37
37
  });
38
+ utils.addMethod(proto, 'toBeInstanceOf', function (obj) {
39
+ return this.instanceOf(obj);
40
+ });
41
+ // mock
42
+ utils.addMethod(proto, 'toBeCalledTimes', function (number) {
43
+ return this.callCount(number);
44
+ });
45
+ utils.addMethod(proto, 'toBeCalledOnce', function () {
46
+ return this.callCount(1);
47
+ });
48
+ utils.addMethod(proto, 'toBeCalled', function () {
49
+ return this.called;
50
+ });
51
+ utils.addMethod(proto, 'toHaveBeenCalled', function () {
52
+ return this.called;
53
+ });
38
54
  };
39
55
  }
@@ -6,14 +6,3 @@ export interface SnapshotOptions {
6
6
  }
7
7
  export declare function getSnapshotManager(): SnapshotManager;
8
8
  export declare function SnapshotPlugin(options: SnapshotOptions): Promise<ChaiPlugin>;
9
- declare global {
10
- namespace Chai {
11
- interface Assertion {
12
- toMatchSnapshot(message?: string): Assertion;
13
- matchSnapshot(message?: string): Assertion;
14
- }
15
- interface ExpectStatic {
16
- addSnapshotSerializer: import('pretty-format').Plugin;
17
- }
18
- }
19
- }
@@ -1,4 +1,5 @@
1
1
  import { JSDOM } from 'jsdom';
2
+ import { KEYS } from './keys';
2
3
  export function setupJSDOM(global) {
3
4
  const dom = new JSDOM('<!DOCTYPE html>', {
4
5
  pretendToBeVisual: true,
@@ -6,7 +7,7 @@ export function setupJSDOM(global) {
6
7
  // TODO: options
7
8
  url: 'http://localhost:3000',
8
9
  });
9
- const keys = Object.getOwnPropertyNames(dom.window)
10
+ const keys = KEYS.concat(Object.getOwnPropertyNames(dom.window))
10
11
  .filter(k => !k.startsWith('_'))
11
12
  .filter(k => !(k in global));
12
13
  for (const key of keys)
@@ -0,0 +1 @@
1
+ export declare const KEYS: string[];
@@ -0,0 +1,220 @@
1
+ // SEE https://github.com/jsdom/jsdom/blob/master/lib/jsdom/living/interfaces.js
2
+ const LIVING_KEYS = [
3
+ 'DOMException',
4
+ 'URL',
5
+ 'URLSearchParams',
6
+ 'EventTarget',
7
+ 'NamedNodeMap',
8
+ 'Node',
9
+ 'Attr',
10
+ 'Element',
11
+ 'DocumentFragment',
12
+ 'DOMImplementation',
13
+ 'Document',
14
+ 'XMLDocument',
15
+ 'CharacterData',
16
+ 'Text',
17
+ 'CDATASection',
18
+ 'ProcessingInstruction',
19
+ 'Comment',
20
+ 'DocumentType',
21
+ 'NodeList',
22
+ 'HTMLCollection',
23
+ 'HTMLOptionsCollection',
24
+ 'DOMStringMap',
25
+ 'DOMTokenList',
26
+ 'StyleSheetList',
27
+ 'HTMLElement',
28
+ 'HTMLHeadElement',
29
+ 'HTMLTitleElement',
30
+ 'HTMLBaseElement',
31
+ 'HTMLLinkElement',
32
+ 'HTMLMetaElement',
33
+ 'HTMLStyleElement',
34
+ 'HTMLBodyElement',
35
+ 'HTMLHeadingElement',
36
+ 'HTMLParagraphElement',
37
+ 'HTMLHRElement',
38
+ 'HTMLPreElement',
39
+ 'HTMLUListElement',
40
+ 'HTMLOListElement',
41
+ 'HTMLLIElement',
42
+ 'HTMLMenuElement',
43
+ 'HTMLDListElement',
44
+ 'HTMLDivElement',
45
+ 'HTMLAnchorElement',
46
+ 'HTMLAreaElement',
47
+ 'HTMLBRElement',
48
+ 'HTMLButtonElement',
49
+ 'HTMLCanvasElement',
50
+ 'HTMLDataElement',
51
+ 'HTMLDataListElement',
52
+ 'HTMLDetailsElement',
53
+ 'HTMLDialogElement',
54
+ 'HTMLDirectoryElement',
55
+ 'HTMLFieldSetElement',
56
+ 'HTMLFontElement',
57
+ 'HTMLFormElement',
58
+ 'HTMLHtmlElement',
59
+ 'HTMLImageElement',
60
+ 'HTMLInputElement',
61
+ 'HTMLLabelElement',
62
+ 'HTMLLegendElement',
63
+ 'HTMLMapElement',
64
+ 'HTMLMarqueeElement',
65
+ 'HTMLMediaElement',
66
+ 'HTMLMeterElement',
67
+ 'HTMLModElement',
68
+ 'HTMLOptGroupElement',
69
+ 'HTMLOptionElement',
70
+ 'HTMLOutputElement',
71
+ 'HTMLPictureElement',
72
+ 'HTMLProgressElement',
73
+ 'HTMLQuoteElement',
74
+ 'HTMLScriptElement',
75
+ 'HTMLSelectElement',
76
+ 'HTMLSlotElement',
77
+ 'HTMLSourceElement',
78
+ 'HTMLSpanElement',
79
+ 'HTMLTableCaptionElement',
80
+ 'HTMLTableCellElement',
81
+ 'HTMLTableColElement',
82
+ 'HTMLTableElement',
83
+ 'HTMLTimeElement',
84
+ 'HTMLTableRowElement',
85
+ 'HTMLTableSectionElement',
86
+ 'HTMLTemplateElement',
87
+ 'HTMLTextAreaElement',
88
+ 'HTMLUnknownElement',
89
+ 'HTMLFrameElement',
90
+ 'HTMLFrameSetElement',
91
+ 'HTMLIFrameElement',
92
+ 'HTMLEmbedElement',
93
+ 'HTMLObjectElement',
94
+ 'HTMLParamElement',
95
+ 'HTMLVideoElement',
96
+ 'HTMLAudioElement',
97
+ 'HTMLTrackElement',
98
+ 'SVGElement',
99
+ 'SVGGraphicsElement',
100
+ 'SVGSVGElement',
101
+ 'SVGTitleElement',
102
+ 'SVGAnimatedString',
103
+ 'SVGNumber',
104
+ 'SVGStringList',
105
+ 'Event',
106
+ 'CloseEvent',
107
+ 'CustomEvent',
108
+ 'MessageEvent',
109
+ 'ErrorEvent',
110
+ 'HashChangeEvent',
111
+ 'PopStateEvent',
112
+ 'StorageEvent',
113
+ 'ProgressEvent',
114
+ 'PageTransitionEvent',
115
+ 'UIEvent',
116
+ 'FocusEvent',
117
+ 'InputEvent',
118
+ 'MouseEvent',
119
+ 'KeyboardEvent',
120
+ 'TouchEvent',
121
+ 'CompositionEvent',
122
+ 'WheelEvent',
123
+ 'BarProp',
124
+ 'External',
125
+ 'Location',
126
+ 'History',
127
+ 'Screen',
128
+ 'Performance',
129
+ 'Navigator',
130
+ 'PluginArray',
131
+ 'MimeTypeArray',
132
+ 'Plugin',
133
+ 'MimeType',
134
+ 'FileReader',
135
+ 'Blob',
136
+ 'File',
137
+ 'FileList',
138
+ 'ValidityState',
139
+ 'DOMParser',
140
+ 'XMLSerializer',
141
+ 'FormData',
142
+ 'XMLHttpRequestEventTarget',
143
+ 'XMLHttpRequestUpload',
144
+ 'XMLHttpRequest',
145
+ 'WebSocket',
146
+ 'NodeFilter',
147
+ 'NodeIterator',
148
+ 'TreeWalker',
149
+ 'AbstractRange',
150
+ 'Range',
151
+ 'StaticRange',
152
+ 'Selection',
153
+ 'Storage',
154
+ 'CustomElementRegistry',
155
+ 'ShadowRoot',
156
+ 'MutationObserver',
157
+ 'MutationRecord',
158
+ 'Headers',
159
+ 'AbortController',
160
+ 'AbortSignal',
161
+ ];
162
+ const OTHER_KEYS = [
163
+ 'addEventListener',
164
+ 'alert',
165
+ 'atob',
166
+ 'blur',
167
+ 'btoa',
168
+ /* 'clearInterval', */
169
+ /* 'clearTimeout', */
170
+ 'close',
171
+ 'confirm',
172
+ /* 'console', */
173
+ 'createPopup',
174
+ 'dispatchEvent',
175
+ 'document',
176
+ 'focus',
177
+ 'frames',
178
+ 'getComputedStyle',
179
+ 'history',
180
+ 'innerHeight',
181
+ 'innerWidth',
182
+ 'length',
183
+ 'location',
184
+ 'moveBy',
185
+ 'moveTo',
186
+ 'name',
187
+ 'navigator',
188
+ 'open',
189
+ 'outerHeight',
190
+ 'outerWidth',
191
+ 'pageXOffset',
192
+ 'pageYOffset',
193
+ 'parent',
194
+ 'postMessage',
195
+ 'print',
196
+ 'prompt',
197
+ 'removeEventListener',
198
+ 'resizeBy',
199
+ 'resizeTo',
200
+ 'screen',
201
+ 'screenLeft',
202
+ 'screenTop',
203
+ 'screenX',
204
+ 'screenY',
205
+ 'scroll',
206
+ 'scrollBy',
207
+ 'scrollLeft',
208
+ 'scrollTo',
209
+ 'scrollTop',
210
+ 'scrollX',
211
+ 'scrollY',
212
+ 'self',
213
+ /* 'setInterval', */
214
+ /* 'setTimeout', */
215
+ 'stop',
216
+ /* 'toString', */
217
+ 'top',
218
+ 'window',
219
+ ];
220
+ export const KEYS = LIVING_KEYS.concat(OTHER_KEYS);
@@ -1,3 +1,5 @@
1
1
  import sinon from 'sinon';
2
2
  export { sinon };
3
3
  export const { mock, spy, stub } = sinon;
4
+ // @ts-expect-error
5
+ sinon.fn = sinon.spy;
@@ -71,8 +71,10 @@ export class DefaultReporter {
71
71
  await this.listrPromise;
72
72
  this.log();
73
73
  this.end = performance.now();
74
- const failedFiles = files.filter(i => i.error);
74
+ const suites = files.reduce((acc, file) => acc.concat(file.suites), []);
75
75
  const tasks = files.reduce((acc, file) => acc.concat(file.suites.flatMap(i => i.tasks)), []);
76
+ const failedFiles = files.filter(i => i.error);
77
+ const failedSuites = suites.filter(i => i.error);
76
78
  const runable = tasks.filter(i => i.state === 'pass' || i.state === 'fail');
77
79
  const passed = tasks.filter(i => i.state === 'pass');
78
80
  const failed = tasks.filter(i => i.state === 'fail');
@@ -87,6 +89,15 @@ export class DefaultReporter {
87
89
  this.log();
88
90
  });
89
91
  }
92
+ if (failedSuites.length) {
93
+ this.error(c.bold(`\nFailed to run ${failedSuites.length} suites:`));
94
+ failedSuites.forEach((i) => {
95
+ var _a;
96
+ this.error(`\n- ${(_a = i.file) === null || _a === void 0 ? void 0 : _a.filepath} > ${i.name}`);
97
+ console.error(i.error || 'Unknown error');
98
+ this.log();
99
+ });
100
+ }
90
101
  if (failed.length) {
91
102
  this.error(c.bold(`\nFailed Tests (${failed.length})`));
92
103
  failed.forEach((task) => {
package/dist/run.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import { File, Config, Task, RunnerContext } from './types';
1
+ import { File, Config, Task, RunnerContext, Suite } from './types';
2
2
  export declare function runTask(task: Task, ctx: RunnerContext): Promise<void>;
3
3
  export declare function collectFiles(paths: string[]): Promise<File[]>;
4
+ export declare function runSite(suite: Suite, ctx: RunnerContext): Promise<void>;
4
5
  export declare function runFile(file: File, ctx: RunnerContext): Promise<void>;
5
6
  export declare function run(config: Config): Promise<void>;
package/dist/run.js CHANGED
@@ -14,16 +14,24 @@ export async function runTask(task, ctx) {
14
14
  (_a = getSnapshotManager()) === null || _a === void 0 ? void 0 : _a.setTask(task);
15
15
  await ((_b = reporter.onTaskBegin) === null || _b === void 0 ? void 0 : _b.call(reporter, task, ctx));
16
16
  if (task.mode === 'run') {
17
- await callHook(task.suite, 'afterEach', [task, task.suite]);
18
17
  try {
18
+ await callHook(task.suite, 'beforeEach', [task, task.suite]);
19
19
  await task.fn();
20
20
  task.state = 'pass';
21
21
  }
22
22
  catch (e) {
23
23
  task.state = 'fail';
24
24
  task.error = e;
25
+ process.exitCode = 1;
26
+ }
27
+ try {
28
+ await callHook(task.suite, 'afterEach', [task, task.suite]);
29
+ }
30
+ catch (e) {
31
+ task.state = 'fail';
32
+ task.error = e;
33
+ process.exitCode = 1;
25
34
  }
26
- await callHook(task.suite, 'afterEach', [task, task.suite]);
27
35
  }
28
36
  await ((_c = reporter.onTaskEnd) === null || _c === void 0 ? void 0 : _c.call(reporter, task, ctx));
29
37
  }
@@ -75,6 +83,32 @@ function interpretOnlyMode(items) {
75
83
  });
76
84
  }
77
85
  }
86
+ export async function runSite(suite, ctx) {
87
+ var _a, _b;
88
+ const { reporter } = ctx;
89
+ await ((_a = reporter.onSuiteBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, suite, ctx));
90
+ if (suite.mode === 'skip') {
91
+ suite.status = 'skip';
92
+ }
93
+ else if (suite.mode === 'todo') {
94
+ suite.status = 'todo';
95
+ }
96
+ else {
97
+ try {
98
+ await callHook(suite, 'beforeAll', [suite]);
99
+ await Promise.all(suite.tasks.map(i => runTask(i, ctx)));
100
+ // for (const t of suite.tasks)
101
+ // await runTask(t, ctx)
102
+ await callHook(suite, 'afterAll', [suite]);
103
+ }
104
+ catch (e) {
105
+ suite.error = e;
106
+ suite.status = 'fail';
107
+ process.exitCode = 1;
108
+ }
109
+ }
110
+ await ((_b = reporter.onSuiteEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, suite, ctx));
111
+ }
78
112
  export async function runFile(file, ctx) {
79
113
  var _a, _b;
80
114
  const { reporter } = ctx;
@@ -82,17 +116,13 @@ export async function runFile(file, ctx) {
82
116
  if (runableSuites.length === 0)
83
117
  return;
84
118
  await ((_a = reporter.onFileBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, file, ctx));
85
- // TODO: support toggling parallel or serial
86
- await Promise.all(file.suites.map(async (suite) => {
87
- var _a, _b;
88
- await ((_a = reporter.onSuiteBegin) === null || _a === void 0 ? void 0 : _a.call(reporter, suite, ctx));
89
- await callHook(suite, 'beforeAll', [suite]);
90
- await Promise.all(suite.tasks.map(i => runTask(i, ctx)));
91
- // for (const t of suite.tasks)
92
- // await runTask(t, ctx)
93
- await callHook(suite, 'afterAll', [suite]);
94
- await ((_b = reporter.onSuiteEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, suite, ctx));
95
- }));
119
+ if (ctx.config.parallel) {
120
+ await Promise.all(file.suites.map(suite => runSite(suite, ctx)));
121
+ }
122
+ else {
123
+ for (const suite of file.suites)
124
+ await runSite(suite, ctx);
125
+ }
96
126
  await ((_b = reporter.onFileEnd) === null || _b === void 0 ? void 0 : _b.call(reporter, file, ctx));
97
127
  }
98
128
  export async function run(config) {
package/dist/types.d.ts CHANGED
@@ -15,6 +15,12 @@ export interface UserOptions {
15
15
  * @default false
16
16
  */
17
17
  jsdom?: boolean;
18
+ /**
19
+ * Run tests files in parallel
20
+ *
21
+ * @default false
22
+ */
23
+ parallel?: boolean;
18
24
  }
19
25
  export interface Config extends UserOptions {
20
26
  rootDir?: string;
@@ -47,6 +53,8 @@ export interface Suite {
47
53
  mode: RunMode;
48
54
  tasks: Task[];
49
55
  file?: File;
56
+ error?: unknown;
57
+ status?: TaskState;
50
58
  hooks: {
51
59
  beforeAll: HookListener<[Suite]>[];
52
60
  afterAll: HookListener<[Suite]>[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vitest",
3
- "version": "0.0.17",
3
+ "version": "0.0.21",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "homepage": "https://github.com/antfu/vitest#readme",
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './dist/index'