- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在学习为用 Typescript
编写的 Angular 项目编写单元测试用例。为此,我选择了 Karma
和 Mocha
。这是应用程序结构:
Project/
├── app/
│ └── components/
│ └── mycoolcomponent/
│ ├── coolcomp.spec.ts
│ └── coolcomp.ts
│
├── node_modules/
│
├── gulpfile.js
│── karma.conf.js
│── package.json
└── tsconfig.json
这是karma.conf.js:
module.exports = function (config) {
config.set({
basePath: '',
frameworks: ['mocha', 'chai', 'sinon'],
files: [
'app/components/mycoolcomponent/coolcomp.ts',
'app/components/mycoolcomponent/coolcomp.spec.ts'
],
exclude: [
],
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', 'IE', 'PhantomJS'],
singleRun: true,
concurrency: Infinity
})
}
tsconfig.json:
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "system",
"moduleResolution": "node",
"noImplicitAny": false,
"outDir": "Scripts/app",
"removeComments": false,
"sourceMap": true,
"target": "es6",
"inlineSources": true
},
"exclude": [
"node_modules",
"wwwroot",
"typings/boot",
"typings/boot.d.ts"
]
}
Gulp 任务:
gulp.task('test', function (done) {
new Server({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done).start();
});
Package.json 具有以下开发依赖性:
"devDependencies": {
"@types/chai": "^4.0.1",
"@types/expect": "^1.20.1",
"@types/mocha": "^2.2.41",
"@types/sinon": "^2.3.3",
"chai": "^4.1.0",
"del": "2.2.2",
"gridstack": "^0.3.0",
"gulp": "^3.9.1",
"gulp-sourcemaps": "2.4.1",
"gulp-typescript": "3.1.7",
"karma": "^1.7.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-ie-launcher": "^1.0.0",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sinon": "^1.0.5",
"karma-typescript-preprocessor": "^0.3.1",
"merge": "1.2.0",
"mocha": "^3.4.2",
"phantomjs": "^2.1.7",
"sinon": "^2.4.1",
"typescript": "^2.4.2",
"typings": "2.1.0"
}
coolcomp.ts:
export class Calculator {
add(x: number, y: number): number {
return x + y;
}
}
coolcomp.spec.ts :
import { Calculator } from "./coolcomp";
var expect = chai.expect;
describe("Calculator Mocha", () => {
var calculator;
beforeEach(() => {
calculator = new Calculator();
});
xit("can add", () => {
var result = calculator.add(5, 5);
expect(result).to.be.equal(1);
});
});
当我运行 gulp 任务时出现此错误:
ReferenceError: Can't find variable: exports
但是,如果我从 coolcomp.ts 中删除 export
关键字并从 coolcomp.spec.ts 中删除第一行(import..)测试运行顺利。在 SO 上已经有几个这样的问题,但没有一个对我有帮助。
有人可以指导我哪里做错了吗?
更新:在 StackOverflow 社区和其他几个论坛的帮助下,我找到了解决此问题的方法。对于那些希望看到它的人,这里是我的代码存储库的 url:GitHub Link
最佳答案
这就是您的解决方案。暂时去掉sinon。
npm 卸载@types/sinonnpm 卸载 sinon
测试 tsc 是否从命令行运行。然后从命令行执行:“karma start”。无需吞咽。
// Karma configuration
module.exports = function (config) {
config.set({
basePath: "",
frameworks: [ "karma-typescript" , "mocha", "chai" ],
files: [
{ pattern: "app/components/mycoolcomponent/**/*.ts", included: true, watches: false }
],
preprocessors: { "app/components/mycoolcomponent/**/*.ts": ["karma-typescript"] },
port: 8081,
typescriptPreprocessor: {
options: {
sourceMap: true,
noResolve: false
},
transformPath: function(path) {
return path.replace(/\.ts$/, ".js");
}
},
browsers: [ "Chrome" ],
reporters: [ "progress", "mocha", "karma-typescript" ],
autoWatch: true,
singleRun: false,
concurrency: Infinity,
plugins: [
require("karma-typescript"),
require("karma-chrome-launcher"),
require("karma-sourcemap-writer"),
require("karma-mocha-reporter"),
require("karma-mocha"),
require("karma-chai")
]
})
}
//TS配置
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noEmitOnError": false,
"noImplicitAny": false,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowJs": true,
"removeComments": true,
"inlineSourceMap": true
},
"types": [
"node",
"mocha",
"chai",
"expect"
],
"version": "2.4.1",
"include": [
"app/**/*.ts"
]
//包.JSON
{
"name": "unittestingwithkarmamocha",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.0.1",
"@types/expect": "^1.20.1",
"@types/mocha": "^2.2.41",
"chai": "^4.1.0",
"gulp": "^3.9.1",
"karma": "^1.7.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage": "^1.1.1",
"karma-ie-launcher": "^1.0.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.3",
"karma-sinon": "^1.0.5",
"karma-source-map-support": "^1.2.0",
"karma-sourcemap-loader": "^0.3.7",
"karma-sourcemap-writer": "^0.1.2",
"karma-typescript": "^3.0.4",
"karma-typescript-preprocessor": "^0.3.1",
"mocha": "^3.4.2",
"phantomjs": "^2.1.7",
"systemjs": "^0.20.17",
"typescript": "^2.4.2"
}
}
关于javascript - Karma - TypeScript - 找不到变量 : exports,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45369418/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!