javascript - Karma - TypeScript - Can't find variable: exports -
i learning write unit test cases angular project written in typescript
. have chosen karma
, mocha
that. here application structure:
project/ ├── app/ │ └── components/ │ └── mycoolcomponent/ │ ├── coolcomp.spec.ts │ └── coolcomp.ts │ ├── node_modules/ │ ├── gulpfile.js │── karma.conf.js │── package.json └── tsconfig.json
here 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 task:
gulp.task('test', function (done) { new server({ configfile: __dirname + '/karma.conf.js', singlerun: true }, done).start(); });
package.json has following dev-dependencies:
"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 } "./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); }); });
when run gulp task getting error:
referenceerror: can't find variable: exports
however if remove export
keyword coolcomp.ts , 1st line (import..) coolcomp.spec.ts tests run smoothly. there few questions posted here on none helped me.
can please guide me doing wrong?
update: stackoverflow community , few other forums have figured out solution problem. wish see here url of code's repository: github link
here goes solution. remove sinon while.
npm uninstall @types/sinon npm uninstall sinon
test if tsc works command line. execute: "karma start" command line. no need gulp.
// 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") ] }) }
// tsconfig
{ "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" ]
}
// package.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" } }
Comments
Post a Comment