为什么我的摩卡/柴错误抛出测试失败?

2024-01-11

我有一个简单的 javascript 包我正在尝试测试。我想检查是否抛出错误,但是当运行我的测试并抛出错误时,测试被标记为失败。

这是代码:

var should = require('chai').should(),
    expect = require('chai').expect();

describe('#myTestSuite', function () {

    it ('should check for TypeErrors', function () {

        // Pulled straight from the 'throw' section of
        // http://chaijs.com/api/bdd/
        var err = new ReferenceError('This is a bad function.');
        var fn = function () { throw err; }
        expect(fn).to.throw(ReferenceError);

    })

})

运行时会给出以下输出:

kh:testthing khrob$ npm test

> [email protected] /cdn-cgi/l/email-protection test /Users/khrob/testthing
> mocha



  #myTestSuite
    1) should check for TypeErrors


  0 passing (5ms)   1 failing

  1) #myTestSuite should check for TypeErrors:
     TypeError: object is not a function
      at Context.<anonymous> (/Users/khrob/testthing/test/index.js:10:3)
      at callFn (/Users/khrob/testthing/node_modules/mocha/lib/runnable.js:249:21)
      at Test.Runnable.run (/Users/khrob/testthing/node_modules/mocha/lib/runnable.js:242:7)
      at Runner.runTest (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:373:10)
      at /Users/khrob/testthing/node_modules/mocha/lib/runner.js:451:12
      at next (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:298:14)
      at /Users/khrob/testthing/node_modules/mocha/lib/runner.js:308:7
      at next (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:246:23)
      at Object._onImmediate (/Users/khrob/testthing/node_modules/mocha/lib/runner.js:275:5)
      at processImmediate [as _immediateCallback] (timers.js:336:15)



npm ERR! Test failed.  See above for more details. 
npm ERR! not ok code 0

我知道这里有很多关于传递给 Expect() 的内容是函数而不是函数的结果的答案,并且我已经尝试了我能想到的匿名函数化的每一种排列,但我总是得到失败的测试结果。

我认为这一定与我的配置有关,因为我基本上只是运行文档中的示例,或者我对测试通过或失败的期望没有正确校准。

有什么线索吗?


这应该可以解决您的问题:

var expect = require('chai').expect;

请注意,expect函数未被调用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的摩卡/柴错误抛出测试失败? 的相关文章

随机推荐