rewiremock
rewiremock copied to clipboard
TypeError: Cannot read property 'filename' of undefined
To repro:
-
install rewiremock
-
put this is any js file or test:
const rewiremock = require('rewiremock/node');
rewiremock.overrideEntryPoint(module);
Throws error:
TypeError: Cannot read property 'filename' of undefined
at getModuleName (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/module.js:99:24)
at Function.overrideEntryPoint (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/index.js:56:50)
at Function.overrideEntryPoint (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/index.js:57:18)
at Function.overrideEntryPoint (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/index.js:57:18)
at Function.overrideEntryPoint (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/index.js:57:18)
at Function.overrideEntryPoint (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/index.js:57:18)
at Function.overrideEntryPoint (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/rewiremock/lib/index.js:57:18)
at Object.
rewiremock.overrideEntryPoint(module); <-- module.parent is undefined
I am almost 100% sure that you dont have to call overrideEntryPoint - it shall be used only you are wrapping rewiremock with your own helper, ie putting it in another module, you are doing to import from another place, thus being a parent of that module. Are you?
Hey thanks. :)
No, I'm not wrapping rewiremock. I just put that line in because in the readme it said "this is important" so I just figured it was really important. Interestingly, when I switch to rewiremock version 3.12.3 it does work even with the "overrideEntryPoint" line.
Also, it's not very clear wat this means, "I would recommend not to importing rewiremock directly from tests, but create a rewiremock.js file and require it - that way, you can preconfigure rewiremock for all tests."
Huh?? Require it where? In app.js? In any random test? In the config of something? Imo it would be nice to have a clearer recommended spot to put this setup code.
Other than that it's a pretty nice lib. 😁 thanks!
Create helpers/rewiremock.js and import it from tests, instead of import ... 'rewiremock' - thus you will have full control on it, and how it would work best for you.
And it this case you have to overrideEntryPoint - rewiremock expects all URLS to be defined relative to the current 'test'.
However, if you are using rewiremock.proxy(() => require(guided mocks) - that's does not matter.
I was running into this problem too, and was indeed confused by it.
Tried to do as directed in the README, but it didn't work due to the exception.
Then tried switching over to guided mocks, but had to change too much stuff, so gave up.
May want to change some of the wording there, or make it less prominent, or add a FAQ section, or something.
This is an example from my sources that ended up resolving the issue for me and is the sort of example that would benefit the README. I am using ava with the esm module. The test files are common js and the files being tested are es6
kpwe_activetabmsg.spec.js
const rewiremock = require('rewiremock/node');
test('getactivetab, should return active tab', t => {
const kpwe_activetabmsg = rewiremock.proxy('../src/kpwe_activetabmsg.js', {
'../src/kpwe_webapi.js': {
tabs: {
query: (opts, fn) => params.push([ opts, fn ])
}
}
}).default;
kpwe_activetabmsg.getactivetab(() => {});
t.deepEqual( params[0][0], {
active : true,
currentWindow : true
});
});
rewiremock is required and used without ceremony. The string '../src/kpwe_activetabmsg.js' is the relative path to that file from the test and not the source file. The source file imports kpwe_activetabmsg using this different string path...
kpwe_activetabmsg.js
import kpwe_webapi from './kpwe_webapi.js';
@theKashey this is package is a terrific help to me and there is nothing else out there that handles this situation for me. This package should be more well-known. The README would benefit from editing/simplification. Humbly, thank you.
Thanks for the kind words @iambumblehead. But I still haven't learned how to write clear and simple READMEs 😭
hey I can't solve this problem! Same error with
helper/rewiremock.js
const rewiremock = require('rewiremock/node');
rewiremock.overrideEntryPoint(module);
module.exports = rewiremock;
test.js
rewiremock(() => require('dependency')).with(stub);
rewiremock.enable();
....
rewiremock.disable();
Someone can help me?
'rewiremock/node' got 💩 - by flattening ESM exports to commonjs it was poisoning overrideEntryPoint, making it circular. In a few iterations it was hitting top level module, which does not have a parent, resulting this error.
patch version released - v3.14.3 working example - https://github.com/theKashey/rewiremock-simple-example
@mi-mazouz - thank you for spotting the problem.
Issue came back with mocha@9