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

reverting a JSON rewire fails #40

Closed
otusweb opened this issue Feb 19, 2015 · 1 comment
Closed

reverting a JSON rewire fails #40

otusweb opened this issue Feb 19, 2015 · 1 comment

Comments

@otusweb
Copy link

otusweb commented Feb 19, 2015

The following test passes if I use 2.1.5, but fails with 2.3. I'm unsure if i'm doing something wrong or not.

var assert = require("assert");
var should = require('should');
var rewire = require('rewire');
var sinon = require('sinon');

var user = rewire('../user');

describe('user', function(){
    describe('#buildNumber', function(){
        it('should return the value contained in the field "build"', function(){
            var rewireRevert = user.__set__({JSON : {
            "parse": function()
            {
                return {"build":"4"};
            }}});

            var buildNumber = user.buildNumber()
            should(buildNumber).be.ok;
            buildNumber.should.be.eql("4");

            //reset the user object to its original state
            rewireRevert();
            })
        it('should not fail when trying to get the version from the package.json', function(){
            var buildNumber = user.buildNumber()
            should(buildNumber).be.ok;
        })
    })
})

The code being tested:

var http = require('http');
var util = require('util');
var fs = require('fs');

function buildNumber()
{
    var packageVersion = JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).build;

    //extract the "major" version
    return packageVersion;
}

The error returned:

user #buildNumber should return the value contained in the field "build":
     TypeError: undefined is not a function
      at __set__ (/Users/otusweb/Documents/elektra-liveshare/user.js:149:81)
      at /Users/otusweb/Documents/elektra-liveshare/user.js:178:17
      at Context.<anonymous> (/Users/otusweb/Documents/elektra-liveshare/test/user.test.js:22:4)
      at callFn (/usr/local/lib/node_modules/mocha/lib/runnable.js:251:21)
      at Test.Runnable.run (/usr/local/lib/node_modules/mocha/lib/runnable.js:244:7)
      at Runner.runTest (/usr/local/lib/node_modules/mocha/lib/runner.js:374:10)
      at /usr/local/lib/node_modules/mocha/lib/runner.js:452:12
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:299:14)
      at /usr/local/lib/node_modules/mocha/lib/runner.js:309:7
      at next (/usr/local/lib/node_modules/mocha/lib/runner.js:248:23)
      at Immediate._onImmediate (/usr/local/lib/node_modules/mocha/lib/runner.js:276:5)
      at processImmediate [as _immediateCallback] (timers.js:358:17)
@jhnns jhnns closed this as completed in e70efd1 Feb 23, 2015
@jhnns
Copy link
Owner

jhnns commented Feb 23, 2015

Rewire was using JSON.stringify. Your mock made the original JSON.stringify unaccessible for rewire.

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