I'm trying to test an array such as:
let projects = [
{
"id": "55a75be01fa2c7ff76a2ce7a",
"title: "Only-Ben",
"other_keys": "that can contain objects or arrays"
},
{
"id": "55a75be01fa2c7ff76a2ce7d",
"title: "Only-Thomas"
},
{
"id": "55a75be01fa2c7ff76a2ce7c",
"title: "Other-Project"
}
];
The goal is to test that the array
- Contains an element which has a key
{title: 'Only Ben'}
- Do not contain an element which has a key
{title: 'Only Thomas'}
I'm currently using chai.js and chai things with this test:
projects.should.include.something.that.deep.have.property('title', 'Only Thomas');
This is my error response:
Uncaught TypeError: Cannot read property 'something' of undefined
Just to make things clearer, I've tried running the example given in Chai Things documentation:
[{ a: 'cat' }, { a: 'dog' }].should.include.something.that.deep.equals({ a: 'cat' })
And I still get a similar error response:
Uncaught TypeError: Cannot read property 'something' of undefined
It seems to me that the issue might be caused by the use of Babel. My Node project heavily uses ES6 syntax, that's the reason why I'm using Babel. In order to run mocha tests with babel I'm using a babel-hook:
(in package.json)
{
"scripts": {
"test": "./node_modules/.bin/mocha --require babelhook --reporter spec",
}
}
How can I fix this undefined error when asserting arrays?
==EDIT==
I've made 2 tests (with and without babel) to confirm that Babel is the issue in this scenario. Here they are:
1. Test without babel
var chai = require("chai");
chai.should();
chai.use(require('chai-things'));
describe('Array', function() {
it('Test array', function(cb){
[{ a: 'cat' }, { a: 'dog' }].should.include.something.that.deep.equals({ a: 'cat' })
cb();
});
});
Result: Test pass
2. Test with babel
import chai from 'chai';
let should = chai.should;
let expect = chai.expect;
import chai_things from 'chai-things';
chai.use(chai_things);
describe('Array', function() {
it('Test array', function(cb){
[{ a: 'cat' }, { a: 'dog' }].should.include.something.that.deep.equals({ a: 'cat' })
cb();
});
});
Result: Error: TypeError: Cannot read property 'something' of undefined