[英]Assert arrays using Chai (or Should) in Javascript ES6 (using Babel)
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 目的是测试该阵列
{title: 'Only Ben'}
{title: 'Only Ben'}
的元素 {title: 'Only Thomas'}
{title: 'Only Thomas'}
的元素 I'm currently using chai.js and chai things with this test: 我目前正在与此测试一起使用chai.js和chai事物 :
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: 为了使事情更清楚,我尝试运行Chai Things文档中给出的示例:
[{ 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 . 在我看来,该问题可能是由于使用Babel引起的。 My Node project heavily uses ES6 syntax, that's the reason why I'm using Babel.
我的Node项目大量使用ES6语法,这就是为什么我使用Babel的原因。 In order to run mocha tests with babel I'm using a babel-hook:
为了使用babel进行摩卡测试,我使用了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. 我已经进行了2个测试(使用和不使用babel),以确认在这种情况下Babel是问题所在。 Here they are:
他们来了:
1. Test without babel 1.无通行证测试
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 2.用通天塔测试
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
结果: 错误 :
TypeError: Cannot read property 'something' of undefined
The second version (with Babel) does not work because your test forgets to call chai.should()
. 第二个版本(与Babel一起使用)不起作用,因为您的测试忘记了调用
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. 似乎Bergi在此评论中是正确的。
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. 提供您正在使用的
babelhook
--compilers
选项的源代码可能会有所帮助。 (The standard way of referencing Babel from Mocha's compiler option is mocha --compilers js:babel/register
.) (从Mocha的编译器选项中引用Babel的标准方法是
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();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.