Mocking Modules
#47959
-
Hey! I'm playing around with the new testing and mocking utilities and I'm running into a issue trying to mock a module. I've got something like this in my code: import assert from 'node:assert/strict';
import { test } from 'node:test';
import * as leftPad from 'left-pad';
import codeThatUsesLeftPad from '../index.mjs';
test('test code', async (t) => {
t.mock.method(leftPad, 'foo', (...args) => console.log('foo: ', ...args));
codeThatUsesLeftPad();
}); But I get this error:
Am I doing this wrong? |
Beta Was this translation helpful? Give feedback.
Answered by
cjihrig
May 11, 2023
Replies: 1 comment 4 replies
-
You haven't provided a runnable example, so it's hard to say for sure. However, given the error message, it's possible you're trying to mock a method that is non-configurable: import { test } from 'node:test';
const obj = Object.create(null);
Object.defineProperty(obj, 'foo', { value: function() {} });
test('test code', async (t) => {
t.mock.method(obj, 'foo');
}); I can tell you that mocking of entire modules is not currently implemented. It's something I plan to work on once ESM loaders stabilize a bit more. |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
koddsson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You haven't provided a runnable example, so it's hard to say for sure. However, given the error message, it's possible you're trying to mock a method that is non-configurable:
I can tell you that mocking of entire modules is not currently implemented. It's something I plan to work on once ESM loaders stabilize a bit more.