Skip to content

Commit 9c48849

Browse files
committed
02.02 (spies/mocks) and 02.03 (time mocking)
1 parent a2b0e17 commit 9c48849

12 files changed

Lines changed: 133 additions & 87 deletions

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
matrix:
1111
# node-version: [lts/*, 21.x]
12-
node-version: [lts/*]
12+
node-version: [21.x]
1313

1414
steps:
1515
- uses: actions/checkout@v4

src/02.02-to-be-called.test.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/02.02-to-have-been-called.test.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/02.03-date-now-spy.test.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/02.03-date-now-stub.test.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/02.03-mock-date-class.test.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/02.03-spy-date-constructor.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
const myObject = {
4+
doSomething() {
5+
console.log("does something");
6+
},
7+
};
8+
9+
test("stub .callCount()", () => {
10+
const stub = mock.fn();
11+
stub();
12+
assert.equal(stub.mock.callCount(), 1);
13+
});
14+
test("spyOn .callCount()", () => {
15+
const somethingSpy = mock.method(myObject, "doSomething");
16+
myObject.doSomething();
17+
assert.equal(somethingSpy.mock.callCount(), 1);
18+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test, mock, afterEach } from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
const isNode20OrLower = process.versions.node.split(".")[0] <= "20";
5+
6+
const getCurrentDate = () => new Date();
7+
8+
afterEach(() => {
9+
mock.timers.reset();
10+
});
11+
12+
test(
13+
"mock.timers can mock Date and be reset",
14+
{ skip: isNode20OrLower },
15+
(t) => {
16+
t.mock.timers.enable({
17+
// @ts-ignore
18+
apis: ["Date"],
19+
now: new Date("2023-05-14T11:01:58.135Z"),
20+
});
21+
22+
assert.deepEqual(getCurrentDate(), new Date("2023-05-14T11:01:58.135Z"));
23+
24+
t.mock.timers.reset();
25+
assert(new Date().getFullYear() > 2023);
26+
},
27+
);
28+
29+
test("mock.timers can mock Date.now()", { skip: isNode20OrLower }, (t) => {
30+
t.mock.timers.enable({
31+
// @ts-ignore
32+
apis: ["Date"],
33+
now: new Date("2023-05-14T11:01:58.135Z"),
34+
});
35+
36+
assert.deepEqual(Date.now(), 1684062118135);
37+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { test, mock } from "node:test";
2+
import assert from "node:assert/strict";
3+
4+
const getNow = () => new Date(Date.now());
5+
6+
test("It should create correct now Date", () => {
7+
mock
8+
.method(global.Date, "now")
9+
.mock.mockImplementationOnce(() =>
10+
new Date("2023-05-14T11:01:58.135Z").valueOf(),
11+
);
12+
13+
assert.deepEqual(getNow(), new Date("2023-05-14T11:01:58.135Z"));
14+
});

0 commit comments

Comments
 (0)