Why can't I see console log output when testing node apps with jest
I am new to testing with jest and I cannot seem to be able to see the console output from modules I want to test.
my-module.js:
var _ = require('underscore');
exports.filter = function(data) {
if(_.isArray(data)) {
console.log("Data is: " + data);
data = data[0];
}
return data;
}
my-module-test.js:
jest.dontMock('../my-module');
var testdata = [{label: "test"}, {id: 5}];
describe('test my module', function(){
it('changes some data' , function(){
var transformedData = require('../my-module').filter(testdata);
expect(transformedData).toBe(testdata[0]);
});
});
Why is jest swallowing my console.log output in "my-module.js"?
Jest seems to mock everything by default. Which is the issue I encountered.
Underscore "_" was mocked and there was no visible error, so the execution never got to the console.log in the first place.
There are three possibilities (as far as I know) to prevent such a situation:
In your package.json file, prevent mocking of node modules (which includes underscore)
"jest": { "unmockedModulePathPatterns": ["/node_modules/"] }
Explicitly prevent auto mocking in your test file using:
jest.autoMockOff();
Explicitly exclude underscore from mocking:
jest.unmock('underscore');
上一篇: 打开/复制PDF文件到iBook