Jasmine Spec as Typescript File

99封情书 提交于 2019-12-25 09:09:48

问题


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.

My spec file is:

/// <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 should simply return my process.env.TEST_DB_NAME.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/38269907/jasmine-spec-as-typescript-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!