GeezerVison is showing github.com — test_runner: add mockReturnValue and promise helpers · nodejs/node@7bdf3cb
File treeExpand file treeCollapse file tree Open diff view settings lib/internal/test_runner/mock
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/test.md +83Lines changed: 83 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -2458,6 +2458,89 @@ test('changes a mock behavior once', (t) => { | 2458 | 2458 | }); | 2459 | 2459 | ``` | 2460 | 2460 |
| | 2461 | +### `ctx.mockRejectedValue(reason)` | | 2462 | + | | 2463 | +<!-- YAML | | 2464 | +added: REPLACEME | | 2465 | +--> | | 2466 | + | | 2467 | +* `reason` {any} The value the returned promise rejects with. | | 2468 | + | | 2469 | +Changes the mock's implementation to a function that returns a promise rejected | | 2470 | +with `reason`. This is shorthand for | | 2471 | +`ctx.mockImplementation(() => Promise.reject(reason))`. | | 2472 | + | | 2473 | +```js | | 2474 | +test('mocks a rejected value', async (t) => { | | 2475 | + const fn = t.mock.fn(); | | 2476 | + | | 2477 | + fn.mock.mockRejectedValue(new Error('boom')); | | 2478 | + await assert.rejects(fn(), { message: 'boom' }); | | 2479 | +}); | | 2480 | +``` | | 2481 | + | | 2482 | +### `ctx.mockResolvedValue(value)` | | 2483 | + | | 2484 | +<!-- YAML | | 2485 | +added: REPLACEME | | 2486 | +--> | | 2487 | + | | 2488 | +* `value` {any} The value the returned promise resolves with. | | 2489 | + | | 2490 | +Changes the mock's implementation to a function that returns a promise resolved | | 2491 | +with `value`. This is shorthand for | | 2492 | +`ctx.mockImplementation(() => Promise.resolve(value))`. | | 2493 | + | | 2494 | +```js | | 2495 | +test('mocks a resolved value', async (t) => { | | 2496 | + const fn = t.mock.fn(); | | 2497 | + | | 2498 | + fn.mock.mockResolvedValue(42); | | 2499 | + assert.strictEqual(await fn(), 42); | | 2500 | +}); | | 2501 | +``` | | 2502 | + | | 2503 | +### `ctx.mockReturnValue(value)` | | 2504 | + | | 2505 | +<!-- YAML | | 2506 | +added: REPLACEME | | 2507 | +--> | | 2508 | + | | 2509 | +* `value` {any} The value returned by the mock. | | 2510 | + | | 2511 | +Changes the mock's implementation to a function that returns `value`. This is | | 2512 | +shorthand for `ctx.mockImplementation(() => value)`. | | 2513 | + | | 2514 | +```js | | 2515 | +test('mocks a return value', (t) => { | | 2516 | + const fn = t.mock.fn(); | | 2517 | + | | 2518 | + fn.mock.mockReturnValue(42); | | 2519 | + assert.strictEqual(fn(), 42); | | 2520 | + assert.strictEqual(fn(), 42); | | 2521 | +}); | | 2522 | +``` | | 2523 | + | | 2524 | +### `ctx.mockThrownValue(value)` | | 2525 | + | | 2526 | +<!-- YAML | | 2527 | +added: REPLACEME | | 2528 | +--> | | 2529 | + | | 2530 | +* `value` {any} The value thrown by the mock. | | 2531 | + | | 2532 | +Changes the mock's implementation to a function that throws `value`. This is | | 2533 | +shorthand for `ctx.mockImplementation(() => { throw value; })`. | | 2534 | + | | 2535 | +```js | | 2536 | +test('mocks a thrown value', (t) => { | | 2537 | + const fn = t.mock.fn(); | | 2538 | + | | 2539 | + fn.mock.mockThrownValue(new Error('boom')); | | 2540 | + assert.throws(fn, { message: 'boom' }); | | 2541 | +}); | | 2542 | +``` | | 2543 | + | 2461 | 2544 | ### `ctx.resetCalls()` | 2462 | 2545 |
| 2463 | 2546 | <!-- YAML | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/test_runner/mock/mock.js +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,6 +15,8 @@ const { | 15 | 15 | ObjectKeys, | 16 | 16 | ObjectSetPrototypeOf, | 17 | 17 | ObjectValues, | | 18 | + PromiseReject, | | 19 | + PromiseResolve, | 18 | 20 | Proxy, | 19 | 21 | ReflectApply, | 20 | 22 | ReflectConstruct, | @@ -143,6 +145,38 @@ class MockFunctionContext { | 143 | 145 | this.#mocks.set(call, implementation); | 144 | 146 | } | 145 | 147 |
| | 148 | + /** | | 149 | + * Sets a new implementation that always returns the given value. | | 150 | + * @param {any} value - The value to return. | | 151 | + */ | | 152 | + mockReturnValue(value) { | | 153 | + this.#implementation = function() { return value; }; | | 154 | + } | | 155 | + | | 156 | + /** | | 157 | + * Sets a new implementation that returns a promise resolved with the given value. | | 158 | + * @param {any} value - The value to resolve with. | | 159 | + */ | | 160 | + mockResolvedValue(value) { | | 161 | + this.#implementation = function() { return PromiseResolve(value); }; | | 162 | + } | | 163 | + | | 164 | + /** | | 165 | + * Sets a new implementation that returns a promise rejected with the given reason. | | 166 | + * @param {any} reason - The reason to reject with. | | 167 | + */ | | 168 | + mockRejectedValue(reason) { | | 169 | + this.#implementation = function() { return PromiseReject(reason); }; | | 170 | + } | | 171 | + | | 172 | + /** | | 173 | + * Sets a new implementation that always throws the given value. | | 174 | + * @param {any} value - The value to throw. | | 175 | + */ | | 176 | + mockThrownValue(value) { | | 177 | + this.#implementation = function() { throw value; }; | | 178 | + } | | 179 | + | 146 | 180 | /** | 147 | 181 | * Restores the original function that was mocked. | 148 | 182 | */ | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-runner-mocking.js +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -892,6 +892,52 @@ test('mock implementation can be changed dynamically', (t) => { | 892 | 892 | assert.strictEqual(fn.mock.callCount(), 12); | 893 | 893 | }); | 894 | 894 |
| | 895 | +test('mockReturnValue() sets a fixed return value', (t) => { | | 896 | + const fn = t.mock.fn(common.mustNotCall()); | | 897 | + | | 898 | + fn.mock.mockReturnValue(42); | | 899 | + assert.strictEqual(fn(), 42); | | 900 | + assert.strictEqual(fn(1, 2), 42); | | 901 | + assert.strictEqual(fn.mock.callCount(), 2); | | 902 | + assert.strictEqual(fn.mock.calls[1].result, 42); | | 903 | +}); | | 904 | + | | 905 | +test('mockResolvedValue() returns a resolved promise', async (t) => { | | 906 | + const fn = t.mock.fn(common.mustNotCall()); | | 907 | + | | 908 | + fn.mock.mockResolvedValue(42); | | 909 | + const result = fn(); | | 910 | + assert(result instanceof Promise); | | 911 | + assert.strictEqual(await result, 42); | | 912 | +}); | | 913 | + | | 914 | +test('mockRejectedValue() returns a rejected promise', async (t) => { | | 915 | + const fn = t.mock.fn(common.mustNotCall()); | | 916 | + const error = new Error('boom'); | | 917 | + | | 918 | + fn.mock.mockRejectedValue(error); | | 919 | + await assert.rejects(fn(), error); | | 920 | +}); | | 921 | + | | 922 | +test('mockThrownValue() throws the given value', (t) => { | | 923 | + const fn = t.mock.fn(common.mustNotCall()); | | 924 | + const error = new Error('boom'); | | 925 | + | | 926 | + fn.mock.mockThrownValue(error); | | 927 | + assert.throws(fn, error); | | 928 | + assert.strictEqual(fn.mock.callCount(), 1); | | 929 | + assert.strictEqual(fn.mock.calls[0].error, error); | | 930 | +}); | | 931 | + | | 932 | +test('mockImplementationOnce() takes precedence over mockReturnValue()', (t) => { | | 933 | + const fn = t.mock.fn(); | | 934 | + | | 935 | + fn.mock.mockReturnValue(1); | | 936 | + fn.mock.mockImplementationOnce(() => 2); | | 937 | + assert.strictEqual(fn(), 2); | | 938 | + assert.strictEqual(fn(), 1); | | 939 | +}); | | 940 | + | 895 | 941 | test('local mocks are auto restored after the test finishes', async (t) => { | 896 | 942 | const obj = { | 897 | 943 | foo() {}, | |
|