gpt4 book ai didi

javascript - 运行用 TypeScript 编写的 Jasmine 测试

转载 作者:太空宇宙 更新时间:2023-11-04 00:32:09 29 4
gpt4 key购买 nike

我有一个 Typescript+Node+Angular2+Electron 应用程序,目前正在尝试运行 Node 类的测试,也是用 Typescript 编写的。

为了构建应用程序并在电子中运行它,我使用以下 tsconfig:

"compilerOptions": {
"module": "system",
"target": "es6",
...
}

正如你所看到的,它使用systemjs并将TS编译成JS-es6。它工作正常,应用程序本身正在工作。

现在我需要 Jasmine 加入。我安装了这个 npm 包,更新了我的 gulp 任务以仅运行 1 个文件的 gulp-jasmine:

gulp.task('jasmine', function() {
gulp.src('./test/test.js')
.pipe(jasmine())
});

这就是我的 test.js 的样子:

System.register(["./models-src/app/models/pathWatch/pathWatch"], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var pathWatch_1;
return {
setters:[
function (pathWatch_1_1) {
pathWatch_1 = pathWatch_1_1;
}],
execute: function() {
describe("Run Application:", () => {
it("starts", () => {
var pw1 = new pathWatch_1.PathWatch();
expect(true).toEqual(true);
});
});
}
}
});

所以,没什么特别的,1 import-1test-1assert,用 SystemJs 的东西包装。当我尝试运行此测试时,出现错误:“系统未定义”。

我的问题是:

1) 是否可以使用 systemjs 加载器来运行 jasmine 测试?

2)如果可能的话,我需要安装/配置一些额外的东西吗?

3)我尝试使用 Module="commonjs"编译 TS 并且它工作正常。但我不想以不同的方式编译我的源代码以进行测试和构建。为什么它可以与 commonjs 一起正常工作而无需任何额外的操作?

4)我还尝试使用 Module="es6"编译 TS。它不起作用,我有一个错误“意外的保留字”。是否可以运行用 js es6 编写的 jasmine 测试而不将其转换为 es5?

非常感谢!

最佳答案

1) Is it possible to run jasmine tests, using systemjs loader inside?

2) If it's possible, do I need to install/configure some additional stuff?

您的意思是,使用 systemjs 作为加载器在 Node 中运行 jasmine 测试?我认为 jasmine 不支持使用 systemjs 而不是 require 来加载模块。所以你的测试需要在commonjs中,但是测试代码可以使用SystemJS来加载和测试应用程序代码。只要 systemjs 配置正确并且可以找到 pathWatch 模块,test.js 中类似的东西就可以工作:

describe("Run Application:", () => {
it("starts", (done) => {

var system = require('systemjs');
system.config({
// systemjs config here
//
});
system.import('path-to-path-watch-module').then(pathWatch => {
var pw = new pathWatch.PathWatch();
expect(true).toEqual(true);
done();
});
});
});

system.import 是异步的,因此所有 jasmine 测试都需要 async也是。

3) I tried to compile TS using Module="commonjs" and it's working. But I don't want to compile my source differently for tests and build. Why it's working fine with commonjs without any additional manipulations?

因为编译后的代码中没有对 System 的引用 - 它像任何其他 Node 模块一样使用 module.exports 并且可以由 jasmine 按原样加载。

4) Also I tried to compile TS using Module="es6". It's not working, I have an error "Unexpected reserved word". Is it possible to run jasmine tests written in js es6 without transpiling them into es5?

Module="es6" 需要一个支持 es6 importexport 的运行时,因此它需要一个转译器和模块加载器才能在当前版本的 Node 上运行。

关于javascript - 运行用 TypeScript 编写的 Jasmine 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40575823/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com