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

查看:780
本文介绍了使用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");
    });
});

然而,import语句有一条红色波浪线,错误信息无法解析外部模块../ ../../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中定义的。我从DefinitelyTyped网站下载了这个文件。

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 {
    ...
}


推荐答案

这是(在我看来)测试<的最佳方法code> ts-node app截至2018年:

Here's (in my opinion) the best way to test a ts-node app as of 2018:

npm install --save-dev jasmine @types/jasmine ts-node

package.json

{
  "scripts": {
    "test": "ts-node node_modules/jasmine/bin/jasmine"
  }
}

In你的spec文件:

In your spec files:

import "jasmine";
import something from "../src/something";

describe("something", () => {
    it("should work", () => {
        expect(something.works()).toBe(true);
    });
});

运行测试:

npm test

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

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