Unit testing using Jasmine and TypeScript
我正在尝试使用Jasmine编译以Typescript编写的单元测试。在我的单元测试文件中,使用以下命令,Resharper提示我一个链接,该链接用于从jasmine.d.ts导入类型。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /// <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"); }); }); |
因此,我单击链接并得到以下内容(实际上重新共享程序仅在describe函数的前缀为" Jasmine。",因此我手动为其他Jasmine调用添加了前缀):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /// <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"); }); }); |
但是import语句有一条弯曲的红色线条,并带有错误消息"无法解析外部模块../../../scripts/typings/jasmine/jasmine。模块不能别名为非模块类型" <铅>
任何想法导致此错误的原因是什么?我检查了项目构建设置中是否将"模块系统"选项设置为AMD。我还检查了jasmine模块是否在jasmine.d.ts中定义。我从DefinitelyTyped网站下载了此文件。
1 2 3 | declare module jasmine { ... } |
(在我看来)这是自2018年起测试
1 | npm install --save-dev typescript jasmine @types/jasmine ts-node |
在
1 2 3 4 5 | { "scripts": { "test":"ts-node node_modules/jasmine/bin/jasmine" } } |
在您的规格文件中:
1 2 3 4 5 6 7 8 | import"jasmine"; import something from"../src/something"; describe("something", () => { it("should work", () => { expect(something.works()).toBe(true); }); }); |
运行测试:
1 | npm test |
这将使用本地安装的
注意:如果您具有Web应用程序而不是节点应用程序,则可能应该使用Karma而不是Jasmine CLI运行测试。
将此内容放置在typescript规范文件的顶部:
1 2 | /// <reference path="../../node_modules/@types/jasmine/index.d.ts" /> let Jasmine = require('jasmine'); |
您必须安装以下Jasmine模块才能运行:
1 | $ npm install jasmine-core jasmine @types/jasmine jasmine-ts --save-dev |
完成此操作后,IDE(例如WebStorm)将识别Jasmine及其功能,例如describe(),it()和Expect()。。因此,您无需在它们前面加上" Jasmine。"另外,您可以使用jasmine-ts模块从命令行运行规范文件。全局安装以下命令行工具:
1 | $ npm install -g jasmine jasmine-ts |
然后配置" jasmine"命令行模块,以便Jasmine可以找到其配置文件。然后您应该能够运行jasmine-ts,并且您的spec文件应该可以从命令行正常运行:
1 | ./node_modules/.bin/jasmine-ts src/something.spec.ts |
..,您还可以配置IDE使其同样运行,并且以这种方式进行调试运行也应该起作用(对我有用)。
以这种方式编写测试,您可以在不使用Karma的情况下在服务器端运行Jasmine测试规范,或使用Karma在Web浏览器中运行它。相同的typescript代码。
如果导入有问题,请使用
1 | npm i ts-node tsconfig-paths types/jasmine jasmine --save-dev |
运行启用了typescript的茉莉花:
1 | ts-node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine.js |
确保您的茉莉花将搜索.ts文件:
1 2 3 4 5 6 | "spec_files": [ "**/*[sS]pec.ts" ], "helpers": [ "helpers/**/*.ts" ], |
要测试脚本,如果在项目中使用脚本,则可能还需要填充。创建具有所需导入的帮助文件,例如
1 | import 'core-js'; |
对我来说,我做了以下事情:
安装打字
1 | npm install typings --global |
然后添加茉莉花的类型
1 | typings install dt~jasmine --save --global |
您可以尝试仅副作用导入,它会引入
1 2 3 4 | // tslint:disable-next-line:no-import-side-effect import"jasmine"; describe("My Unit Test", () => { /* ... */ } ); |
当然,您仍然需要安装茉莉花和键入内容:
1 | $ npm i jasmine @types/jasmine --save-dev |
但是对于ts或node不需要专门的茉莉花加载程序。只需对已编译的js文件运行jasmine:
1 | $ node ./node_modules/jasmine/bin/jasmine.js --config=test/support/jasmine.json |
假设您的typescript文件位于编译为
1 2 3 4 5 6 7 8 | { "spec_dir":"bin/test", "spec_files": [ "**/*[sS]pec.js" ], "stopSpecOnExpectationFailure": false, "random": false } |
P.S。以上所有方法在Windows上也都可以使用
将此包含到您的茉莉html文件中,...
1 | <script type="text/javascript" src="jasmine/lib/jasmine-2.0.0/jasmine.js"> |
...或安装npm茉莉花软件包:
1 | npm install --save-dev jasmine |
使用第二种方式(茉莉花作为模块)时,必须将其导入:
1 | var jasmine = require('jasmine'); |
或
1 | import jasmine from 'jasmine'; |
然后更改其他代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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"); }); }); |
就我个人而言,我更喜欢第一种方式而不使用jasmine npm模块。 (我尚未测试模块)
您并不是要这样做,而是要获得加分:一旦获得并运行AJ的答案(使用
1 2 3 | "scripts": { "watch":"ts-node-dev --respawn -- ./node_modules/jasmine/bin/jasmine src/**.spec.ts" } |
当然,您可以根据需要使用Jasmine的配置文件来传递规格或任何其他参数。现在,Jasmine将一次运行所有规格,然后