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

Stubbing fs.readFileSync fails since 1.8.1 #458

Closed
rprieto opened this issue Apr 15, 2014 · 7 comments
Closed

Stubbing fs.readFileSync fails since 1.8.1 #458

rprieto opened this issue Apr 15, 2014 · 7 comments

Comments

@rprieto
Copy link

rprieto commented Apr 15, 2014

In Sinon 1.7.0, the following works as expected:

sinon.stub(fs, 'readFileSync');

Later versions seem to throw:

TypeError: Cannot call method 'charCodeAt' of undefined

Unfortunately I can't seem to get a real stack trace for the error. Stubbing fs.readFile (not sync) works fine, and I'm running Node v0.10.25.

mroderick added a commit to mroderick/sinon that referenced this issue Jul 31, 2014
This pull request closes issue sinonjs#458 by implementing a test that shows that it is possible to stub fs.readFileSync. I've run the tests on node 0.10.25 and 0.10.29 and cannot reproduce the bug.
mroderick added a commit to mroderick/sinon that referenced this issue Jul 31, 2014
This pull request closes issue sinonjs#458 by implementing a test that shows that it is possible to stub fs.readFileSync. I've run the tests on node 0.10.25 and 0.10.29 and cannot reproduce the bug.
@mantoni mantoni closed this as completed in 928cf6c Aug 1, 2014
mantoni added a commit that referenced this issue Aug 1, 2014
Close #458 by proving that there is no failure
@rprieto
Copy link
Author

rprieto commented Aug 1, 2014

Thanks for the test case.... I'll check again on my side to see if I can still reproduce it.

@mjashanks
Copy link

I can recreate this everytime - it is specific to coffee script....

fs = require("fs")
sinon = require("sinon")

sinon.stub(fs, "readFileSync")

crashes with coffee, but not node.

CoffeeScript v1.7.1
Sinon v1.10.3

Stack trace...

TypeError: Cannot call method 'charCodeAt' of undefined
at Object.exports.compileFile (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\node_modules\coffee-script\lib\coffee-script\coffee-script.js:207:20)
at getSourceMap (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\node_modules\coffee-script\lib\coffee-script\coffee-script.js:314:22)
at getSourceMapping (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\node_modules\coffee-script\lib\coffee-script\coffee-script.js:322:19)
at formatSourcePosition (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\node_modules\coffee-script\lib\coffee-script\coffee-script.js:274:16)
at C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\node_modules\coffee-script\lib\coffee-script\coffee-script.js:340:34
at Function.Error.prepareStackTrace (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\node_modules\coffee-script\lib\coffee-script\coffee-script.js:343:7)
at [object Object].jasmine.Spec.fail (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:2405:22)
at [object Object].jasmine.Block.execute (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:1147:17)
at [object Object].jasmine.Queue.next
(C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:2177:31)
at [object Object]._onTimeout (C:\Users\Michael\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:2167:18)

@roc
Copy link

roc commented Sep 8, 2016

I also get this issue, in javascript... what should I add to usefully prove this?

@fearphage
Copy link
Member

Is the code open source? Have you tried upgrading to a later version of sinon?

@roc
Copy link

roc commented Sep 13, 2016

@fearphage didn't have any free time to reproduce this until now. Here's an example with proxyquire, which I feel maybe muddies the waters of what's happening here, but I'd hoped it might be useful nonetheless:

https://github.com/roc/sinon-filesync

@fearphage
Copy link
Member

fearphage commented Sep 13, 2016

@roc Your test case is broken. There are no passing tests. Fix it and I'll check it out.

➜  sinon-filesync git:(master) mocha         


  loading a file and printing it
    1) should say hello m8

  replace file contents
    2) "before all" hook


  0 passing (13ms)
  2 failing

  1) loading a file and printing it should say hello m8:
     TypeError: Cannot read property 'charCodeAt' of undefined
      at Object.stripBOM (internal/module.js:48:14)
      at require (internal/module.js:20:19)
      at Context.it (test/index.js:11:17)

  2) replace file contents "before all" hook:
     TypeError: Cannot read property 'charCodeAt' of undefined
      at Object.stripBOM (internal/module.js:48:14)
      at Object.require.extensions.(anonymous function) (node_modules/proxyquire/lib/proxyquire.js:259:43)
      at Proxyquire._withoutCache (node_modules/proxyquire/lib/proxyquire.js:179:12)
      at Proxyquire.load (node_modules/proxyquire/lib/proxyquire.js:136:15)
      at Context.before (test/index.js:22:11)

@fearphage
Copy link
Member

'use strict'

const file = require('fs').readFileSync(
  __dirname + '/file.txt', 'utf8'
);

module.exports = `hello ${file}`;

The way your code is written you'll never be able to mock it out without something like proxyquire. You're calling functions at import-time which is generally a bad idea. To rephrase, as soon as someone require's your file, you start executing code. That's suboptimal. If you altered your code a bit, it would make it much easier to test and much more friendly for other people to use.

const fs = require('fs');

module.exports = function() {
  return `hello ${fs.readFileSync(...)}`;
};

Here's an example of it working for someone else:

http://stackoverflow.com/questions/18044737/mock-fs-readdir-for-testing

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

4 participants