使用Jasmine和TypeScript进行单元测试

时间:2015-06-16 09:22:07

标签: javascript unit-testing typescript jasmine chutzpah

我正在尝试使用Jasmine编写一个用Typescript编写的单元测试。在我的单元测试文件中有以下内容,Resharper提示我从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只用“Jasmine。”作为描述函数的前缀,所以我手动为其他Jasmine调用做了前缀):

/// <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。模块不能别名为非模块类型”< / p>

知道导致此错误的原因是什么?我已经检查过“模块系统”选项在我的项目构建设置中设置为AMD。我还检查过jasmine模块是在jasmine.d.ts中定义的。我从DefinitelyTyped网站下载了这个文件。

declare module jasmine {
    ...
}

9 个答案:

答案 0 :(得分:39)

这是(在我看来)截至2018年测试ts-node应用的最佳方式:

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

package.json

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

在您的spec文件中:

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

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

运行测试:

npm test

这将使用本地安装的ts-nodejasmine版本。这比使用全局安装的版本更好,因为对于本地版本,您可以确保每个人都使用相同的版本。

注意:如果您有一个Web应用程序而不是节点应用程序,您应该使用Karma而不是Jasmine CLI运行测试。

答案 1 :(得分:7)

将它放在打字稿规范文件的顶部:

/// <reference path="../../node_modules/@types/jasmine/index.d.ts" />
let Jasmine = require('jasmine');

您必须安装以下Jasmine模块才能正常工作:

$ npm install jasmine-core jasmine @types/jasmine jasmine-ts --save-dev

一旦你这样做,IDE(例如WebStorm)将识别Jasmine及其功能,例如describe(),it()和expect()..所以你不需要在它们前面添加& #34;茉莉&#34。此外,您可以使用jasmine-ts模块从命令行运行spec文件。全局安装这些命令行工具:

$ npm install -g jasmine jasmine-ts

然后配置&#34; jasmine&#34;命令行模块使Jasmine可以找到其配置文件。然后你应该能够运行jasmine-ts并且你的spec文件应该从命令行运行正常:

./node_modules/.bin/jasmine-ts src/something.spec.ts

..并且,您可以将IDE配置为像这样运行它,并且以这种方式运行的调试也应该有效(对我有用)。

以这种方式编写测试,您可以在没有Karma的情况下在服务器端运行Jasmine测试规范,或者使用Karma在Web浏览器中运行它。相同的打字稿代码。

答案 2 :(得分:5)

对我来说,我做了以下事情:

安装打字

npm install typings --global

然后为jasmine添加输入法

typings install dt~jasmine --save --global

答案 3 :(得分:3)

将此内容包含在您的jasmine html文件中,...

<script type="text/javascript" src="jasmine/lib/jasmine-2.0.0/jasmine.js"></script>

...或安装npm jasmine包:

npm install --save-dev jasmine

当你使用第二种方式(jasmine作为模块)时,你必须导入它:

var jasmine = require('jasmine');

import jasmine from '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");
    });
});

我个人更喜欢不使用jasmine npm模块的第一种方式。 (我还没有测试模块)

答案 4 :(得分:1)

如果您遇到导入问题,请使用tsconfig-paths

npm i ts-node tsconfig-paths types/jasmine jasmine --save-dev

运行使用typescript-enabled的jasmine:

ts-node -r tsconfig-paths/register node_modules/jasmine/bin/jasmine.js

确保您的茉莉花将搜索.ts文件:

"spec_files": [
    "**/*[sS]pec.ts"
],
"helpers": [
    "helpers/**/*.ts"
],

要测试您的脚本,如果您在项目中使用它们,您可能还需要填充。创建一个包含所需导入的帮助文件,例如helpers/global/polifill.ts

import 'core-js';

答案 5 :(得分:1)

您可以尝试仅副作用导入,它会引入@types/jasmine声明,并将茉莉花函数放到全局范围内,因此您无需在每个调用前加{ {1}}允许从现有的单元测试中快速移植,并且仍然可以很好地与webpack一起使用。

jasmine.

当然,您仍然需要安装茉莉花和打字稿:

// tslint:disable-next-line:no-import-side-effect
import "jasmine";

describe("My Unit Test", () => { /* ... */ } );

但是对于ts或node不需要专门的茉莉花加载程序。只需对已编译的js文件运行jasmine:

$ npm i jasmine @types/jasmine --save-dev

假设您的打字稿文件位于编译到$ node ./node_modules/jasmine/bin/jasmine.js --config=test/support/jasmine.json 的“测试”子目录中,并且您有一个bin/test,其内容如下:

test/support/jasmine.json

P.S。以上所有方法同样适用于Windows

答案 6 :(得分:0)

由于某种原因,这个问题从来没有得到适当的回答,当然事情发生了变化,但这里没有现代答案,我是通过谷歌来到这里的,因为我懒惰,有时也是google片段。

如果您遇到此问题,请确保已将@typings/jasmine安装为devDependency,并且您的tsConfig包含包含中的类型。

  "include": ["typings/**/*.d.ts"]

答案 7 :(得分:0)

你没有要求这个,而是为了加分:一旦你得到 AJ 的答案并运行(使用 ts-node 调用 Jasmine 启动脚本),你可以添加一个新任务:

"scripts": {
  "watch": "ts-node-dev --respawn -- ./node_modules/jasmine/bin/jasmine src/**.spec.ts"
}

当然,如果您愿意,您可以使用 Jasmine 的配置文件传递您的规范或任何其他参数。现在,Jasmine 将运行您的所有规范一次,然后 ts-node-dev 将坐在后台等待您的测试或任何他们可能require'd 更改的内容,此时 jasmine 将是再次运行。我还没有找到只运行已更改的测试(或导入已更改的测试)的方法——据我所知,这是 not supported anyway;

答案 8 :(得分:0)

我的文件夹结构

Spec 文件夹位于项目的根目录

    spec
      \- dist              // compiled tests
      \- helpers           // files modified testing env
         \- ts-console.ts  // pretty prints of results
      \- support
         \- jasmine.json
      \- YourTestHere.spec.ts
      \- tsconfig.json     // tsconfig for your tests

文件内容

ts-console.ts

const TSConsoleReporter = require("jasmine-console-reporter");
jasmine.getEnv().clearReporters();
jasmine.getEnv().addReporter(new TSConsoleReporter());

jasmine.json

{
  "spec_dir": "spec/dist",
  "spec_files": [
     "**/*[sS]pec.js"
  ],
  "helpers": [
    "spec/helpers/**/*.js"
  ],
  "stopSpecOnExpectationFailure": false,
  "random": true
}

package.json

中有额外的脚本
"scripts": {
  "test": "rm -rf ./spec/dist && tsc -p ./spec && jasmine"
}

并将“/spec/dist”行添加到 .gitignore

运行您的测试!

使用 npm test 运行您的测试。

它是如何工作的?

  1. 测试目录已清理。
  2. 测试被编译到 JS 的 spec/dist 文件夹中。
  3. 测试从此位置运行。

希望能帮到你。良好的编码。

相关问题