在 TypeScript 中进行单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12695934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unittesting in TypeScript
提问by MicTech
I know, TypeScript is one day old. But I`m curious if is here somekind unit test framework or way how to write and run unit tests for TypeScript?
我知道,TypeScript 已经诞生了一天。但我很好奇这里是否有某种单元测试框架或如何为 TypeScript 编写和运行单元测试?
TypeScript can be compiled to JavaScript and I can write test for that JavaScript, but it`s not what I want.
TypeScript 可以编译为 JavaScript,我可以为该 JavaScript 编写测试,但这不是我想要的。
回答by mohamed hegazy
TypeScript is not a runtime language. To execute your TypeScript code you first need to compile it to JavaScript; same applies to testing it. Your tests can be in TypeScript as well, compile both into JavaScript and use your favorite test framework to execute the tests.
TypeScript 不是运行时语言。要执行您的 TypeScript 代码,您首先需要将其编译为 JavaScript;这同样适用于测试它。你的测试也可以在 TypeScript 中,将它们编译成 JavaScript 并使用你最喜欢的测试框架来执行测试。
回答by Fenton
You can write your unit tests in TypeScript or JavaScript, using any of the existing JavaScript unit testing frameworks.
您可以使用任何现有的 JavaScript 单元测试框架在 TypeScript 或 JavaScript 中编写单元测试。
Very soon, I imagine the existing frameworks will get TypeScript ambient definition files(update - they have: http://definitelytyped.org/), which will make them statically typed as far as TypeScript is concerned. In the meantime, you may need to read up on Ambient Declarations and add a few of your own at the start of your tests.
很快,我想现有的框架将获得 TypeScript 环境定义文件(更新 - 他们有:http: //definelytyped.org/),这将使它们就 TypeScript 而言是静态类型的。同时,您可能需要阅读环境声明并在测试开始时添加一些您自己的声明。
Alternatively, you can use tsUnit TypeScript Unit Testing Framework, which is a unit testing framework written in TypeScript - so it plays nice with TypeScript (and can be used in JavaScript too).
或者,您可以使用tsUnit TypeScript 单元测试框架,这是一个用 TypeScript 编写的单元测试框架 - 因此它与 TypeScript 配合得很好(也可以在 JavaScript 中使用)。
回答by Niels van Reijmersdal
Seems there is another test runner/framework called Intern. https://theintern.github.io/
似乎还有另一个名为 Intern 的测试运行程序/框架。https://theintern.github.io/
Here is an article explaining how to use it combined with TypeScript: https://www.sitepen.com/blog/2015/03/24/testing-typescript-with-intern/
这是一篇解释如何将它与 TypeScript 结合使用的文章:https: //www.sitepen.com/blog/2015/03/24/testing-typescript-with-intern/
Looks pretty promising when you are using TypeScript and you are looking for a unit-testing setup that supports source maps.
当您使用 TypeScript 并且正在寻找支持源映射的单元测试设置时,这看起来很有希望。
Example test:
示例测试:
import registerSuite = require('intern!object');
import assert = require('intern/chai!assert');
// Assume that we now have a version of our model in TypeScript:
import SimpleTodoModel = require('todo/model/SimpleTodoModel');
registerSuite({
name: 'SimpleTodoModel',
// Assume we have a promises interface defined
'default data'() {
var emptyModel = new SimpleTodoModel(),
id:string = emptyModel.get('id'),
length:number = emptyModel.get('todos').length,
incomplete:number = emptyModel.get('incomplete'),
complete:number = emptyModel.get('complete');
assert.strictEqual(id, 'todos-dojo',
'Id should default to "todos-dojo"');
assert.strictEqual(length, 0,
'Todos array should default to an empty array.');
assert.strictEqual(incomplete, 0,
'Incomplete count should default to 0.');
assert.strictEqual(complete, 0,
'Incomplete count should default to 0.');
}
});