Jasmine throws"找不到变量:require"

时间:2017-01-22 10:59:58

标签: typescript gulp jasmine commonjs

我使用CommonJS模块系统将VS2015 TypeScript项目转换为ECMA5,并在同一项目中使用Jasmine进行单元测试。一些代码引用self所以我需要使用jasmineBrowser运行测试以获得类似浏览器的环境。

我尝试使用jasmineBrowser.server运行测试,但在" Jasmine服务器监听"之后它一直挂着的消息。我尝试按照gulp-jasmine-browser文档中的说明使用PhantomJS 2切换到jasmineBrowser.headless,但后来我收到了这个错误:

  

ReferenceError:无法找到变量:require

对我来说,测试是使用jasmineBrowser.server还是jasmineBrowser.headless进行测试并不重要。任何解决方案都适合我。有些代码粘贴在下面。

规格文件:

import {MockHub} from "path/to/mockHub";
import {Subject} from "path/to/Subject";

describe("A nice little test", () => {
    let mockHub: MockHub;
    let subject: Subject;    

    jasmine.getEnv().updateInterval = 200;
    jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;

    beforeEach(function () {
        mockHub = new MockHub();
        subject = new Subject();
    });

    afterEach(function () {
        mockHub.dispose();
    });

    it("should assert that it works", function () {
        let result = -1,
            done = false;

        mockHub.bind(someEvent, () => {
            result = subject.getResult();
            done = true;
        });

        runs(function () {
            setTimeout(() => mockHub.dispatch(someEvent), 2000);
        });

        waitsFor(function () {
            return done;
        });

        runs(function () {
            expect(result).toEqual(0);
        });
    });
});

Gulp文件:

var gulp = require('gulp'),
    jasmine = require('gulp-jasmine'),
    runSequence = require("run-sequence"),
    jasmineBrowser = require('gulp-jasmine-browser');

gulp.task("test", function () {
    gulp.src([
        "tests/**/SubjectSpec.js"
    ])
    .pipe(jasmineBrowser.specRunner({ console: true }))
    .pipe(jasmineBrowser.headless());
    // .pipe(jasmineBrowser.specRunner({ console: true }))
    // .pipe(jasmineBrowser.server({port: 9000}));

});

Subject.js文件,来自Subject.ts文件:

import {EventTypes} from "./EventTypes";

class Subject implements ISubject {
    let counter = 0;

    constructor(private hub: IHub) {
        this.hub.bind(EventTypes.all, () => this.counter++);
    }

    public getResult(): number {
        return this.counter;
    }
}

export {Subject};

0 个答案:

没有答案