Skip to content

Commit

Permalink
test_runner: add getter and setter to MockTracker
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
fossamagna committed Nov 18, 2022
1 parent 6f9175d commit 04a27b4
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/api/test.md
Expand Up @@ -995,6 +995,23 @@ test('spies on an object method', (t) => {
});
```

### `mock.getter(object, methodName[, implementation][, options])`

<!-- YAML
added: REPLACEME
-->

This function is sugar syntax for [`MockTracker.method()`][] with `options.getter` is `true`.

### `mock.setter(object, methodName[, implementation][, options])`

<!-- YAML
added: REPLACEME
-->

This function is sugar syntax for [`MockTracker.method()`][] with `options.setter` is `true`.


### `mock.reset()`

<!-- YAML
Expand Down
24 changes: 24 additions & 0 deletions lib/internal/test_runner/mock.js
Expand Up @@ -202,6 +202,30 @@ class MockTracker {
return mock;
}

getter(
object,
methodName,
implementation = kDefaultFunction,
options = kEmptyObject
) {
return this.method(object, methodName, implementation, {
getter: true,
times: options.times
});
}

setter(
object,
methodName,
implementation = kDefaultFunction,
options = kEmptyObject
) {
return this.method(object, methodName, implementation, {
setter: true,
times: options.times
});
}

reset() {
this.restoreAll();
this.#mocks = [];
Expand Down
63 changes: 63 additions & 0 deletions test/parallel/test-runner-mocking.js
Expand Up @@ -534,6 +534,69 @@ test('mocks a setter', (t) => {
assert.strictEqual(obj.prop, 65);
});

test('mocks a getter with syntax sugar', (t) => {
const obj = {
prop: 5,
get method() {
return this.prop;
},
};

function mockMethod() {
return this.prop - 1;
}
const getter = t.mock.getter(obj, 'method', mockMethod);
assert.strictEqual(getter.mock.calls.length, 0);
assert.strictEqual(obj.method, 4);

const call = getter.mock.calls[0];

assert.deepStrictEqual(call.arguments, []);
assert.strictEqual(call.result, 4);
assert.strictEqual(call.target, undefined);
assert.strictEqual(call.this, obj);

assert.strictEqual(getter.mock.restore(), undefined);
assert.strictEqual(obj.method, 5);
});

test('mocks a setter with syntax sugar', (t) => {
const obj = {
prop: 100,
// eslint-disable-next-line accessor-pairs
set method(val) {
this.prop = val;
},
};

function mockMethod(val) {
this.prop = -val;
}

assert.strictEqual(obj.prop, 100);
obj.method = 88;
assert.strictEqual(obj.prop, 88);

const setter = t.mock.setter(obj, 'method', mockMethod);

assert.strictEqual(setter.mock.calls.length, 0);
obj.method = 77;
assert.strictEqual(obj.prop, -77);
assert.strictEqual(setter.mock.calls.length, 1);

const call = setter.mock.calls[0];

assert.deepStrictEqual(call.arguments, [77]);
assert.strictEqual(call.result, undefined);
assert.strictEqual(call.target, undefined);
assert.strictEqual(call.this, obj);

assert.strictEqual(setter.mock.restore(), undefined);
assert.strictEqual(obj.prop, -77);
obj.method = 65;
assert.strictEqual(obj.prop, 65);
});

test('mocked functions match name and length', (t) => {
function getNameAndLength(fn) {
return {
Expand Down

0 comments on commit 04a27b4

Please sign in to comment.