使用Jasmine和TypeScript进行单元测试

编程入门 行业动态 更新时间:2024-10-25 18:24:39
本文介绍了使用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 { ... }

推荐答案

这是(在我看来)测试 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

这将使用本地安装的 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进行单元测试

本文发布于:2023-06-13 09:32:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/678957.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   Jasmine   TypeScript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!