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

Rewire messes up line number reporting in Jest #165

Open
lorenzos opened this issue May 17, 2019 · 0 comments
Open

Rewire messes up line number reporting in Jest #165

lorenzos opened this issue May 17, 2019 · 0 comments

Comments

@lorenzos
Copy link

lorenzos commented May 17, 2019

When I use rewire() in my Jest test files, JEST CLI starts reporting wrong lines when highlighting errors and failed test. I don't know if it's a Rewire or Jest issue, however everything works correctly when not calling rewire() and using normal require() instead.

Here's all you need to reproduce:

npm install jest rewire # Jest 24.8.0, Rewire 4.0.1 
node --version # v10.15.3
npm init # Set "jest" as test command
// main.js
const sum = (a, b) => a + b;
module.exports = { sum };
// main.test.js
const rewire = require('rewire');
const main = rewire('./main.js');
test('sum', () => {
	const sum = main.__get__('sum');
	expect(sum(1, 2)).toBe(4); // Fail
	expect(sum(1, 2)).toBe(3); // Pass
	expect(sum(1, 1)).toBe(2); // Pass
});

Now, if you run npm test, the test correctly runs and fails on the first expect(), but the third one is highlighted. Here's the output of Jest, note how expected and received values are correct, but highlighted is wrong:

    Expected: 4
    Received: 3

       6 | 	expect(sum(1, 2)).toBe(4); // Fail
       7 | 	expect(sum(1, 2)).toBe(3); // Pass
    >  8 | 	expect(sum(1, 1)).toBe(2); // Pass
         | 	                   ^
       9 | });

Funnilly enough, if you don't use Rewire's __get__ to get the test function, but simply use main.sum instead, Jest highlights a different wrong line, the second expect():

    Expected: 4
    Received: 3

       5 | 	const sum = main.sum;
       6 | 	expect(sum(1, 2)).toBe(4); // Fail
    >  7 | 	expect(sum(1, 2)).toBe(3); // Pass
         | 	                   ^
       8 | 	expect(sum(1, 1)).toBe(2); // Pass
       9 | });

Lastly, if you don't use Rewire altogether, including the main module with require('./main.js') and commenting out require('rewire'), everything is correct:

    Expected: 4
    Received: 3

      4 | test('sum', () => {
      5 | 	const sum = main.sum;
    > 6 | 	expect(sum(1, 2)).toBe(4); // Fail
        | 	                  ^
      7 | 	expect(sum(1, 2)).toBe(3); // Pass
      8 | 	expect(sum(1, 1)).toBe(2); // Pass
      9 | });

Thanks.

@lorenzos lorenzos changed the title Rewire screws line reporting in Jest, I think Rewire mess up line number reporting in Jest, I think May 17, 2019
@lorenzos lorenzos changed the title Rewire mess up line number reporting in Jest, I think Rewire messes up line number reporting in Jest, I think May 17, 2019
@lorenzos lorenzos changed the title Rewire messes up line number reporting in Jest, I think Rewire messes up line number reporting in Jest May 17, 2019
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

1 participant