-
Notifications
You must be signed in to change notification settings - Fork 128
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
Comments
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 |
Thanks @Shakakai ! I was indeed writing tests with jest |
Any updates here? |
@sinkersan not on my end |
I'm having the same problem, also using Jest! |
|
I see this same issue in Mocha. Any suggestions / fixes? |
I've added a test case in the most recent version and I think this is not an issue anymore: rewire/testLib/sharedTestCases.js Lines 426 to 432 in f5c655a
|
I'm still seeing this in rewire v6 / jest v27.4.5. db.jsfunction connectDB() {
console.log('in connectdb');
}
module.exports.connectDB = connectDB; constMod.jsconst { connectDB } = require('./db');
function doIt() {
console.log('no');
connectDB();
} constMod.test.jsconst 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:
|
This could be because you are using object destructuring: |
changing the main function to use
results in the same error |
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. |
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
The text was updated successfully, but these errors were encountered: