Sunday, 11 June 2017

Using Sinon to read assert console messages in Mocha

I am trying to test a web component. This web component writes a warning message to the console if an invalid property value is set. Currently, I have the following:

import { expect } from 'chai';
import { mount } from 'avoriaz';

import MyComponent from '../src/my-component.vue';

const sinon = require('sinon');

describe('my-component.vue', function() {
  let sandbox = null;

  beforeEach(function() {
    sandbox = sinon.sandbox.create();
    sandbox.stub(console, 'warn');
  });

  afterEach(function() {
    sandbox.restore();
  });

  it('should show warning message in console', function() {
    let wrapper = mount(MyComponent, { propsData: { start:-1 } });  // start has to be positive.
    let result = sandbox.calledWith('WARNING!');
    expect(result).to.equal(true);
  });
});

When I run these tests, I get the following exception thrown:

sandbox.calledWith is not a function

I then tried to use sandbox.fakes.calledWith instead. I then received this error:

sandbox.fakes.calledWith is not a function

What am I doing wrong? How do I test to see if a console message is written to the console line? The thing is, if I remove the sandbox.stub(console, 'warn'); line, I can see the line written to the actual console window. So I know it's getting generated (as expected). I just can't figure out how to test for it.

Any help is appreciated.



via user687554

No comments:

Post a Comment