首页 > 解决方案 > Travis CI 构建过程 - TypeScript 在 Mocha 之前捕获错误

问题描述

这与其说是 TypeScript 编译器完成其工作的错误,但它导致我的 Travis 构建失败。

在我的包中,我有一个函数completeRound,它接受一个数字作为它的第一个参数,然后是 3 个可选参数,因为我已经用 TypeScript 编写了它,所以我告诉它它需要一个数字作为第一个参数传入:

function completeRound( number: number, rounding = 1, direction = 'closest', offset = 0 ): number {
  let n: number = +number,
      r: number = Math.abs(+rounding),
      d: string = direction,
      o: number = +offset;

  if (typeof n !== 'number') {
    throw new TypeError('You need to round a number!');
  }

  // Rest of the code goes here
}

一切正常,唯一的麻烦是在测试方面。在我的测试脚本中,我有:

import completeRound from '../src/completeRound';
import { expect } from 'chai';
import 'mocha';

const expectedResults = [
  {
    testVal : {
      number    : 3.5,
      rounding  : 1,
      direction : 'down'
    },
    eR      : 3
  },
  // 40-something different tests, all passing fine
];

expectedResults.map(t => describe(`completeRound(${t.testVal.number}, ${t.testVal.rounding}, '${t.testVal.direction}', ${t.testVal.offset})`,() => {
  it(`should return ${t.eR}`, () => {
    const result = completeRound(t.testVal.number,t.testVal.rounding,t.testVal.direction,t.testVal.offset);
    expect(result).to.equal(t.eR);
  });
})); // For testing results where it shouldn't throw an error

/* This one is the problem line */
expect(() => completeRound([5])).to.throw(TypeError, /a number/);
/* ---------------------------- */

expect(() => completeRound(5,1,'arriba')).to.throw(Error, /valid rounding direction/);

所以我将一个数组而不是一个数字传递给它,我希望 JavaScript 在其中抛出 TypeError。显然,我希望人们在 JavaScript 或 TypeScript 中使用它的人都能得到同样的覆盖。另外,我在我的报道报告中追求那个甜蜜的 100% 数字。但是,从 Travis 的以下报告中,我们可以看到 TypeScript 编译器首先到达那里并引发编译错误。

TSError: ⨯ Unable to compile TypeScript:

dev/test/completeRound.spec.ts:414:28 - error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'number'.

414 expect(() => completeRound([5])).to.throw(TypeError, /a number/);

我没有看到可以测试编译代码的方法,因为这意味着,编译它,我正在使用 webpack 编译它,它直接把它放在我的./dist文件夹中,浏览它和其他所有东西,所以我不能看看如何从中导入,我真的不想要一个两步的过程,我编译它然后捆绑它。

我想简短的版本是,我想测试 JavaScript 错误抛出的错误,TypeScript 会自动捕获它,但 JavaScript 不会。

任何人都可以帮忙吗?

标签: typescripttestingnpmmocha.jstravis-ci

解决方案


我至少看到了两个问题:

  1. 在函数 completeRound() 中,您使用一元加(n = +number),这意味着 n 始终具有类型编号 (AFAIK)。您需要先检查“数字”(第一个函数参数)。或者你可以使用isNaN()来检查,如果你以后还想检查它。示例(简化版):
function completeRound(number: number): number|TypeError {
  // Check the type of the first argument.
  if (typeof number !== 'number') {
    throw new TypeError('You need to round a number!');
  }
  // Before assign it.
  const n: number = +number;
  // For example if first parameter is function, n still has type number, but NaN.
  if (Number.isNaN(n)) {
    throw new TypeError('You need to round a number!');
  }
  // Rest of the code goes here
  return n;
}
  1. 您可以定义unknown然后执行type assertion。例如:
// Use unknown testVar.
const testVar:unknown = [5];
// Do type assertion on testVar.
expect(() => completeRound(testVar as number)).to.throw(TypeError, /a number/);
// Try again with function.
const testVarAgain: unknown = () => false;
expect(() => completeRound(testVarAgain as number)).to.throw(TypeError, /a number/);

希望这可以帮助。祝你好运。


推荐阅读