首页 > 解决方案 > Chai 函数(isAbove、isBelow、ecc)不起作用

问题描述

我正在尝试使用 Mocha 和 Chai 编写测试文件,但 Chai 库似乎不想工作。我肯定做错了什么,但我不明白在哪里。

这是代码

function byte() {}

describe("byteToSignedShort()", function()
{
   context("in b argument with the first bit set to 1", function()
   {
       it("should return a negative number", function()
       {
           assert.isBelow(byte(), 0, "");
       })
   })
   context("in b argument with the first bit set to 0", function()
   {
       it("should return a positive number", function()
       {
           assert.isAtLeast(byte(), 0, "");
       })
   })
})

我不知道它是否有用,但这是 package.json

{
  "name": "learing_typescript",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "mocha -r ts-node/register test/**.ts --recursive --no-deprecation"
  },
  "author": "",
  "license": "UNLICENSED",
  "devDependencies": {
    "@types/expect": "^24.3.0",
    "@types/mocha": "^8.0.3",
    "chai": "^4.2.0",
    "mocha": "^8.1.3",
    "ts-mocha": "^7.0.0",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.3"
  },
  "dependencies": {
    "@babel/cli": "^7.11.6",
    "@babel/core": "^7.11.6",
    "@babel/preset-env": "^7.11.5",
    "@babel/register": "^7.11.5"
  }
}

当我用“npm test”启动它时,会出现这个错误

byteToSignedShort()
    in b argument with the first bit set to 1
      1) should return a negative number
    in b argument with the first bit set to 0
      2) should return a positive number

  0 passing (16ms)
  2 failing

  1) byteToSignedShort()
       in b argument with the first bit set to 1


should return a negative number:
     TypeError: assert.isBelow is not a function
      at Context.<anonymous> (test\byteTestDC.ts:10:20)
      at processImmediate (internal/timers.js:456:21)

  2) byteToSignedShort()
       in b argument with the first bit set to 0
         should return a positive number:
     TypeError: assert.isAtLeast is not a function
      at Context.<anonymous> (test\byteTestDC.ts:17:20)
      at processImmediate (internal/timers.js:456:21)

标签: node.jstypescriptmocha.jschai

解决方案


您可能需要从包中添加assert导入语句chai

import { assert } from 'chai'; // add import statement

function byte() {}

describe("byteToSignedShort()", function()
{
  ...
}

推荐阅读