使用 Jasmine 和 TypeScript 进行单元测试 [英] Unit testing using Jasmine and TypeScript

查看:25
本文介绍了使用 Jasmine 和 TypeScript 进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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-nodejasmine 版本.这比使用全局安装的版本要好,因为使用本地版本,您可以确保每个人都使用相同的版本.

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 进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆