Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: introduces a new MockTimers API #47775

Merged
Merged
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift click to select a range
568ad96
test_runner: add initial draft for fakeTimers
ErickWendel Apr 28, 2023
ac0fab3
test_runner: move fakerTimers to mock module
ErickWendel May 1, 2023
e54a162
test_runner: change fakeTimers prop name
ErickWendel May 1, 2023
71ffd10
test_runner: implement PriorityQueue for timers
ErickWendel May 1, 2023
a0f8afd
test_runner: add @ljharb suggestion
ErickWendel May 1, 2023
0f55f65
test_runner: add @benjamingr suggestion
ErickWendel May 1, 2023
e34a426
test_runner: add fake setTimeout initial impl
ErickWendel May 1, 2023
df35174
test_runner: add @molow suggestion
ErickWendel May 1, 2023
e5af52f
test_runner: fix a test
ErickWendel May 1, 2023
a22321f
test_runner: rename from fakeTimers to mockTimers
ErickWendel May 3, 2023
21be0bc
test_runner: add experimental warning
ErickWendel May 3, 2023
102a830
test_runner: fix tests
ErickWendel May 3, 2023
1d30e47
test_runner: add @ljharb suggestion
ErickWendel May 3, 2023
0cb4e7c
test_runner: fix possible truthy evaluation on tick by @ljharb
ErickWendel May 3, 2023
a53aedc
test_runner: add tests for clearTimeout
ErickWendel May 3, 2023
1a2cf34
test_runner: add tests for setInterval modules
ErickWendel May 3, 2023
db2a79f
test_runner: add tests for clearInterval modules
ErickWendel May 3, 2023
f200467
test_runner: add impl for timers.promises.setInterval function
ErickWendel May 4, 2023
cc652fe
test_runner: fix setInterval algorithm
ErickWendel May 4, 2023
942f504
test_runner: add abortController support for promises.setTimeout and …
ErickWendel May 14, 2023
bb75287
test_runner: add test cases for abortController and remove listeners …
ErickWendel May 14, 2023
b0baf00
test_runner: change import order
ErickWendel May 15, 2023
92bf115
test_runner: add ArrayPrototypeAt instead of [].at
ErickWendel May 15, 2023
1fe731e
test_runner: return safe asyncIterator
ErickWendel May 15, 2023
08f27ba
test_runner: make promises.setInterval update time during iterations
ErickWendel May 15, 2023
3e39782
test_runner: add __proto__ null while returning the object
ErickWendel May 19, 2023
a563ce9
test_runner: make promises.setInterval work with abortControllers
ErickWendel May 30, 2023
17381f1
test_runner: rm unsafe array iteration and comments
ErickWendel May 30, 2023
3677482
test_runner: avoid avoid code repetition
ErickWendel May 30, 2023
1825426
test_runner: (rollback) avoid avoid code repetition
ErickWendel May 30, 2023
abc7c96
test_runner: rm unecessarly PromiseRejection
ErickWendel May 30, 2023
87b0d36
test_runner: add timers.reset on test postRun
ErickWendel Jun 5, 2023
a001e65
test_runner: add config for choosing timers to use, error handling an…
ErickWendel Jun 5, 2023
d168614
test_runner: implement releaseAllTimers function
ErickWendel Jun 5, 2023
1e09a22
test_runner: reach 100% code coverage
ErickWendel Jun 5, 2023
89b489b
test_runner: rename function to runAll
ErickWendel Jun 6, 2023
2b64a4a
fix flaky test
ErickWendel Jun 8, 2023
a9ed28d
add abort once flag
ErickWendel Jun 8, 2023
b383a1c
check that timeout will only be called when reaches the specified tim…
ErickWendel Jun 8, 2023
ba725f4
fix lint problems
ErickWendel Jun 8, 2023
1bcbe65
test_runner: add initial doc
ErickWendel Jun 12, 2023
7c65e64
test_runner: fix heading
ErickWendel Jun 12, 2023
aaf7070
test_runner: fix text
ErickWendel Jun 12, 2023
ac9f5b6
test_runner: fix links
ErickWendel Jun 12, 2023
13fab2b
test_runner: add docs for timers.tick
ErickWendel Jun 12, 2023
296f5b0
test_runner: add docs for timers.runAll
ErickWendel Jun 12, 2023
a75ddb4
doc rm empty spaces
ErickWendel Jun 12, 2023
1bee321
Update test/parallel/test-runner-mock-timers.js
ErickWendel Jun 13, 2023
bc8fea7
Update lib/internal/test_runner/mock/mock_timers.js
ErickWendel Jun 13, 2023
1bfd7b8
format test.md
ErickWendel Jun 13, 2023
d2c9a1a
add 1ms to tick to fix flakyness
ErickWendel Jun 13, 2023
93950f5
test_runner: add 50ms to .tick so setInterval will not be flaky
ErickWendel Jun 13, 2023
49e8eba
test_runner: revert last commit
ErickWendel Jun 13, 2023
4d3a45c
founde the bug 🐛
MoLow Jun 13, 2023
12772d1
Apply suggestions from code review
ErickWendel Jun 14, 2023
5c9e425
apply suggestions from review
ErickWendel Jun 14, 2023
0424bf5
add info about support for destructuring from modules
ErickWendel Jun 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
format test.md
Signed-off-by: Erick Wendel <[email protected]>
  • Loading branch information
ErickWendel committed Jun 13, 2023
commit 1bfd7b8d6416ad430f6eb292d227496867da1616
31 changes: 25 additions & 6 deletions doc/api/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 514,7 @@ test('spies on an object method', (t) => {
assert.strictEqual(call.this, number);
});
```

### Timers

Mocking timers is a technique commonly used in software testing to simulate and
Expand Down Expand Up @@ -555,6 556,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w
mock.reset();
});
```

```js
const assert = require('node:assert');
const { mock, test } = require('node:test');
Expand Down Expand Up @@ -619,6 621,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w
assert.strictEqual(fn.mock.callCount(), 1);
});
```

## Test reporters

<!-- YAML
Expand Down Expand Up @@ -1514,6 1517,7 @@ The [`MockTracker`][] provides a top level `timers` export
which is a MockTimers instance
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

### `timers.enable([,timers])`
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

<!-- YAML
added:
- REPLACEME
Expand All @@ -1522,9 1526,9 @@ added:
Enables timer mocking for the specified timers.

* `timers` {Array} An optional array containing the timers to mock.
The currently supported timer values are `'setInterval'` and `'setTimeout'`.
If no array is provided, all timers (`'setInterval'`, `'clearInterval'`, `'setTimeout'`,
and `'clearTimeout'`) will be mocked by default.
The currently supported timer values are `'setInterval'` and `'setTimeout'`.
If no array is provided, all timers (`'setInterval'`, `'clearInterval'`, `'setTimeout'`,
and `'clearTimeout'`) will be mocked by default.

**Note:** When you enable mocking for a specific timer, its associated
clear function will also be implicitly mocked.
Expand All @@ -1535,14 1539,15 @@ Example usage:
import { mock } from 'node:test';
mock.timers.enable(['setInterval']);
```

```js
const { mock } = require('node:test');
mock.timers.enable(['setInterval']);
```

The above example enables mocking for the `setInterval` timer and
implicitly mocks the `clearInterval` function. Only the `setInterval`
and `clearInterval` functions from [node:timers](./timers.md),
and `clearInterval` functions from [node:timers](./timers.md),
[node:timers/promises](./timers.md#timers-promises-api), and
`globalThis` will be mocked.

Expand All @@ -1554,6 1559,7 @@ functions from `node:timers`, `node:timers/promises`,
and `globalThis` will be mocked.

### `timers.reset()`

<!-- YAML
added:
- REPLACEME
Expand All @@ -1570,20 1576,23 @@ the test context's `MockTracker`.
import { mock } from 'node:test';
mock.timers.reset();
```

```js
const { mock } = require('node:test');
mock.timers.reset();
```

### `timers.tick(milliseconds)`

<!-- YAML
added:
- REPLACEME
-->

Advances time for all mocked timers.

* `milliseconds` {number} The amount of time, in milliseconds,
to advance the timers.
to advance the timers.

**Note:** This diverges from how `setTimeout` in Node.js behaves and accepts
only positive numbers. In Node.js, `setTimeout` with negative numbers is
Expand Down Expand Up @@ -1612,6 1621,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w
assert.strictEqual(fn.mock.callCount(), 1);
});
```

```js
const assert = require('node:assert');
const { test } = require('node:test');
Expand All @@ -1629,6 1639,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w
assert.strictEqual(fn.mock.callCount(), 1);
});
```

Alternativelly, the `.tick` function can be called many times

```mjs
Expand Down Expand Up @@ -1713,6 1724,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w
assert.strictEqual(fn.mock.callCount(), 0);
});
```

#### Working with Node.js timers modules

Once you enable mocking timers, [node:timers](./timers.md),
Expand Down Expand Up @@ -1745,6 1757,7 @@ test('mocks setTimeout to be executed synchronously without having to actually w
assert.strictEqual(nodeTimerPromiseSpy.mock.callCount(), 1);
});
```

```js
const assert = require('node:assert');
const { test } = require('node:test');
Expand All @@ -1771,8 1784,9 @@ test('mocks setTimeout to be executed synchronously without having to actually w
assert.strictEqual(nodeTimerPromiseSpy.mock.callCount(), 1);
});
```

In Node.js, `setInterval` from [node:timers/promises](./timers.md#timers-promises-api)
is a Async Gerator and is also supported by this API:
is a Async Gerator and is also supported by this API:
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

```mjs
import assert from 'node:assert';
Expand Down Expand Up @@ -1805,6 1819,7 @@ test('should tick five times testing a real use case', async (context) => {
}
});
```

```js
const assert = require('node:assert');
const { test } = require('node:test');
Expand Down Expand Up @@ -1838,14 1853,17 @@ test('should tick five times testing a real use case', async (context) => {
```

### `timers.runAll()`

<!-- YAML
added:
- REPLACEME
-->

Triggers all pending mocked timers immediately.

The example below triggers all pending timers immediately,
causing them to execute without any delay.

```mjs
import assert from 'node:assert';
import { test } from 'node:test';
Expand All @@ -1867,6 1885,7 @@ test('runAll functions following the given order', (context) => {
assert.deepStrictEqual(results, [3, 2, 1]);
});
```

```js
const assert = require('node:assert');
const { test } = require('node:test');
Expand Down
Loading