我正在尝试使用 Jasmine 编译一个用 Typescript 编写的单元测试.在我的单元测试文件中包含以下内容后,Resharper 会提示我一个从 jasmine.d.ts 导入类型的链接.
I am trying to get a unit test written in Typescript using Jasmine to compile. With the following in my unit-test file, Resharper prompts me with a link to import types from jasmine.d.ts.
/// <reference path="sut.ts" />
/// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />
describe("Person FullName", function () {
var person;
BeforeEach(function () {
person = new Person();
person.setFirstName("Joe");
person.setLastName("Smith");
});
It("should concatenate first and last names", function () {
Expect(person.getFullName()).toBe("Joe, Smith");
});
});
所以我点击链接并得到以下结果(实际上 resharper 只在 describe 函数前加上了Jasmine.",所以我手动为其他 Jasmine 调用添加了前缀):
So I click on the link and end up with the following (actually resharper only prefixed the describe function with "Jasmine.", so I manually prefixed the other Jasmine calls):
/// <reference path="sut.ts" />
/// <reference path="../../../scripts/typings/jasmine/jasmine.d.ts" />
import Jasmine = require("../../../Scripts/typings/jasmine/jasmine");
Jasmine.describe("Person FullName", function () {
var person;
Jasmine.BeforeEach(function () {
person = new Person();
person.setFirstName("Joe");
person.setLastName("Smith");
});
Jasmine.It("should concatenate first and last names", function () {
Jasmine.Expect(person.getFullName()).toBe("Joe, Smith");
});
});
但是,导入语句有一条红色波浪线,带有错误消息无法解析外部模块../../../scripts/typings/jasmine/jasmine.模块不能别名为非模块类型"
However the import statement has a red squiggly line with error message "Unable to resolve external module ../../../scripts/typings/jasmine/jasmine. Module cannot be aliased to a non-module type"
知道是什么导致了这个错误吗?我检查了我的项目构建设置中的模块系统"选项是否设置为 AMD.我还检查了 jasmine 模块是否在 jasmine.d.ts 中定义.我从DefiniteTyped 网站下载了这个文件.
Any idea what is causing this error? I've checked that the "Module System" option is set to AMD in my project build settings. I've also checked that the jasmine module is defined in jasmine.d.ts. I downloaded this file from DefinitelyTyped site.
declare module jasmine {
...
}
这是(在我看来)截至 2018 年测试 ts-node 应用程序的最佳方法:
Here's (in my opinion) the best way to test a ts-node app as of 2018:
npm install --save-dev typescript jasmine @types/jasmine ts-node
在package.json中:
{
"scripts": {
"test": "ts-node node_modules/jasmine/bin/jasmine"
}
}
在 jasmine.json 中将文件模式更改为 *.ts
In jasmine.json change file pattern to *.ts
"spec_files": ["**/*[sS]pec.ts"],
在您的规范文件中:
import "jasmine";
import something from "../src/something";
describe("something", () => {
it("should work", () => {
expect(something.works()).toBe(true);
});
});
运行测试:
npm test
这将使用本地安装的 ts-node 和 jasmine 版本.这比使用全局安装的版本要好,因为使用本地版本,您可以确保每个人都使用相同的版本.
This will use the locally installed versions of ts-node and jasmine. This is better than using globally installed versions, because with local versions, you can be sure that everyone is using the same version.
注意:如果您有一个 Web 应用而不是节点应用,您可能应该使用 Karma 而不是 Jasmine CLI 运行测试.
Note: if you have a web app instead of a node app, you should probably run your tests using Karma instead of the Jasmine CLI.
这篇关于使用 Jasmine 和 TypeScript 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
在 javascript 认为文档“准备好"之前,如何让How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 认为文档“准备好
jasmine 运行和等待实际上是做什么的?What do jasmine runs and waitsFor actually do?(jasmine 运行和等待实际上是做什么的?)
如何提供模拟文件来更改 <input type='filHow to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模拟文件来更改 lt;input type=filegt; 的事
如何使用 Jasmine 对链式方法进行单元测试How to unit test a chained method using Jasmine(如何使用 Jasmine 对链式方法进行单元测试)
如何将 $rootScope 注入 AngularJS 单元测试?How do I inject $rootScope into an AngularJS unit test?(如何将 $rootScope 注入 AngularJS 单元测试?)
Jasmine - 如何监视函数中的函数调用?Jasmine - How to spy on a function call within a function?(Jasmine - 如何监视函数中的函数调用?)