简体   繁体   English

如何为打字稿项目设置茉莉花

[英]How to setup jasmine for typescript project

I need some guidance of how to set up unit testing of TypeScript project with Jasmine. 我需要一些有关如何使用Jasmine设置TypeScript项目的单元测试的指导。

Test spec file looks like this: 测试规范文件如下所示:

/// <reference path="../../../typings/tsd.d.ts" />
import {Mediator} from '../../../services/remoting/Mediator';

describe('Mediator', () =>
{
    let mediator: Mediator;

    beforeEach(() => 
    {
        mediator = new Mediator();
    });

    it('blah blah', () =>
    {
        expect(mediator.TEST).toBeDefined();
    });
});

I use npm jasmine module to run tests. 我使用npm茉莉花模块运行测试。 jasmine.json is pointing to the built JS spec files like this: jasmine.json指向构建的JS规范文件,如下所示:

{
  "spec_dir": "build/spec",
  "spec_files": [
    "**/*.js"
  ]
}

The build spec file looks like this: 构建规范文件如下所示:

/// <reference path="../../../typings/tsd.d.ts" />
define(["require", "exports", '../../../services/remoting/Mediator'], function (require, exports, Mediator_1) {
    describe('factory: Mediator', function () {
        var mediator;
        beforeEach(function () {
            mediator = new Mediator_1.Mediator();
        });
        it('should have defined all required fields', function () {
            expect(mediator.ExecuteQuery).toBeDefined();
        });
    });
});
//# sourceMappingURL=Mediator.spec.js.map

when I try to run tests jasmine complains over undefined 'define' function: 当我尝试运行测试时,茉莉花抱怨未定义的“定义”功能:

ReferenceError: define is not defined

I have tried to search on the TypeScript + Jasmine but there is not much information (and I do not want to use full VisualStudio). 我试图在TypeScript + Jasmine上进行搜索,但是没有太多信息(并且我不想使用完整的VisualStudio)。 So I would appreciate if someone can point me in the right direction of how to set typescript + jasmine and where my mistake is. 因此,如果有人能指出正确的方向设置打字稿+茉莉花以及我的错误所在,我将不胜感激。

Thanks in advance. 提前致谢。

It looks like you are compiling using the --module amd flag, which is designed for asynchronous modules (like when you use RequireJS). 看起来您正在使用--module amd标志进行编译,该标志是为异步模块设计的(例如使用RequireJS时)。

If you are running on Node, you need to supply --module commonjs . 如果您在Node上运行,则需要提供--module commonjs This will result in the following output: 这将导致以下输出:

var mediator_1 = require('../../../services/remoting/Mediator');

(Although the name of the variable may change as it creates a name on the fly for you when compiling ES6 style imports down). (尽管变量的名称可能会更改,因为在向下编译ES6样式时会为您动态创建一个名称)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM