-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest-mock-test.js
More file actions
46 lines (37 loc) · 1.21 KB
/
jest-mock-test.js
File metadata and controls
46 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const assert = require('node:assert/strict');
const { fn } = require('jest-mock');
// Create a mock function and call it
const mock = fn();
assert.equal(typeof mock, 'function');
mock('hello');
mock('world');
// Track calls
assert.equal(mock.mock.calls.length, 2);
assert.deepEqual(mock.mock.calls[0], ['hello']);
assert.deepEqual(mock.mock.calls[1], ['world']);
// Set return value
const mockWithReturn = fn();
mockWithReturn.mockReturnValue(42);
assert.equal(mockWithReturn(), 42);
assert.equal(mockWithReturn(), 42);
// mockImplementation
const mockImpl = fn();
mockImpl.mockImplementation((x) => x * 2);
assert.equal(mockImpl(5), 10);
assert.equal(mockImpl(3), 6);
assert.equal(mockImpl.mock.calls.length, 2);
// mockReset clears calls and return values
const mockReset = fn();
mockReset.mockReturnValue('before');
assert.equal(mockReset(), 'before');
mockReset.mockReset();
assert.equal(mockReset(), undefined);
assert.equal(mockReset.mock.calls.length, 1);
// mockReturnValueOnce
const mockOnce = fn();
mockOnce.mockReturnValueOnce('first').mockReturnValueOnce('second');
assert.equal(mockOnce(), 'first');
assert.equal(mockOnce(), 'second');
assert.equal(mockOnce(), undefined);
console.log('jest-mock-test:ok');