Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test_runner: fix mock.method to support class instances #45608

Merged
merged 19 commits into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/internal/test_runner/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
FunctionPrototypeCall,
ObjectDefineProperty,
ObjectGetOwnPropertyDescriptor,
ObjectGetPrototypeOf,
Proxy,
ReflectApply,
ReflectConstruct,
Expand Down Expand Up @@ -126,13 +127,15 @@ class MockTracker {
}

method(
object,
objectOrFunction,
methodName,
implementation = kDefaultFunction,
options = kEmptyObject,
) {
validateObject(object, 'object');
validateStringOrSymbol(methodName, 'methodName');
if (typeof objectOrFunction !== 'function') {
validateObject(objectOrFunction, 'object');
}

if (implementation !== null && typeof implementation === 'object') {
options = implementation;
Expand All @@ -157,8 +160,8 @@ class MockTracker {
'options.setter', setter, "cannot be used with 'options.getter'"
);
}
const descriptor = findMethodOnPrototypeChain(objectOrFunction, methodName);
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

const descriptor = ObjectGetOwnPropertyDescriptor(object, methodName);
let original;

if (getter) {
Expand All @@ -175,7 +178,7 @@ class MockTracker {
);
}

const restore = { descriptor, object, methodName };
const restore = { descriptor, object: objectOrFunction, methodName };
const impl = implementation === kDefaultFunction ?
original : implementation;
const ctx = new MockFunctionContext(impl, restore, times);
Expand All @@ -197,7 +200,7 @@ class MockTracker {
mockDescriptor.value = mock;
}

ObjectDefineProperty(object, methodName, mockDescriptor);
ObjectDefineProperty(objectOrFunction, methodName, mockDescriptor);

return mock;
}
Expand Down Expand Up @@ -346,4 +349,21 @@ function validateTimes(value, name) {
validateInteger(value, name, 1);
}

function findMethodOnPrototypeChain(instance, methodName) {
let host = instance;
let descriptor;

while (host !== null) {
descriptor = ObjectGetOwnPropertyDescriptor(host, methodName);

if (descriptor) {
break;
}

host = ObjectGetPrototypeOf(host);
}

return descriptor;
}

module.exports = { MockTracker };
152 changes: 151 additions & 1 deletion test/parallel/test-runner-mocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const common = require('../common');
const assert = require('node:assert');
const { mock, test } = require('node:test');

test('spies on a function', (t) => {
const sum = t.mock.fn((arg1, arg2) => {
return arg1 + arg2;
Expand Down Expand Up @@ -319,6 +318,157 @@ test('spy functions can be bound', (t) => {
assert.strictEqual(sum.bind(0)(2, 11), 13);
});

test('mocks prototype methods on an instance', async (t) => {
class Runner {
async someTask(msg) {
return Promise.resolve(msg);
}

async method(msg) {
await this.someTask(msg);
return msg;
}
}
const msg = 'ok';
const obj = new Runner();
assert.strictEqual(await obj.method(msg), msg);
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

t.mock.method(obj, obj.someTask.name);
assert.strictEqual(obj.someTask.mock.calls.length, 0);

assert.strictEqual(await obj.method(msg), msg);

const call = obj.someTask.mock.calls[0];

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

const obj2 = new Runner();
// Ensure that a brand new instance is not mocked
assert.strictEqual(
obj2.someTask.mock,
undefined
);

assert.strictEqual(obj.someTask.mock.restore(), undefined);
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(await obj.method(msg), msg);
assert.strictEqual(obj.someTask.mock, undefined);
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(Runner.prototype.someTask.mock, undefined);
});

test('spies on async static class methods', async (t) => {
class Runner {
static async someTask(msg) {
return Promise.resolve(msg);
}

static async method(msg) {
await this.someTask(msg);
return msg;
}
}
const msg = 'ok';
assert.strictEqual(await Runner.method(msg), msg);

t.mock.method(Runner, Runner.someTask.name);
assert.strictEqual(Runner.someTask.mock.calls.length, 0);

assert.strictEqual(await Runner.method(msg), msg);

const call = Runner.someTask.mock.calls[0];

assert.deepStrictEqual(call.arguments, [msg]);
assert.strictEqual(await call.result, msg);
assert.strictEqual(call.target, undefined);
assert.strictEqual(call.this, Runner);

assert.strictEqual(Runner.someTask.mock.restore(), undefined);
assert.strictEqual(await Runner.method(msg), msg);
assert.strictEqual(Runner.someTask.mock, undefined);
assert.strictEqual(Runner.prototype.someTask, undefined);

});

test('given null to a mock.method it throws a invalid argument error', (t) => {
assert.throws(() => t.mock.method(null, {}), { code: 'ERR_INVALID_ARG_TYPE' });
});

test('it should throw given an inexistent property on a object instance', (t) => {
assert.throws(() => t.mock.method({ abc: 0 }, 'non-existent'), {
code: 'ERR_INVALID_ARG_VALUE'
});
});

test('spy functions can be used on classes inheritance', (t) => {
// Makes sure that having a null-prototype doesn't throw our system off
class A extends null {
static someTask(msg) {
return msg;
}
static method(msg) {
return this.someTask(msg);
}
}
class B extends A {}
class C extends B {}

const msg = 'ok';
assert.strictEqual(C.method(msg), msg);

t.mock.method(C, C.someTask.name);
assert.strictEqual(C.someTask.mock.calls.length, 0);

assert.strictEqual(C.method(msg), msg);

const call = C.someTask.mock.calls[0];

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

assert.strictEqual(C.someTask.mock.restore(), undefined);
assert.strictEqual(C.method(msg), msg);
assert.strictEqual(C.someTask.mock, undefined);
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved
});

test('spy functions don\'t affect the prototype chain', (t) => {

class A {
static someTask(msg) {
return msg;
}
}
class B extends A {}
class C extends B {}

const msg = 'ok';

const ABeforeMockIsUnchanged = Object.getOwnPropertyDescriptor(A, A.someTask.name);
const BBeforeMockIsUnchanged = Object.getOwnPropertyDescriptor(B, B.someTask.name);
const CBeforeMockShouldNotHaveDesc = Object.getOwnPropertyDescriptor(C, C.someTask.name);
ErickWendel marked this conversation as resolved.
Show resolved Hide resolved

t.mock.method(C, C.someTask.name);
C.someTask(msg);
const BAfterMockIsUnchanged = Object.getOwnPropertyDescriptor(B, B.someTask.name);

const AAfterMockIsUnchanged = Object.getOwnPropertyDescriptor(A, A.someTask.name);
const CAfterMockHasDescriptor = Object.getOwnPropertyDescriptor(C, C.someTask.name);

assert.strictEqual(CBeforeMockShouldNotHaveDesc, undefined);
assert.ok(CAfterMockHasDescriptor);

assert.deepStrictEqual(ABeforeMockIsUnchanged, AAfterMockIsUnchanged);
assert.strictEqual(BBeforeMockIsUnchanged, BAfterMockIsUnchanged);
assert.strictEqual(BBeforeMockIsUnchanged, undefined);

assert.strictEqual(C.someTask.mock.restore(), undefined);
const CAfterRestoreKeepsDescriptor = Object.getOwnPropertyDescriptor(C, C.someTask.name);
assert.ok(CAfterRestoreKeepsDescriptor);
});

test('mocked functions report thrown errors', (t) => {
const testError = new Error('test error');
const fn = t.mock.fn(() => {
Expand Down