Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"?

like image 487
Andrei Avatar asked Feb 06 '15 17:02

Andrei


1 Answers

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:

  1. In your package.json file, prevent mocking of node modules (which includes underscore)

    "jest": { "unmockedModulePathPatterns": ["/node_modules/"] }

  2. Explicitly prevent auto mocking in your test file using:

    jest.autoMockOff();

  3. Explicitly exclude underscore from mocking:

    jest.unmock('underscore');

like image 131
Andrei Avatar answered Sep 28 '22 16:09

Andrei