Jasmine Spec as Typescript文件 [英] Jasmine Spec as Typescript File

查看:130
本文介绍了Jasmine Spec as Typescript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jasmine在我的项目中设置单元测试。我在Typescript中编写我的规范。我的第一个测试只是检查配置文件是否按预期返回值。但是,当我 import 配置时,Jasmine无法找到规范。如果我拿出 import 并填写虚拟值,一切正常。

I'm attempting to set up unit testing in my project, using Jasmine. I am writing my specs in Typescript. My first test is simply checking that a config file returns a value as expected. However, when I import the config, Jasmine can't find the spec. If I take out the import and fill in dummy values, everything works fine.

我的spec文件是:

/// <reference path="../typings/index.d.ts"/>
process.env.ENV = "test";
process.env.TEST_DB_NAME= "test";

import environment = require("../config/config");

describe("Config Tests:", () => {
    it("db returns string", () => {
        expect(environment.db).toEqual(process.env.TEST_DB_NAME);
    });
});

environment.db 应该只返回我的 process.env.TEST_DB_NAME

我觉得这必须对 import <做一些事情/ code>一开始使Jasmine找不到 describe()。任何人都知道如何让Jasmine使用导入工具,或者我只是想以错误的方式测试它?

I feel this has to do something with the import at the beginning making Jasmine not find the describe(). Anyone know of a way to get Jasmine to work with imports or am I just going about testing this the wrong way?

推荐答案

如果你直接在你的文件中调用require我认为你需要创建一个模块并将其导出。我成功使用导入的另一种方法是创建一个接口,导出它,然后做这样的事情。

If you call require directly in your file I think you need to create a module and export it. Another way that I have used import successfully has been to create an interface, export it, and then did something like this.

import IUser = UserList.Interfaces.IUser;

然后,您可以将其用作模拟对象的类型。

You can then use this as the type for a mock object.

这篇关于Jasmine Spec as Typescript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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