And*_*ewO 6 testing typechecking typescript
当我写一个打字稿库泛型约束,我希望能够指定的代码测试应类型检查和代码应该不是(因为,说,我要确保类型检查是抓东西,止跌” t正确满足约束)。
我没有在传统的单元测试库中找到解决方案(因为失败的测试最初不会编译)。我想我在TypeScript测试套件中看到了一些这样的示例,其中他们使用文件命名或注释来表示断言,但现在找不到它们,而且我无法阐明测试运行程序如何在如此大的范围内工作无论如何。谷歌搜索也很难:我想到的每一个术语组合都会带回有关类型保护的链接或有关TypeScript本身的错误报告。
我知道我可以建立一个bash脚本,该脚本仅tsc
在文件上运行并断言它们失败,但是我更希望能够在可能的情况下针对特定的编译失败进行匹配。有任何想法吗?
您可以为此使用Microsoft/dtslint。完成设置后,您可以编写如下测试文件:
索引.d.ts:
// TypeScript Version: 2.1
declare module 'Abc' {
function TestFunc(s: string): undefined;
}
Run Code Online (Sandbox Code Playgroud)
测试.ts:
import Abc from 'Abc';
// $ExpectError
Abc.TestFunc(true);
// $ExpectError
Abc.TestFunc(5);
Abc.TestFunc("it is a string");
Run Code Online (Sandbox Code Playgroud)
现在,当您运行 dtslint cli 工具时,不会返回任何错误,因为所有错误都是预期的。如果 f.ex. 错误没有用$ExpectError
(eg Abc.TestFunc(true);
)注释,然后 dtslint 工具失败并显示一条消息:
Error: C:/stackoveflow/test.ts:3:14
ERROR: 3:14 expect TypeScript@next compile error:
Argument of type 'true' is not assignable to parameter of type 'string'.
at I:\..\dtslint\bin\index.js:101:19
at Generator.next (<anonymous>)
at fulfilled (I:\..\dtslint\bin\index.js:5:58)
at <anonymous>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
459 次 |
最近记录: |