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

Code coverage tool (jest --coverage) not recognizing coverage for rewired functions (jest @23.4.2, rewire @4.0.1) #145

Open
El-Fitz opened this issue Aug 10, 2018 · 20 comments

Comments

@El-Fitz
Copy link

El-Fitz commented Aug 10, 2018

Hello !

So, first of all, I love rewire. Being able to just test specific functions without having to export them all is great. Thank you !

On to the issue.
We recently migrated from mocha to Jest, and it seems that when it comes to coverage jest has an issue with rewire, or rewire an issue with jest.

Trying to get a specific function, like this

const myModule = rewire("./path/to/module"); const myFunction = myModule.__get__("myFunction");

Then testing it, like so

`describe("Simple function test", function () {
let input = "hey !";
let expectedResult = "hello !";
let result = () => myFunction(input);

	it("should not throw an error", () => {
		return expect(result).not.toThrow();
	});
	
	it("should return the expected result", () => {
		return expect(result()).toStrictEqual(expectedResult);
	});
});`

Ends up running the tests properly (checked with breakpoints that everything passed where it should), but the function is not considered as covered in the coverage report :'(

I'll put together a minimal repo to demonstrate it, either this evening or by the end of next week.

A great week-end to all of those who read this ! :-)

@El-Fitz El-Fitz changed the title Code coverage tool (jest --coverage) not recognizing rewired functions (jest @23.4.2, rewire @4.0.1) Code coverage tool (jest --coverage) not recognizing coverage for rewired functions (jest @23.4.2, rewire @4.0.1) Aug 10, 2018
@liunate
Copy link

liunate commented Aug 16, 2018

Ran into the same issue with latest rewire@4.0.1, jest@23.5.0, 0% line coverage for private function being invoked in test

Here's the demo repo showing the issue, simple jest test, also with my coverage report included
https://github.com/liunate/rewire-coverage.git

@andynhi
Copy link

andynhi commented Aug 18, 2018

hitting same issue, i attempted to roll back rewire to @2 and @3, but no luck

@sygint
Copy link

sygint commented Sep 13, 2018

Rewire is amazing for testing our non-module code, but sadly I am having this issue as well.

@aonamrata
Copy link

aonamrata commented Oct 15, 2018

😢 Did this work for anyone? I am on

    "@babel/core": "^7.0.0-0",
    "rewire": "^4.0.1",
    "babel-plugin-rewire": "^1.2.0"

and my tests run correctly but coverage is 0%

babel.config.js

module.exports = {
    presets: ['@babel/preset-env'],
    "env": {
		"test": {
			"plugins": [
				"rewire"
			]
		}
	}
};

@o-t-w
Copy link

o-t-w commented Oct 17, 2018

I am also hitting the same issue

@rafaelcamargo
Copy link

Looking for some alternative here because I've fallen in the same place as cited above, but I'd love to keep with rewire. Your API is awesome 👍

@rafaelcamargo
Copy link

rafaelcamargo commented Nov 17, 2018

I've just realized that any require() dependency could be easily mocked if you transform your module in a class:

// module.js
const rawFs = require('fs');

class Module {
  constructor(fs){
    this.fs = fs || rawFs;
  }
  doSomething(filepath){
    const file = this.fs.readFileSync(filepath, 'utf-8');
    ...
  }
}

module.exports = Module;

So, mocking becomes a breeze...

// module.test.js
const Module = require('./module');

describe('Module File', () => {
  it('should so something', () => {
    const fsMock = { readFileSync: jest.fn() };
    const filepath = 'some/path/to/file.js';
    const module = new Module(fsMock);
    module.doSomething(filepath);
    expect(fsMock.readFileSync).toHaveBeenCalledWith(filepath, 'utf-8');
  });
})

@Cosmo439
Copy link

Any movement on this issue? I've got the same problem as well jest: 24.5.0 and rewire: 4.0.1

@tsunamiq
Copy link

Rewire is great but not being able to provide code coverage details is a major blocker for us. Would love to see coverage added to this library. Current jest version is 23.6.0 and the version of rewire out team tried out was 4.0.0

@kmpvelasquez
Copy link

I have the same issue. Any updates?

@OrBarak1990
Copy link

Also in my case, any new? or some kind of a workaround?

@fvbrennan
Copy link

I'm running into the same issue and its approaching 1-1/2 years unresolved? Like the other commenters, I really like rewire, but I'm wondering if I should abandon it before I get too dependent on it?

@mrdulin
Copy link

mrdulin commented Feb 4, 2020

+1

@joejeet
Copy link

joejeet commented Mar 27, 2020

Any update on this?

@CBD-iBiZa
Copy link

Same issue today : code coverage don't count rewires calls for internal (unexported) tests.

  • jest 25.1.0
  • rewire 5.0.0
  • ts-jest 25.2.0
  • ts-node 8.6.2
  • typescript 3.7.5

@samuelgamito
Copy link

samuelgamito commented Jun 15, 2020

I'am facing the same issue:

  • jest : 26.0.1
  • rewire : 5.0.0

@coleabbeduto-NM
Copy link

Same issue :/
Jest: 26.0.1
Rewire: 5.0.0

@TheJizel
Copy link

TheJizel commented Jul 8, 2020

+1

"jest": "26.1.0"
"rewire": "5.0.0"

@denisdnc
Copy link

denisdnc commented Jul 17, 2020

+1
"jest": "^26.1.0" "rewire": "^5.0.0"

@cqcmdwym
Copy link

Found a temp solution: https://www.grzegorowski.com/jest-tests-with-rewire-plugin
We can use babel-jest and babel-plugin-rewire

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