Skip to content

Commit 6dca9b9

Browse files
committedNov 18, 2022
test_runner: add getter and setter to MockTracker
This commit allows tests in test runner to use the `getter` and `setter` methods as "syntax sugar" for `MockTracker.method` with the `options.getter` or `options.setter` set to true in the options. Refs: #45326 (comment)
1 parent 6f9175d commit 6dca9b9

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
 

‎doc/api/test.md

+17
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,22 @@ test('spies on an object method', (t) => {
995995
});
996996
```
997997

998+
### `mock.getter(object, methodName[, implementation][, options])`
999+
1000+
<!-- YAML
1001+
added: REPLACEME
1002+
-->
1003+
1004+
This function is syntax sugar for [`MockTracker.method`][] with `options.getter` is `true`.
1005+
1006+
### `mock.setter(object, methodName[, implementation][, options])`
1007+
1008+
<!-- YAML
1009+
added: REPLACEME
1010+
-->
1011+
1012+
This function is syntax sugar for [`MockTracker.method`][] with `options.setter` is `true`.
1013+
9981014
### `mock.reset()`
9991015

10001016
<!-- YAML
@@ -1357,6 +1373,7 @@ added:
13571373
[`--test-only`]: cli.md#--test-only
13581374
[`--test`]: cli.md#--test
13591375
[`MockFunctionContext`]: #class-mockfunctioncontext
1376+
[`MockTracker.method`]: #mockmethodobject-methodname-implementation-options
13601377
[`MockTracker`]: #class-mocktracker
13611378
[`SuiteContext`]: #class-suitecontext
13621379
[`TestContext`]: #class-testcontext

‎lib/internal/test_runner/mock.js

+24
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,30 @@ class MockTracker {
202202
return mock;
203203
}
204204

205+
getter(
206+
object,
207+
methodName,
208+
implementation = kDefaultFunction,
209+
options = kEmptyObject
210+
) {
211+
return this.method(object, methodName, implementation, {
212+
getter: true,
213+
...options
214+
});
215+
}
216+
217+
setter(
218+
object,
219+
methodName,
220+
implementation = kDefaultFunction,
221+
options = kEmptyObject
222+
) {
223+
return this.method(object, methodName, implementation, {
224+
setter: true,
225+
...options
226+
});
227+
}
228+
205229
reset() {
206230
this.restoreAll();
207231
this.#mocks = [];

‎test/parallel/test-runner-mocking.js

+63
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,69 @@ test('mocks a setter', (t) => {
534534
assert.strictEqual(obj.prop, 65);
535535
});
536536

537+
test('mocks a getter with syntax sugar', (t) => {
538+
const obj = {
539+
prop: 5,
540+
get method() {
541+
return this.prop;
542+
},
543+
};
544+
545+
function mockMethod() {
546+
return this.prop - 1;
547+
}
548+
const getter = t.mock.getter(obj, 'method', mockMethod);
549+
assert.strictEqual(getter.mock.calls.length, 0);
550+
assert.strictEqual(obj.method, 4);
551+
552+
const call = getter.mock.calls[0];
553+
554+
assert.deepStrictEqual(call.arguments, []);
555+
assert.strictEqual(call.result, 4);
556+
assert.strictEqual(call.target, undefined);
557+
assert.strictEqual(call.this, obj);
558+
559+
assert.strictEqual(getter.mock.restore(), undefined);
560+
assert.strictEqual(obj.method, 5);
561+
});
562+
563+
test('mocks a setter with syntax sugar', (t) => {
564+
const obj = {
565+
prop: 100,
566+
// eslint-disable-next-line accessor-pairs
567+
set method(val) {
568+
this.prop = val;
569+
},
570+
};
571+
572+
function mockMethod(val) {
573+
this.prop = -val;
574+
}
575+
576+
assert.strictEqual(obj.prop, 100);
577+
obj.method = 88;
578+
assert.strictEqual(obj.prop, 88);
579+
580+
const setter = t.mock.setter(obj, 'method', mockMethod);
581+
582+
assert.strictEqual(setter.mock.calls.length, 0);
583+
obj.method = 77;
584+
assert.strictEqual(obj.prop, -77);
585+
assert.strictEqual(setter.mock.calls.length, 1);
586+
587+
const call = setter.mock.calls[0];
588+
589+
assert.deepStrictEqual(call.arguments, [77]);
590+
assert.strictEqual(call.result, undefined);
591+
assert.strictEqual(call.target, undefined);
592+
assert.strictEqual(call.this, obj);
593+
594+
assert.strictEqual(setter.mock.restore(), undefined);
595+
assert.strictEqual(obj.prop, -77);
596+
obj.method = 65;
597+
assert.strictEqual(obj.prop, 65);
598+
});
599+
537600
test('mocked functions match name and length', (t) => {
538601
function getNameAndLength(fn) {
539602
return {

0 commit comments

Comments
 (0)