Comments (4)
You can see this in a unit test on the main branch by:
- adding an export to
testLib/someOtherModule.js
:exports.meh = () => {return "meh";}
- import it in
testLib/constModule.js
:const { meh } = require("./someOtherModule");
- add this test case to
testLib/sharedTestCases.js
:
it("should be possible to re-assign const required methods", function () {
var test = rewire("./constModule.js");
test.__set__({meh: () => {return "bleh"; }});
const val = test.meh();
expect(val).to.be("bleh");
});
from rewire.
I have an interesting finding.
If you rewire a js module below, a type error will be thrown saying the constant variable cannot be assigned.
target.js
const a = 1;
a = 2;
function handler() {}
However, a slight change will make the error disappear.
const a = 1;
a = 2;
async function handler() {}
I haven't checked the rewire source yet and have no idea which behaviour is correct. But it seems that the keyword async
triggers another parsing system that changes the constant to a normal variable.
from rewire.
@georgezlei + 1 on this, found this out today too. I think the latter is incorrect, becasue it means tests will pass gracefully if you accidently re-assign a const.
from rewire.
Disagree with the conclusion. The constant re-assignment can be easily detected by the lint tool and it doesn't have to be the responsibility of the unit test. On the other hand, enabling constant re-assignment can help to mock the dependencies of the target module. Think of the below example in your target module:
const dep = require('dependency');
from rewire.
Related Issues (20)
- Injected mocks add functionality rather than replace it HOT 1
- rewiring any function executes every line that isn't in any function
- Rewired function is not instanceof Function in Jest context HOT 1
- Rewire doesn't work with typescript HOT 3
- Rewire does not seem to work with flow HOT 1
- mocking fs when using rewire
- Rewire dosn't mock function
- A behaviour that could have HOT 1
- Update the vulnerable packages HOT 6
- Vulnerable dependancy eslint 7.32.0 HOT 1
- Vulnerability in dependency eslint HOT 2
- Feature request: ESM support
- Special handling for process.env? HOT 2
- Rewire on node module with shebang doesn't work.
- Eslint need to be updated to at least version 8.15.0 HOT 1
- Rewire is great, but it doesn't work for SonarQube HOT 1
- unpexpected : due to type definition in TS HOT 1
- nconf values are undefined when a module is rewire
- Node test coverage fails with rewire 7.0.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rewire.