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

Object.defineProperty called on non-object with mocha #56

Closed
MitMaro opened this issue Apr 20, 2015 · 6 comments
Closed

Object.defineProperty called on non-object with mocha #56

MitMaro opened this issue Apr 20, 2015 · 6 comments

Comments

@MitMaro
Copy link

MitMaro commented Apr 20, 2015

Description

I believe that may be related to #51 given it produces the same error.

When using mocha and rewire with a module that exports a named function directly, an error occurs:

TypeError: Object.defineProperty called on non-object

Running without mocha and using node directly works correctly.

Changing from a named function to assigning the function (see test files below) and exporting the variable instead works.

Example

var foo = function () {};
module.exports = foo;

Test Files

index.js

var rewire = require('rewire');
var test = rewire('./test');

test.js

function run() {}
module.exports = run;

Gist

https://gist.github.com/MitMaro/eb09a24817bcc58f9421

Running

npm install
mocha index.js

Stacktrace

/Users/mitmaro/Code/rewire.mocha.bug/test.js:4
Object.defineProperty(module.exports, '__get__', {enumerable: false, value: fu
       ^
TypeError: Object.defineProperty called on non-object   
    at Function.defineProperty (native)
    at Object.<anonymous> (/Users/mitmaro/Code/rewire.mocha.bug/test.js:4:8)
    at Module._compile (module.js:446:26)
    at Object.Module._extensions..js (module.js:464:10)
    at Module.load (module.js:341:32)
    at Object.load (/Users/mitmaro/Code/rewire.mocha.bug/node_modules/rewire/lib/moduleEnv.js:19:18)
    at internalRewire (/Users/mitmaro/Code/rewire.mocha.bug/node_modules/rewire/lib/rewire.js:50:15)
    at rewire (/Users/mitmaro/Code/rewire.mocha.bug/node_modules/rewire/lib/index.js:11:12)
    at Object.<anonymous> (/Users/mitmaro/Code/rewire.mocha.bug/index.js:2:12)
    at Module._compile (module.js:446:26)
    at Object.Module._extensions..js (module.js:464:10)
    at Module.load (module.js:341:32)
    at Function.Module._load (module.js:296:12)
    at Module.require (module.js:351:17)
    at require (module.js:370:17)
    at /Users/mitmaro/Code/rewire.mocha.bug/node_modules/mocha/lib/mocha.js:192:27
    at Array.forEach (native)
    at Mocha.loadFiles (/Users/mitmaro/Code/rewire.mocha.bug/node_modules/mocha/lib/mocha.js:189:14)
    at Mocha.run (/Users/mitmaro/Code/rewire.mocha.bug/node_modules/mocha/lib/mocha.js:422:31)
    at Object.<anonymous> (/Users/mitmaro/Code/rewire.mocha.bug/node_modules/mocha/bin/_mocha:398:16)
    at Module._compile (module.js:446:26)
    at Object.Module._extensions..js (module.js:464:10)
    at Module.load (module.js:341:32)
    at Function.Module._load (module.js:296:12)
    at Function.Module.runMain (module.js:487:10)
    at startup (node.js:111:16)
    at node.js:799:3
@MitMaro
Copy link
Author

MitMaro commented Apr 23, 2015

I noticed the issue I had linked was the incorrect one. It's been fixed.

@jhnns
Copy link
Owner

jhnns commented Apr 27, 2015

Thx for the detailed report. Unfortunately I can't reproduce the error. After downloading the gist and running mocha test.js everything is just fine.

Pecorino:eb09a24817bcc58f9421 jhnns$ mocha test.js 

  0 passing (2ms)

@MitMaro
Copy link
Author

MitMaro commented Apr 27, 2015

Little mistake in the command, should be mocha index.js not mocha test.js.

The node version case it is useful:

node --version
v0.10.38

If I get a chance this week I will try to debug the issue.

@MitMaro
Copy link
Author

MitMaro commented Apr 27, 2015

Did a quick look and it is because of the function name. global.run is a global that seems to be added by mocha but is undefined.

The getImportGlobalSrc.js file adds var run = global.run; which would hide the function run() {} in my test case.

I don't know if there is a way to alert the user of this, or perhaps it can just be documented - if it's not already.

edit: Added info on source of run global

@MitMaro
Copy link
Author

MitMaro commented Apr 27, 2015

After looking into the source a little more I can't see a way to warn a user if a local variable is being overwritten by a injected global without using some sort of JavaScript parser.

@jhnns jhnns closed this as completed in 839e62b Apr 27, 2015
@jhnns
Copy link
Owner

jhnns commented Apr 27, 2015

Thx for the detailed research. I was able to fix that problem by wrapping the source code inside an IIFE.

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

2 participants