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

Type Error with const #144

Closed
Sufiane opened this issue Jul 30, 2018 · 12 comments
Closed

Type Error with const #144

Sufiane opened this issue Jul 30, 2018 · 12 comments

Comments

@Sufiane
Copy link

Sufiane commented Jul 30, 2018

I face myself against a Type Error when trying to rewire a private const
I already have the last version 4.0.1

I used the workaround using let but I would prefer to use const as I don't want to change my code only for test purposes

Demo:
index.js:

const myPromisifiedFunction = promisify(someFunct.ToPromisify)
index.test.js:

const myModule = rewire('../myModule')
const myStub = sinon.stub()
const revert = myModule.set('myPromisifiedFunction', myStub)
And what I get when running tests is:
TypeError: Assignment to constant variable. at eval (eval at set (index.js:159:5), :1:19) at Object.set (index.js:159:5) at Object. (test/index.test.js:14:48)

Thanks in advance for any help you could provide !

Edit: I'm aware of the #79 issue, I already commented on it but since it's closed I thought it would be better to open a new one

@Shakakai
Copy link

Shakakai commented Aug 3, 2018

I'd like to provide some additional evidence. The issue appears to happen when using the Jest testing library but not with Mocha. I created a git repo to show the issue: https://github.com/Shakakai/jest-rewire-const-error

Here's a link to the issue in Jest: jestjs/jest#6803
I'm not sure which library is responsible.

@Sufiane
Copy link
Author

Sufiane commented Aug 6, 2018

Thanks @Shakakai ! I was indeed writing tests with jest

@sinkersan
Copy link

Any updates here?

@Sufiane
Copy link
Author

Sufiane commented Dec 13, 2018

@sinkersan not on my end

@missmatsuko
Copy link

I'm having the same problem, also using Jest!

missmatsuko added a commit to missmatsuko/lighthouse-labs-challenge that referenced this issue May 6, 2019
@tyukesz
Copy link

tyukesz commented Mar 2, 2021

+

@andreisaikouski
Copy link

I see this same issue in Mocha. Any suggestions / fixes?

@jhnns
Copy link
Owner

jhnns commented Dec 19, 2021

I've added a test case in the most recent version and I think this is not an issue anymore:

it("should be possible to re-assign consts", function () {
var test = rewire("./constModule.js");
test.__set__("j", "some other value");
expect(test.j()).to.be("some other value");
});

@jhnns jhnns closed this as completed Dec 19, 2021
@justinrush
Copy link

I'm still seeing this in rewire v6 / jest v27.4.5.

db.js

function connectDB() {
    console.log('in connectdb');
}
module.exports.connectDB = connectDB;

constMod.js

const { connectDB } = require('./db');

function doIt() {
    console.log('no');
    connectDB();
}

constMod.test.js

const rewire = require('rewire');
const db = require('./db');
const constMod = rewire('./constMod');

db.connectDB = jest.fn();

describe('constMod', () => {
    it('mocks connectDb', () => {
        constMod.__set__({
            connectDB: db.connectDB,
        });

        const doIt = constMod.__get__('doIt');
        doIt();
        expect(db.connectDB).toHaveBeenCalled();
    });
});

Output:

-> % npm run jest       

> js-test@1.0.0 jest /js-test
> jest

 FAIL  ./constMod.test.js
  constMod
    ✕ mocks connectDb

  ● constMod › mocks connectDb

    TypeError: Assignment to constant variable.

      at eval (eval at __set__ (constMod.js:72:5), <anonymous>:1:11)
      at Object.__set__ (constMod.js:72:5)
      at Object.<anonymous> (constMod.test.js:9:18)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.247 s, estimated 1 s
Ran all test suites.

@tyukesz
Copy link

tyukesz commented Dec 26, 2021

I'm still seeing this in rewire v6 / jest v27.4.5.

db.js

function connectDB() {
    console.log('in connectdb');
}
module.exports.connectDB = connectDB;

constMod.js

const { connectDB } = require('./db');

function doIt() {
    console.log('no');
    connectDB();
}

constMod.test.js

const rewire = require('rewire');
const db = require('./db');
const constMod = rewire('./constMod');

db.connectDB = jest.fn();

describe('constMod', () => {
    it('mocks connectDb', () => {
        constMod.__set__({
            connectDB: db.connectDB,
        });

        const doIt = constMod.__get__('doIt');
        doIt();
        expect(db.connectDB).toHaveBeenCalled();
    });
});

Output:

-> % npm run jest       

> js-test@1.0.0 jest /js-test
> jest

 FAIL  ./constMod.test.js
  constMod
    ✕ mocks connectDb

  ● constMod › mocks connectDb

    TypeError: Assignment to constant variable.

      at eval (eval at __set__ (constMod.js:72:5), <anonymous>:1:11)
      at Object.__set__ (constMod.js:72:5)
      at Object.<anonymous> (constMod.test.js:9:18)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.247 s, estimated 1 s
Ran all test suites.

This could be because you are using object destructuring: const { connectDB } = require('./db');

@justinrush
Copy link

changing the main function to use const db = require('./db'); and then updating the test to be:

const db = require('./db');
const rewire = require('rewire');
const constMod = rewire('./constMod');

db.connectDB = jest.fn();

describe('constMod', () => {
    it('mocks connectDb', () => {
        const conndb = jest.fn();
        constMod.__set__('db', {
            connectDB: conndb,
        });
        const doIt = constMod.__get__('doIt');
        doIt();
        expect(conndb).toHaveBeenCalled();
    });
});

results in the same error

@vuphu
Copy link

vuphu commented Jan 28, 2024

Based on #149, I used rewire for rewire module

const rewireRef = rewire('rewire');
const moduleEnvRef = rewire('rewire/lib/moduleEnv.js');
const jsExtension = moduleEnvRef.__get__('jsExtension');
moduleEnvRef.__set__('jsExtension', () => {
    return function (module: any, filename: any) {
        jsExtension(module, filename);
    };
});
rewireRef.__set__('moduleEnv', moduleEnvRef);

const yourPackage = rewireRef('your-package')

I hope it works in your projects.

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

9 participants