rewiremock icon indicating copy to clipboard operation
rewiremock copied to clipboard

TypeError: Cannot read property 'filename' of undefined

Open JimLynchCodes opened this issue 6 years ago • 10 comments

To repro:

  1. install rewiremock

  2. 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. (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/routes/users.test.js:5:12) at Module._compile (module.js:660:30) at Object.Module._extensions..js (module.js:671:10) at Module.load (module.js:573:32) at tryModuleLoad (module.js:513:12) at Function.Module._load (module.js:505:3) at Module.require (module.js:604:17) at require (internal/module.js:11:18) at /Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/mocha.js:334:36 at Array.forEach () at Mocha.loadFiles (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/mocha.js:331:14) at Mocha.run (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/mocha.js:811:10) at loadAndRun (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/cli/watch-run.js:46:22) at module.exports (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/cli/watch-run.js:61:3) at exports.runMocha (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/cli/run-helpers.js:140:5) at Object.exports.handler.argv [as handler] (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/cli/run.js:292:3) at Object.runCommand (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/yargs/lib/command.js:242:26) at Object.parseArgs [as _parseArgs] (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/yargs/yargs.js:1087:28) at Object.parse (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/yargs/yargs.js:566:25) at Object.exports.main (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/lib/cli/cli.js:68:6) at Object. (/Users/loaner/Git-Projects/NodeJS-Mocking-Deps-ofDeps-Example/node_modules/mocha/bin/mocha:154:29) at Module._compile (module.js:660:30) at Object.Module._extensions..js (module.js:671:10) at Module.load (module.js:573:32) at tryModuleLoad (module.js:513:12) at Function.Module._load (module.js:505:3) at Function.Module.runMain (module.js:701:10) at startup (bootstrap_node.js:193:16) at bootstrap_node.js:617:3

JimLynchCodes avatar Aug 25 '19 19:08 JimLynchCodes

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?

theKashey avatar Aug 25 '19 22:08 theKashey

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!

JimLynchCodes avatar Aug 26 '19 00:08 JimLynchCodes

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.

theKashey avatar Aug 26 '19 01:08 theKashey

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.

boneskull avatar May 21 '20 21:05 boneskull

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';

iambumblehead avatar Jun 06 '20 02:06 iambumblehead

@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.

iambumblehead avatar Jun 06 '20 02:06 iambumblehead

Thanks for the kind words @iambumblehead. But I still haven't learned how to write clear and simple READMEs 😭

theKashey avatar Jun 06 '20 08:06 theKashey

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?

mi-mazouz avatar Jun 24 '20 16:06 mi-mazouz

'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.

theKashey avatar Jun 25 '20 01:06 theKashey

Issue came back with mocha@9

aqemi avatar Dec 20 '21 17:12 aqemi