标签: chai

尝试 ES6 样式导入会给出“不能在模块外使用导入语句”

我正在尝试在 intellij 中编写一个 javascript 测试,我需要为其导入一些依赖项,并且我想使用 ES6 样式的导入语句,但出现错误

/usr/local/bin/node /workspace/rr-sample/node_modules/mocha/bin/_mocha
--ui bdd --reporter "/Users/me/Library/Application Support/IntelliJIdea2019.1/NodeJS/js/mocha-intellij/lib/mochaIntellijReporter.js"
tests/*.test.js /workspace/rr-sample/tests/App.test.js:3
import chai from 'chai'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1043:16)
    at Module._compile (internal/modules/cjs/loader.js:1091:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1160:10)
    at Module.load (internal/modules/cjs/loader.js:976:32)
    at Function.Module._load (internal/modules/cjs/loader.js:884:14)
    at Module.require (internal/modules/cjs/loader.js:1016:19)
    at require (internal/modules/cjs/helpers.js:69:18)
    at /workspace/rr-sample/node_modules/mocha/lib/mocha.js:334:36
    at Array.forEach (<anonymous>)
    at Mocha.loadFiles (/workspace/rr-sample/node_modules/mocha/lib/mocha.js:331:14)
    at Mocha.run (/workspace/rr-sample/node_modules/mocha/lib/mocha.js:809:10)
    at Object.exports.singleRun (/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:108:16)
    at exports.runMocha (/workspace/rr-sample/node_modules/mocha/lib/cli/run-helpers.js:142:13)
    at Object.exports.handler (/workspace/rr-sample/node_modules/mocha/lib/cli/run.js:292:3)
    at Object.runCommand (/workspace/rr-sample/node_modules/yargs/lib/command.js:242:26)
    at Object.parseArgs [as _parseArgs] (/workspace/rr-sample/node_modules/yargs/yargs.js:1087:28)
    at Object.parse …
Run Code Online (Sandbox Code Playgroud)

javascript intellij-idea mocha.js chai

25
推荐指数
4
解决办法
4万
查看次数

如何单元测试表达路由器的路由

我是Node和Express的新手,我正在尝试对我的路由/控制器进行单元测试.我把我的路线与控制器分开了.我该如何测试我的路线?

配置/ express.js

  var app = express();
  // middleware, etc
  var router = require('../app/router')(app);
Run Code Online (Sandbox Code Playgroud)

应用程序/路由器/ index.js

  module.exports = function(app) {
    app.use('/api/books', require('./routes/books'));
  };
Run Code Online (Sandbox Code Playgroud)

应用程序/路由器/路由/ books.js

  var controller = require('../../api/controllers/books');
  var express = require('express');
  var router = express.Router();

  router.get('/', controller.index);

  module.exports = router;
Run Code Online (Sandbox Code Playgroud)

应用程序/ API /控制器/ books.js

// this is just an example controller
exports.index = function(req, res) {
    return res.status(200).json('ok');
};
Run Code Online (Sandbox Code Playgroud)

应用程序/测试/ API /路由/ books.test.js

  var chai = require('chai');
  var should = chai.should();
  var sinon = require('sinon');

  describe('BookRoute', function() { …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing node.js express chai

24
推荐指数
4
解决办法
2万
查看次数

用Chai.js测试失败

在JUnit中,您可以通过执行以下操作来使测试失败:

fail("Exception not thrown");
Run Code Online (Sandbox Code Playgroud)

使用Chai.js实现相同目标的最佳方法是什么?

javascript node.js chai

23
推荐指数
3
解决办法
2万
查看次数

如何用酶测试反应路由器

我正在使用酶+ mocha + chai来测试我的react-redux项目.酶提供浅层测试组分行为.但我没有找到测试路由器的方法.我正在使用react-router如下:

<Router history={browserHistory}>
     ...
        <Route path="nurse/authorization" component{NurseAuthorization}/>
     ...
  </Route>
Run Code Online (Sandbox Code Playgroud)

我想测试这条路线nurse/authorization参考NurseAuthorization组件.如何在reactjs项目中测试它?

EDIT1

react-router用作路由器框架.

mocha.js chai reactjs react-router enzyme

22
推荐指数
1
解决办法
2万
查看次数

期望().to.be.true如何在柴?

expect(true).to.be.true;
Run Code Online (Sandbox Code Playgroud)

在此代码中,所有'to','be','true'似乎都是来自'expect(true)'的对象响应的属性.

这些属性如何工作以便它们可以引发异常?

javascript chai

22
推荐指数
2
解决办法
9437
查看次数

柴:根据参数预期错误与否

我一直在尝试做一个函数的文本来处理错误,如果它是一个有效的错误,它会被抛出,但如果不是,那么什么都不会抛出.问题是我似乎无法在使用时设置参数:

expect(handleError).to.throw(Error);
Run Code Online (Sandbox Code Playgroud)

理想的是使用:

expect(handleError(validError)).to.throw(Error);
Run Code Online (Sandbox Code Playgroud)

有没有办法实现这个功能?

功能代码:

function handleError (err) {
    if (err !== true) {
        switch (err) {
            case xxx:
            ...
        }
        throw "stop js execution";
    else {}
}
Run Code Online (Sandbox Code Playgroud)

和测试代码(不按预期工作):

it("should stop Javascript execution if the parameter isnt \"true\"", function() {
    expect(handleError).to.be.a("function");
    expect(handleError(true)).to.not.throw(Error);
    expect(handleError("anything else")).to.throw(Error);
});
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js chai

21
推荐指数
2
解决办法
8017
查看次数

Chai unittesting - expect(42).to.be.an('integer')

http://chaijs.com/api/bdd/#a,a/ an可用于检查一个变量的类型.

.一种)

@param{ String } type

@param{ String } message _optional_

aan断言是可以用来作为语言链或断言值的类型的别名.

但是,我无法检查变量是否为整数.给出的例子,例如expect('1337').to.be.a('string');为我工作,但以下不是:

expect(42).to.be.an('integer');
expect(42).to.be.an('Integer');
expect(42).to.be.an('int');
expect(42).to.be.an('Int');
Run Code Online (Sandbox Code Playgroud)

运行mocha时,所有这些都给我以下错误:

Uncaught AssertionError: expected 42 to be an integer
Run Code Online (Sandbox Code Playgroud)

如何用chai测试变量是否为整数?

javascript unit-testing mocha.js node.js chai

21
推荐指数
4
解决办法
1万
查看次数

使用Mocha/Chai和async/await验证是否抛出异常

我正在努力找出在使用async/await时在Mocha测试中验证承诺被拒绝的最佳方法.

这是一个有效的例子,但我不喜欢should.be.rejectedWith返回一个需要从测试函数返回的promise才能正确评估.使用async/await删除了测试值的这个要求(正如我对wins()下面的结果所做的那样),我觉得很可能在某些时候会忘记return语句,在这种情况下测试将始终通过.

// Always succeeds
function wins() {
  return new Promise(function(resolve, reject) {
    resolve('Winner');
  });
}

// Always fails with an error
function fails() {
  return new Promise(function(resolve, reject) {
    reject('Contrived Error');
  });
}

it('throws an error', async () => {
  let r = await wins();
  r.should.equal('Winner');

  return fails().should.be.rejectedWith('Contrived Error');
});
Run Code Online (Sandbox Code Playgroud)

感觉应该可以使用async/await将拒绝转换为异常的事实并将其与Chai的should.throw相结合,但我无法确定正确的语法.

理想情况下这可行,但似乎不是:

it('throws an error', async () => {
  let r = await wins();
  r.should.equal('Winner');

  (await fails()).should.throw(Error);
});
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js async-await chai chai-as-promised

21
推荐指数
5
解决办法
1万
查看次数

如何使用chai测试浮点相等?

我们正在使用Chai的BDD API来编写单元测试.

我们如何断言浮点平等?

例如,如果我尝试使此断言检查66⅔%的返回值:

expect(percentage).to.equal(2 / 3 * 100.0);
Run Code Online (Sandbox Code Playgroud)

我失败了:

AssertionError: expected 66.66666666666667 to equal 66.66666666666666
Expected :66.66666666666666
Actual   :66.66666666666667
Run Code Online (Sandbox Code Playgroud)

javascript floating-point precision chai

20
推荐指数
2
解决办法
6024
查看次数

匹配Chai断言中的部分对象?

我正在寻找匹配以下内容的最佳方式:

expect([
    {
        C1: 'xxx',
        C0: 'this causes it not to match.'
    }
]).to.deep.include.members([
    {
        C1: 'xxx'
    }
]);
Run Code Online (Sandbox Code Playgroud)

以上不起作用,因为C0存在于实际中,但不是预期的.简而言之,我希望这个期望通过PASS,但我不知道如何在不写一堆自定义代码的情况下做到这一点......

chai

19
推荐指数
4
解决办法
1万
查看次数