For code that throws exceptions, I need to wrap the expectation in an anonymous function. Otherwise the exception is thrown before it can be caught by Mocha. See this StackOverflow answer.
But wrapping seems to have side effects.
The following code behaves differently when wrapped. it seems to be a Chai problem.
chai = require 'chai'
expect = chai.expect
describe 'Weird', ->
obj =
name: 'kalle'
it 'not wrapped', () ->
expect(obj).to.have.property 'name', 'kalle'
it 'wrapped', () ->
expect(->obj).to.have.property 'name', 'kalle'
My handcrafted Javascript version:
var expect = require('chai').expect;
describe('Weird', function() {
obj = {name: 'kalle'};
it('not wrapped', function() {
expect(obj).to.have.property('name', 'kalle');
});
it('wrapped', function() {
expect(function() {return obj}).to.have.property('name', 'kalle');
});
});
The failure looks like this:
AssertionError: expected [Function] to have a property 'name' of 'kalle', but got ''
Weird
✓ not wrapped
1) wrapped
1 passing (10ms)
1 failing
1) Weird wrapped:
+ expected - actual
+kalle
Why does the wrapping yield different results?
Thanks in advance!