GithubHelp home page GithubHelp logo

Comments (4)

justinrush avatar justinrush commented on June 3, 2024

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.

georgezlei avatar georgezlei commented on June 3, 2024

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.

kswanny avatar kswanny commented on June 3, 2024

@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.

georgezlei avatar georgezlei commented on June 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.