qunitx 1.0.4 → 1.1.1

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.
@@ -0,0 +1,31 @@
1
+ import QUnit from '../../vendor/qunit.js';
2
+ export declare const isLocal: boolean;
3
+ export declare const on: (event: string, callback: (...args: unknown[]) => void) => void;
4
+ export declare const test: (name: string, callback: (assert: unknown) => void | Promise<void>) => void;
5
+ export declare const skip: (name: string, callback: (assert: unknown) => void) => void;
6
+ export declare const start: (count?: number) => void;
7
+ export declare const is: (type: string, obj: unknown) => boolean;
8
+ export declare const extend: (target: object, source: object) => object;
9
+ export declare const stack: (offset?: number) => string;
10
+ export declare const onUnhandledRejection: (reason: unknown) => void;
11
+ export declare const assert: unknown;
12
+ export declare const dump: unknown;
13
+ export declare const done: (callback: (...args: unknown[]) => void) => void;
14
+ export declare const testStart: (callback: (...args: unknown[]) => void) => void;
15
+ export declare const moduleStart: (callback: (...args: unknown[]) => void) => void;
16
+ export declare const version: string;
17
+ export declare const module: (name: string, callback: (hooks: unknown) => void) => void;
18
+ export declare const todo: (name: string, callback: (assert: unknown) => void) => void;
19
+ export declare const only: (name: string, callback: (assert: unknown) => void) => void;
20
+ export declare const config: import("../../vendor/qunit.js").QUnitConfig;
21
+ export declare const objectType: (obj: unknown) => string;
22
+ export declare const load: () => void;
23
+ export declare const onError: (callback: (...args: unknown[]) => void) => void;
24
+ export declare const pushFailure: (message: string, source?: string, value?: unknown) => void;
25
+ export declare const equiv: (a: unknown, b: unknown) => boolean;
26
+ export declare const begin: (callback: (...args: unknown[]) => void) => void;
27
+ export declare const log: (callback: (...args: unknown[]) => void) => void;
28
+ export declare const testDone: (callback: (...args: unknown[]) => void) => void;
29
+ export declare const moduleDone: (callback: (...args: unknown[]) => void) => void;
30
+ export declare const diff: (a: unknown, b: unknown) => string;
31
+ export default QUnit;
@@ -1,7 +1,5 @@
1
1
  import QUnit from '../../vendor/qunit.js';
2
-
3
2
  QUnit.config.autostart = false;
4
-
5
3
  export const isLocal = QUnit.isLocal;
6
4
  export const on = QUnit.on;
7
5
  export const test = QUnit.test;
@@ -31,5 +29,4 @@ export const log = QUnit.log;
31
29
  export const testDone = QUnit.testDone;
32
30
  export const moduleDone = QUnit.moduleDone;
33
31
  export const diff = QUnit.diff;
34
-
35
32
  export default QUnit;
@@ -0,0 +1,11 @@
1
+ import Module from './module.js';
2
+ import Test from './test.js';
3
+ export declare const module: typeof Module;
4
+ export declare const test: typeof Test;
5
+ declare const _default: {
6
+ AssertionError: import("../types.js").AssertionErrorConstructor;
7
+ module: typeof Module;
8
+ test: typeof Test;
9
+ config: {};
10
+ };
11
+ export default _default;
@@ -1,22 +1,20 @@
1
+ /// <reference types="node" />
1
2
  import { AssertionError } from 'node:assert';
3
+ import { inspect } from 'node:util';
2
4
  import QUnit from '../../vendor/qunit.js';
3
- import Assert from '../shared/assert.js';
4
- import ModuleContext from '../shared/module-context.js';
5
- import TestContext from '../shared/test-context.js';
6
- import Module from './module.js';
7
- import Test from './test.js';
8
-
5
+ import Assert from "../shared/assert.js";
6
+ import ModuleContext from "../shared/module-context.js";
7
+ import TestContext from "../shared/test-context.js";
8
+ import Module from "./module.js";
9
+ import Test from "./test.js";
9
10
  Assert.QUnit = QUnit;
10
11
  Assert.AssertionError = AssertionError;
11
-
12
+ Assert.inspect = inspect;
12
13
  ModuleContext.Assert = Assert;
13
14
  TestContext.Assert = Assert;
14
-
15
15
  Object.freeze(Assert);
16
16
  Object.freeze(ModuleContext);
17
17
  Object.freeze(TestContext);
18
-
19
18
  export const module = Module;
20
19
  export const test = Test;
21
-
22
20
  export default { AssertionError: Assert.AssertionError, module, test, config: {} };
@@ -0,0 +1,6 @@
1
+ import type Assert from '../shared/assert.js';
2
+ import type { HooksObject } from '../types.js';
3
+ export default function module(moduleName: string, runtimeOptions: object | ((hooks: HooksObject<Assert>) => void), moduleContent?: (hooks: HooksObject<Assert>, meta: {
4
+ moduleName: string;
5
+ options: unknown;
6
+ }) => void): void;
@@ -0,0 +1,49 @@
1
+ import { describe, before as beforeAll, after as afterAll } from 'node:test';
2
+ import ModuleContext from "../shared/module-context.js";
3
+ export default function module(moduleName, runtimeOptions, moduleContent) {
4
+ const targetRuntimeOptions = moduleContent ? runtimeOptions : {};
5
+ const targetModuleContent = (moduleContent ? moduleContent : runtimeOptions);
6
+ const moduleContext = new ModuleContext(moduleName);
7
+ describe(moduleName, { ...targetRuntimeOptions }, function () {
8
+ const beforeHooks = [];
9
+ const afterHooks = [];
10
+ beforeAll(async function () {
11
+ // before() assertions are attributed to the first direct test only (matching QUnit's model).
12
+ // Tests inherit parent context via prototype chain, so no Object.assign needed.
13
+ const firstTest = moduleContext.tests[0];
14
+ const beforeAssert = firstTest ? firstTest.assert : moduleContext.assert;
15
+ for (const hook of beforeHooks) {
16
+ await hook.call(moduleContext.userContext, beforeAssert);
17
+ }
18
+ });
19
+ afterAll(async () => {
20
+ for (const testContext of moduleContext.tests) {
21
+ await testContext.assert.waitForAsyncOps();
22
+ }
23
+ const lastTest = moduleContext.tests[moduleContext.tests.length - 1];
24
+ if (lastTest) {
25
+ for (let j = afterHooks.length - 1; j >= 0; j--) {
26
+ await afterHooks[j].call(lastTest.userContext, lastTest.assert);
27
+ }
28
+ }
29
+ for (let i = 0, len = moduleContext.tests.length; i < len; i++) {
30
+ moduleContext.tests[i].finish();
31
+ }
32
+ });
33
+ targetModuleContent.call(moduleContext.userContext, {
34
+ before(beforeFn) {
35
+ beforeHooks.push(beforeFn);
36
+ },
37
+ beforeEach(beforeEachFn) {
38
+ moduleContext.beforeEachHooks.push(beforeEachFn);
39
+ },
40
+ afterEach(afterEachFn) {
41
+ moduleContext.afterEachHooks.push(afterEachFn);
42
+ },
43
+ after(afterFn) {
44
+ afterHooks.push(afterFn);
45
+ }
46
+ }, { moduleName, options: runtimeOptions });
47
+ ModuleContext.currentModuleChain.pop();
48
+ });
49
+ }
@@ -0,0 +1,8 @@
1
+ import type Assert from '../shared/assert.js';
2
+ export default function test(testName: string, runtimeOptions: object | ((assert: Assert, meta: {
3
+ testName: string;
4
+ options: unknown;
5
+ }) => void | Promise<void>), testContent?: (assert: Assert, meta: {
6
+ testName: string;
7
+ options: unknown;
8
+ }) => void | Promise<void>): void;
@@ -0,0 +1,33 @@
1
+ import { it } from 'node:test';
2
+ import TestContext from "../shared/test-context.js";
3
+ import ModuleContext from "../shared/module-context.js";
4
+ export default function test(testName, runtimeOptions, testContent) {
5
+ const moduleContext = ModuleContext.lastModule;
6
+ if (!moduleContext) {
7
+ throw new Error(`Test '${testName}' called outside of module context.`);
8
+ }
9
+ const targetRuntimeOptions = testContent ? runtimeOptions : {};
10
+ const targetTestContent = (testContent ? testContent : runtimeOptions);
11
+ const context = new TestContext(testName, moduleContext);
12
+ // Each test gets a fresh plain object inheriting from the module's user context.
13
+ // This matches QUnit's prototype-chain model: before() sets props on the module context,
14
+ // tests inherit them, and each test's own writes don't pollute sibling tests.
15
+ const userContext = Object.create(moduleContext.userContext);
16
+ context.userContext = userContext;
17
+ it(testName, { ...targetRuntimeOptions }, async function () {
18
+ for (const module of context.module.moduleChain) {
19
+ for (const hook of module.beforeEachHooks) {
20
+ await hook.call(userContext, context.assert);
21
+ }
22
+ }
23
+ const result = await targetTestContent.call(userContext, context.assert, { testName, options: runtimeOptions });
24
+ await context.assert.waitForAsyncOps();
25
+ for (let i = context.module.moduleChain.length - 1; i >= 0; i--) {
26
+ const module = context.module.moduleChain[i];
27
+ for (let j = module.afterEachHooks.length - 1; j >= 0; j--) {
28
+ await module.afterEachHooks[j].call(userContext, context.assert);
29
+ }
30
+ }
31
+ return result;
32
+ });
33
+ }
@@ -0,0 +1,360 @@
1
+ import '../../vendor/qunit.js';
2
+ import type { QUnitObject, AssertionErrorConstructor, InspectFn, TestState, ModuleState, PushResultInfo } from '../types.js';
3
+ /**
4
+ * The assertion object passed to every test callback and lifecycle hook.
5
+ *
6
+ * Every {@linkcode test} callback receives an instance of `Assert` as its first argument.
7
+ * All assertion methods throw an {@linkcode AssertionError} on failure, which the test
8
+ * runner catches and reports.
9
+ *
10
+ * @example
11
+ * ```js
12
+ * import { module, test } from "qunitx";
13
+ *
14
+ * module("Math", () => {
15
+ * test("addition", (assert) => {
16
+ * assert.equal(1 + 1, 2);
17
+ * assert.strictEqual(typeof 42, "number");
18
+ * });
19
+ * });
20
+ * ```
21
+ */
22
+ export default class Assert {
23
+ /** @internal Set by each runtime shim before tests run. */
24
+ static QUnit: QUnitObject;
25
+ /** @internal Set by each runtime shim before tests run. */
26
+ static AssertionError: AssertionErrorConstructor;
27
+ /** @internal Set by each runtime shim before tests run. */
28
+ static inspect: InspectFn;
29
+ /** @internal Mutable test state written during the test run. */
30
+ test: TestState;
31
+ /** @internal */
32
+ constructor(module: ModuleState | null, test?: TestState);
33
+ /** @internal */
34
+ _incrementAssertionCount(): void;
35
+ /**
36
+ * Sets the number of milliseconds after which the current test will fail if not yet complete.
37
+ *
38
+ * @param {number} number - Timeout in milliseconds (positive integer).
39
+ * @example
40
+ * ```js
41
+ * test("slow async operation", async (assert) => {
42
+ * assert.timeout(500);
43
+ * await somethingAsync();
44
+ * assert.ok(true);
45
+ * });
46
+ * ```
47
+ */
48
+ timeout(number: number): void;
49
+ /**
50
+ * Records a named step. Use with {@linkcode Assert.prototype.verifySteps} to assert that
51
+ * a sequence of steps occurred in the right order.
52
+ *
53
+ * @param {string} message - The step label to record.
54
+ * @example
55
+ * ```js
56
+ * test("event order", (assert) => {
57
+ * assert.expect(3);
58
+ * assert.step("step one");
59
+ * assert.step("step two");
60
+ * assert.verifySteps(["step one", "step two"]);
61
+ * });
62
+ * ```
63
+ */
64
+ step(message: string): void;
65
+ /**
66
+ * Asserts that the steps recorded via {@linkcode Assert.prototype.step} match the given array,
67
+ * then clears the recorded steps.
68
+ *
69
+ * @param {string[]} steps - Expected array of step labels in order.
70
+ * @param {string} [message] - Optional failure message.
71
+ * @example
72
+ * ```js
73
+ * test("lifecycle order", (assert) => {
74
+ * assert.step("init");
75
+ * assert.step("run");
76
+ * assert.verifySteps(["init", "run"]);
77
+ * });
78
+ * ```
79
+ */
80
+ verifySteps(steps: string[], message?: string): void;
81
+ /**
82
+ * Sets the number of assertions expected to run in the current test.
83
+ * The test fails if a different number of assertions actually ran.
84
+ *
85
+ * @param {number} number - Expected assertion count (non-negative integer).
86
+ * @example
87
+ * ```js
88
+ * test("exactly two assertions", (assert) => {
89
+ * assert.expect(2);
90
+ * assert.ok(true);
91
+ * assert.ok(true);
92
+ * });
93
+ * ```
94
+ */
95
+ expect(number: number): void;
96
+ /**
97
+ * Returns a `done` callback for callback-style async tests. The test will not
98
+ * finish until every `done` callback returned by `async()` has been called.
99
+ *
100
+ * For `async/await` tests prefer `async (assert) => { ... }` directly.
101
+ *
102
+ * @returns {function} A callback to invoke when the async work finishes.
103
+ * @example
104
+ * ```js
105
+ * test("async callback style", (assert) => {
106
+ * const done = assert.async();
107
+ * setTimeout(() => {
108
+ * assert.ok(true, "async callback ran");
109
+ * done();
110
+ * }, 10);
111
+ * });
112
+ * ```
113
+ */
114
+ async(): () => void;
115
+ /** @internal Used by the test runner to wait for all async operations to complete. */
116
+ waitForAsyncOps(): Promise<void[]>;
117
+ /**
118
+ * Pushes a custom assertion result. Fails the test if `resultInfo.result` is falsy.
119
+ * Throws an {@linkcode AssertionError} on failure.
120
+ *
121
+ * Useful for building custom assertion helpers.
122
+ *
123
+ * @param {{ result: boolean, actual?: unknown, expected?: unknown, message?: string }} resultInfo
124
+ * @example
125
+ * ```js
126
+ * test("custom assertion", (assert) => {
127
+ * assert.pushResult({
128
+ * result: 1 + 1 === 2,
129
+ * actual: 2,
130
+ * expected: 2,
131
+ * message: "custom math check",
132
+ * });
133
+ * });
134
+ * ```
135
+ */
136
+ pushResult(resultInfo?: PushResultInfo): this;
137
+ /**
138
+ * Asserts that `state` is truthy.
139
+ *
140
+ * @param {unknown} state - The value to test.
141
+ * @param {string} [message] - Optional failure message.
142
+ * @example
143
+ * ```js
144
+ * assert.ok(true);
145
+ * assert.ok(1, "non-zero is truthy");
146
+ * assert.ok("hello");
147
+ * ```
148
+ */
149
+ ok(state: unknown, message?: string): void;
150
+ /**
151
+ * Asserts that `state` is falsy.
152
+ *
153
+ * @param {unknown} state - The value to test.
154
+ * @param {string} [message] - Optional failure message.
155
+ * @example
156
+ * ```js
157
+ * assert.notOk(false);
158
+ * assert.notOk(0, "zero is falsy");
159
+ * assert.notOk(null);
160
+ * ```
161
+ */
162
+ notOk(state: unknown, message?: string): void;
163
+ /**
164
+ * Asserts that `state === true` (strict boolean true).
165
+ *
166
+ * @param {unknown} state - The value to test.
167
+ * @param {string} [message] - Optional failure message.
168
+ * @example
169
+ * ```js
170
+ * assert.true(1 === 1);
171
+ * assert.true(Array.isArray([]), "arrays are arrays");
172
+ * ```
173
+ */
174
+ true(state: unknown, message?: string): void;
175
+ /**
176
+ * Asserts that `state === false` (strict boolean false).
177
+ *
178
+ * @param {unknown} state - The value to test.
179
+ * @param {string} [message] - Optional failure message.
180
+ * @example
181
+ * ```js
182
+ * assert.false(1 === 2);
183
+ * assert.false(Number.isNaN(42), "42 is not NaN");
184
+ * ```
185
+ */
186
+ false(state: unknown, message?: string): void;
187
+ /**
188
+ * Asserts that `actual == expected` (loose equality, allows type coercion).
189
+ *
190
+ * Prefer {@linkcode Assert.prototype.strictEqual} for most comparisons. Use {@linkcode Assert.prototype.notEqual}
191
+ * for the inverse.
192
+ *
193
+ * @param {unknown} actual - The value produced by the code under test.
194
+ * @param {unknown} expected - The expected value.
195
+ * @param {string} [message] - Optional failure message.
196
+ * @example
197
+ * ```js
198
+ * assert.equal(1, 1);
199
+ * assert.equal("1", 1, "loose equality allows coercion");
200
+ * ```
201
+ */
202
+ equal(actual: unknown, expected: unknown, message?: string): void;
203
+ /**
204
+ * Asserts that `actual != expected` (loose inequality). Inverse of {@linkcode Assert.prototype.equal}.
205
+ *
206
+ * @param {unknown} actual - The actual value.
207
+ * @param {unknown} expected - The value it should not loosely equal.
208
+ * @param {string} [message] - Optional failure message.
209
+ * @example
210
+ * ```js
211
+ * assert.notEqual(1, 2);
212
+ * assert.notEqual("hello", "world");
213
+ * ```
214
+ */
215
+ notEqual(actual: unknown, expected: unknown, message?: string): void;
216
+ /**
217
+ * Asserts that `actual` and `expected` have the same own enumerable properties
218
+ * and values. Prototype methods are ignored; only own properties are compared.
219
+ *
220
+ * @param {object} actual - The actual object.
221
+ * @param {object} expected - The expected object.
222
+ * @param {string} [message] - Optional failure message.
223
+ * @example
224
+ * ```js
225
+ * assert.propEqual({ a: 1, b: 2 }, { a: 1, b: 2 });
226
+ *
227
+ * // Ignores prototype methods — only own properties matter:
228
+ * function Point(x, y) { this.x = x; this.y = y; }
229
+ * assert.propEqual(new Point(1, 2), { x: 1, y: 2 });
230
+ * ```
231
+ */
232
+ propEqual(actual: unknown, expected: unknown, message?: string): void;
233
+ /**
234
+ * Asserts that `actual` and `expected` do NOT have the same own enumerable
235
+ * properties and values. Inverse of {@linkcode Assert.prototype.propEqual}.
236
+ *
237
+ * @param {object} actual - The actual object.
238
+ * @param {object} expected - The value it should not propEqual.
239
+ * @param {string} [message] - Optional failure message.
240
+ * @example
241
+ * ```js
242
+ * assert.notPropEqual({ a: 1 }, { a: 2 });
243
+ * assert.notPropEqual({ a: 1, b: 2 }, { a: 1 }); // extra key makes them unequal
244
+ * ```
245
+ */
246
+ notPropEqual(actual: unknown, expected: unknown, message?: string): void;
247
+ /**
248
+ * Asserts that `actual` contains all own enumerable properties from `expected`
249
+ * with matching values. Extra properties on `actual` are allowed and ignored.
250
+ *
251
+ * @param {object} actual - The actual object (may have extra keys).
252
+ * @param {object} expected - The subset of key/value pairs that must be present.
253
+ * @param {string} [message] - Optional failure message.
254
+ * @example
255
+ * ```js
256
+ * assert.propContains({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 });
257
+ * assert.propContains(user, { role: "admin" });
258
+ * ```
259
+ */
260
+ propContains(actual: unknown, expected: unknown, message?: string): void;
261
+ /**
262
+ * Asserts that `actual` does NOT contain all own enumerable properties
263
+ * from `expected` with matching values. Inverse of {@linkcode Assert.prototype.propContains}.
264
+ *
265
+ * @param {object} actual - The actual object.
266
+ * @param {object} expected - The subset of properties that must NOT all match.
267
+ * @param {string} [message] - Optional failure message.
268
+ * @example
269
+ * ```js
270
+ * assert.notPropContains({ a: 1, b: 2 }, { a: 9 });
271
+ * assert.notPropContains(user, { role: "banned" });
272
+ * ```
273
+ */
274
+ notPropContains(actual: unknown, expected: unknown, message?: string): void;
275
+ /**
276
+ * Asserts deep equality between `actual` and `expected` using recursive structural
277
+ * comparison. Handles nested objects, arrays, `Date`, `RegExp`, and more.
278
+ *
279
+ * @param {unknown} actual - The actual value.
280
+ * @param {unknown} expected - The expected value.
281
+ * @param {string} [message] - Optional failure message.
282
+ * @example
283
+ * ```js
284
+ * assert.deepEqual([1, { a: 2 }], [1, { a: 2 }]);
285
+ * assert.deepEqual(new Date("2024-01-01"), new Date("2024-01-01"));
286
+ * ```
287
+ */
288
+ deepEqual(actual: unknown, expected: unknown, message?: string): void;
289
+ /**
290
+ * Asserts that `actual` and `expected` are NOT deeply equal. Inverse of {@linkcode Assert.prototype.deepEqual}.
291
+ *
292
+ * @param {unknown} actual - The actual value.
293
+ * @param {unknown} expected - The value it should not deepEqual.
294
+ * @param {string} [message] - Optional failure message.
295
+ * @example
296
+ * ```js
297
+ * assert.notDeepEqual([1, 2], [1, 3]);
298
+ * assert.notDeepEqual({ a: 1 }, { a: 2 });
299
+ * ```
300
+ */
301
+ notDeepEqual(actual: unknown, expected: unknown, message?: string): void;
302
+ /**
303
+ * Asserts that `actual === expected` (strict equality, no type coercion).
304
+ *
305
+ * @param {unknown} actual - The actual value.
306
+ * @param {unknown} expected - The expected value.
307
+ * @param {string} [message] - Optional failure message.
308
+ * @example
309
+ * ```js
310
+ * assert.strictEqual(1 + 1, 2);
311
+ * assert.strictEqual(typeof "hello", "string");
312
+ * ```
313
+ */
314
+ strictEqual(actual: unknown, expected: unknown, message?: string): void;
315
+ /**
316
+ * Asserts that `actual !== expected` (strict inequality). Inverse of {@linkcode Assert.prototype.strictEqual}.
317
+ *
318
+ * @param {unknown} actual - The actual value.
319
+ * @param {unknown} expected - The value it should not strictly equal.
320
+ * @param {string} [message] - Optional failure message.
321
+ * @example
322
+ * ```js
323
+ * assert.notStrictEqual(1, "1", "different types");
324
+ * assert.notStrictEqual({}, {}, "different object references");
325
+ * ```
326
+ */
327
+ notStrictEqual(actual: unknown, expected: unknown, message?: string): void;
328
+ /**
329
+ * Asserts that `blockFn` throws an exception. Optionally validates the thrown
330
+ * error against a string (message substring), RegExp (message pattern),
331
+ * or constructor (`instanceof` check). For async functions use {@linkcode Assert.prototype.rejects}.
332
+ *
333
+ * @param {function} blockFn - A synchronous function expected to throw.
334
+ * @param {string|RegExp|function} [expected] - Optional matcher for the thrown error.
335
+ * @param {string} [message] - Optional failure message.
336
+ * @example
337
+ * ```js
338
+ * assert.throws(() => { throw new Error("boom"); });
339
+ * assert.throws(() => JSON.parse("{bad}"), SyntaxError);
340
+ * assert.throws(() => { throw new Error("bad input"); }, /bad input/);
341
+ * ```
342
+ */
343
+ throws(blockFn: unknown, expectedInput?: unknown, assertionMessage?: string): void;
344
+ /**
345
+ * Asserts that a promise rejects. Optionally validates the rejection reason
346
+ * against a string (message substring), RegExp (message pattern),
347
+ * or constructor (`instanceof` check). For synchronous throws use {@linkcode Assert.prototype.throws}.
348
+ *
349
+ * @param {Promise<unknown>} promise - A promise expected to reject.
350
+ * @param {string|RegExp|function} [expected] - Optional matcher for the rejection reason.
351
+ * @param {string} [message] - Optional failure message.
352
+ * @example
353
+ * ```js
354
+ * await assert.rejects(Promise.reject(new Error("oops")));
355
+ * await assert.rejects(fetch("/bad-url"), TypeError);
356
+ * await assert.rejects(Promise.reject(new Error("timeout")), /timeout/);
357
+ * ```
358
+ */
359
+ rejects(promise: unknown, expectedInput?: unknown, assertionMessage?: string): Promise<void>;
360
+ }