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

TypeError: Cannot read property 'filename' of undefined #96

Open
JimLynchCodes opened this issue Aug 25, 2019 · 10 comments
Open

TypeError: Cannot read property 'filename' of undefined #96

JimLynchCodes opened this issue Aug 25, 2019 · 10 comments
Assignees

Comments

@JimLynchCodes
Copy link

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

@theKashey
Copy link
Owner

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?

@JimLynchCodes
Copy link
Author

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!

@theKashey
Copy link
Owner

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.

@boneskull
Copy link
Contributor

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.

@iambumblehead
Copy link

iambumblehead commented Jun 6, 2020

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
Copy link

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

@theKashey
Copy link
Owner

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

@mi-mazouz
Copy link

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?

@theKashey
Copy link
Owner

theKashey commented Jun 25, 2020

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

@aqemi
Copy link

aqemi commented Dec 20, 2021

Issue came back with mocha@9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants