Typescript/Karma:让 CommonJS 在测试运行期间对 Karma 可用
Typescript/Karma: Making CommonJS available to Karma during test runs
我正在尝试使用 Jasmine 和 运行 Karma 在 Typescript 中编写单元测试。我已经阅读了关于 SO 的 this tutorial and saw this 问题,但我的问题仍然存在。
当 Typescript 编译器将我的 typescript 编译成 javascript 时,它使用 require
语法从其他文件导入定义。当我 运行 我的编译测试时,我得到一个类似于这个的错误:
Uncaught ReferenceError: require is not defined
at calculator.spec.js
据此,我推断在执行我的规范时没有任何可以定义 require
语法的东西可用。但是,我不清楚我应该怎么做。一个 SO 问题建议使用 karma-browserify,但我注意到上面的教程设法在没有它的情况下使它工作。任何人都可以提供任何帮助,我们将不胜感激。
这是我的文件:
package.json
{
"name": "karma-stuff",
"version": "0.1.0",
"description": "A thing that helps me test stuff",
"main": "index.js",
"scripts": {
"test": "karma start"
},
"keywords": [
"help"
],
"author": "me",
"license": "MIT",
"devDependencies": {
"jasmine": "^2.5.2",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.0.2",
"karma-typescript-preprocessor": "^0.3.0",
"tsc": "^1.20150623.0"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
typings.json
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160725163759",
"jasmine": "registry:dt/jasmine#2.5.0+20161003201800",
"node": "registry:dt/node#6.0.0+20160909174046"
}
}
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'*.spec.ts'
],
preprocessors: {
'**/*.ts': ['typescript']
},
typescriptPreprocessor: {
options: {
sourceMap: true, // generate source maps
noResolve: false // enforce type resolution
},
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
}
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
被测对象:calculator.service.ts
export class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
自测:calculator.spec.ts
import { Calculator } from './calculator.service';
describe("A calculator", function() {
it("adds integers correctly", function() {
let things = new Calculator();
expect(things.add(1, 2)).toBe(3);
});
});
问题似乎是,您使用的是 ECMAScript 5,这使得转译器使用 requirejs。要修复此目标 es6 并将模块设置为 es6 或添加 npm 包 "requirejs" 和 "karma-requirejs".
我正在尝试使用 Jasmine 和 运行 Karma 在 Typescript 中编写单元测试。我已经阅读了关于 SO 的 this tutorial and saw this 问题,但我的问题仍然存在。
当 Typescript 编译器将我的 typescript 编译成 javascript 时,它使用 require
语法从其他文件导入定义。当我 运行 我的编译测试时,我得到一个类似于这个的错误:
Uncaught ReferenceError: require is not defined
at calculator.spec.js
据此,我推断在执行我的规范时没有任何可以定义 require
语法的东西可用。但是,我不清楚我应该怎么做。一个 SO 问题建议使用 karma-browserify,但我注意到上面的教程设法在没有它的情况下使它工作。任何人都可以提供任何帮助,我们将不胜感激。
这是我的文件:
package.json
{
"name": "karma-stuff",
"version": "0.1.0",
"description": "A thing that helps me test stuff",
"main": "index.js",
"scripts": {
"test": "karma start"
},
"keywords": [
"help"
],
"author": "me",
"license": "MIT",
"devDependencies": {
"jasmine": "^2.5.2",
"karma": "^1.3.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.0.2",
"karma-typescript-preprocessor": "^0.3.0",
"tsc": "^1.20150623.0"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
}
}
typings.json
{
"globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160725163759",
"jasmine": "registry:dt/jasmine#2.5.0+20161003201800",
"node": "registry:dt/node#6.0.0+20160909174046"
}
}
karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
files: [
'*.spec.ts'
],
preprocessors: {
'**/*.ts': ['typescript']
},
typescriptPreprocessor: {
options: {
sourceMap: true, // generate source maps
noResolve: false // enforce type resolution
},
transformPath: function(path) {
return path.replace(/\.ts$/, '.js');
}
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
被测对象:calculator.service.ts
export class Calculator {
add(a: number, b: number): number {
return a + b;
}
}
自测:calculator.spec.ts
import { Calculator } from './calculator.service';
describe("A calculator", function() {
it("adds integers correctly", function() {
let things = new Calculator();
expect(things.add(1, 2)).toBe(3);
});
});
问题似乎是,您使用的是 ECMAScript 5,这使得转译器使用 requirejs。要修复此目标 es6 并将模块设置为 es6 或添加 npm 包 "requirejs" 和 "karma-requirejs".