使用Chai(或者应该)在Javascript ES6中使用Assert数组(使用Babel) [英] Assert arrays using Chai (or Should) in Javascript ES6 (using Babel)

查看:508
本文介绍了使用Chai(或者应该)在Javascript ES6中使用Assert数组(使用Babel)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  let projects = [
{
id:55a75be01fa2c7ff76a2ce7a,
title:Only-Ben,
other_keys:可以包含对象或数组
},
{
id:55a75be01fa2c7ff76a2ce7d,
title:Only-Thomas
},
{
id:55a75be01fa2c7ff76a2ce7c​​,
title:Other-Project
}
];

目标是测试数组




  • 包含一个元素,其中包含一个键 {title:'Only Ben'}

  • 不要包含一个有的元素{title:'只有Thomas'}



我目前正在使用 chai.js chai的事情与此测试:

  projects.should.include。 something.that.deep.have.property('title','Only Thomas'); 

这是我的错误回复

 未捕获TypeError:无法读取未定义的属性something

只是为了使事情更清楚,我尝试运行Chai Things文档中给出的示例:

  {a:'cat'},{a:'dog'}]。should.include.something.that.deep.equals({a:'cat'})

我仍然收到类似的错误回复:

 未捕获TypeError:无法读取未定义的属性'something'

在我看来,问题可能是由使用 Babel 。我的Node项目大量使用ES6语法,这就是我使用Babel的原因。为了用babel进行摩卡测试,我使用的是babel-hook:

 (在package.json中)
{
脚本:{
test:./node_modules/.bin/mocha --require babelhook --reporter spec,
}
}

断言数组时如何解决这个未定义的错误?



==编辑==



我做了2个测试(有和没有babel)确认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();
});

} );

结果:测试通行证



2。用babel测试

 从'chai'导入chai; 
let should = chai.should;
let expect = chai.expect;
从chai-thing进口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();
});
});

结果:错误 TypeError:无法读取属性某事未定义

解决方案

第二个版本(与Babel)不起作用,因为测试忘记调用 chai.should()。在我的测试中,一旦添加了这种方法调用,一切都按预期工作(测试通过)。



似乎Bergi在评论。



如果这不适合你,似乎为了帮助你,我们需要更多的信息。这可能有助于提供您正在使用的 babelhook - 编译器选项的源代码。 (从Mocha的编译器选项引用Babel的标准方法是 mocha - compilers js:babel / register 。)



这是我的测试代码:

 从'chai'导入chai; 
chai.should();
导入chaiThings fromchai-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();
});
});


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

解决方案

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();
   });
});

这篇关于使用Chai(或者应该)在Javascript ES6中使用Assert数组(使用Babel)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆