問題描述
我正在嘗試使用 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模板網!