最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Can I mock console in NodeJs? - Stack Overflow

programmeradmin0浏览0评论

In my JS test, I need to check if the console.info is called. That's why I want to mock console. However, it seems that the console variable cannot be assigned with a different object. Did I make any mistake?

Here is the code I used:

var oldConsole = console;
var infoContent;
console = {
  info: function(content) {
    infoContent = content;
  }
};

game.process('a mand');
infoContent.should.equal('a mand is processed');
console = oldConsole;

In my JS test, I need to check if the console.info is called. That's why I want to mock console. However, it seems that the console variable cannot be assigned with a different object. Did I make any mistake?

Here is the code I used:

var oldConsole = console;
var infoContent;
console = {
  info: function(content) {
    infoContent = content;
  }
};

game.process('a mand');
infoContent.should.equal('a mand is processed');
console = oldConsole;
Share Improve this question edited Aug 27, 2015 at 13:15 edi9999 20.6k15 gold badges98 silver badges135 bronze badges asked Apr 6, 2015 at 10:08 Peng XiaoPeng Xiao 11.9k9 gold badges56 silver badges69 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

You can use rewire to replace the whole of console to silence it, or to inject a mock. I use deride but sinon would also work.

var rewire = require('rewire');
var deride = require('deride');
var Game = rewire('../lib/game');

describe('game testing', function() {
  var stubConsole, game;
  beforeEach(function() {
    stubConsole = deride.stub(['info']);
    stubConsole.setup.info.toReturn();
    Game.__set__({
      console: stubConsole
    });
    game = new Game();
  });

  it('logs info messages', function() {
    game.process('a mand');
    stubConsole.expect.info.called.withArgs(['a mand is processed']);
  });
});

I find the solution. I can change the method info of console.

console.info = function(content) {
  infoContent = content;
};

The question is now why console object itself cannot be reassigned?

you can use sinon npm to count the call to a function :

it("calls the original function only once", function () {
    var callback = sinon.spy();
    var proxy = once(callback);

    proxy();
    proxy();

    assert(callback.calledOnce);
    // ...or:
    // assert.equals(callback.callCount, 1);
});

You can find the docs here : sinonjs

I thought I had the same problem and my solution was using this std-mocks module:

  • https://github./neoziro/std-mocks

This has the advantage of not taking over the global "console" but allows you to see what gets logged to the stdout / stderr. This solves the problem in a different way than the question was explicitly looking for; however I believe it is a good answer for the problem the question implies and may be useful for others.

const stdMocks = require('std-mocks');
stdMocks.use(); console.log('test'); stdMocks.restore();
// => undefined [nothing gets output, stdout intercepted]
const logged = stdMocks.flush();
console.log(logged)
// => { stdout: [ 'test\n' ], stderr: [] }
发布评论

评论列表(0)

  1. 暂无评论