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');

  • 链接地址: http://www.djcxy.com/p/24664.html

    上一篇: 打开/复制PDF文件到iBook

    下一篇: 为什么在使用笑话测试节点应用程序时看不到控制台日志输出