Assert arrays using Chai (or Should) in Javascript

2019-02-27 02:03发布

问题:

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

回答1:

The second version (with Babel) does not work because your test forgets to call chai.should(). In my testing, once that method call is added, everything works as expected (the test passes).

It also seems that Bergi was correct in this comment.

If this does not work for you, it seems that in order to help you, we would need more information. It might help to provide the source code of the babelhook --compilers option you are using. (The standard way of referencing Babel from Mocha's compiler option is mocha --compilers js:babel/register.)

Here is my test code:

import chai from 'chai';
chai.should();
import chaiThings from "chai-things";
chai.use(chaiThings);

describe('Array', function() {
   it('Test array', function(cb){
       [{ a: 'cat' }, { a: 'dog' }].should.include.something.that.deep.equals({ a: 'cat' })
        cb();
   });
});