I've been attempting to write unit tests using chai mocha, but I keep encountering the error below.
ReferenceError: define is not defined
The application I'm working on is written in TypeScript. Interestingly, when I create a dummy file for testing purposes without any imports, it runs smoothly.
Below is the code for the unit test:
import { expect } from 'chai';
import { MarionetteApp } from '../app/app';
describe("MarionetteApp", () => {
describe("Add", () => {
it("Should return 3 when a = 1 and b = 2", () => {
let calc = new MarionetteApp();
var result = calc.test();
expect(result).to.equal("hello");
});
});
});
The specific file I intend to test is as follows:
import { RootView } from './views/RootView';
import { AdvancedCustomController } from './controllers/AdvancedCustomController';
import * as DB from './db/DB';
export class MarionetteApp extends Marionette.Application {
constructor() {
super();
}
test(){
return "hello";
}
initialize() {
(<any>window).UXToolApp = this;
}
onBeforeStart() {
//Init controllers
var advancedCustomizationController = new AdvancedCustomController();
//Init views
var rootView = new RootView();
rootView.render();
}
}
However, when running the unit test, I encounter the following error message:
Here's the tsconfig file configuration:
"version": "1.5.0-beta",
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
// "module": "commonjs",
"isolatedModules": true,
"jsx": "react",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": true,
"noImplicitAny": false,
"removeComments": false,
"noLib": false,
"preserveConstEnums": true,
"suppressImplicitAnyIndexErrors": true,
"rootDir": "./app",
"moduleResolution" : "node"
},
Lastly, the package.json configuration:
{
"name": "design_mode",
"version": "1.0.0",
"description": "",
"main": "scripts/designMode.ts",
"directories": {
"test": "test"
},
"dependencies": {
"amd-loader": "^0.0.8",
"amdefine": "^1.0.1",
"chai": "^4.1.2",
"mocha": "^5.0.3",
"requirejs": "^2.3.5",
"ts-node": "^5.0.1",
"ws": "^0.4.32"
},
"devDependencies": {
"del": "^2.0.0",
"gulp": "^3.9.0",
"gulp-less": "^3.0.3",
"gulp-rename": "^1.2.2",
"gulp-sourcemaps": "^1.6.0",
"gulp-tslint": "^3.2.0",
"gulp-typescript": "^2.8.1",
"gulp-util": "^3.0.6",
"less": "^2.5.1",
"tsd": "^0.6.4",
"typescript": "^2.7.2"
},
"scripts": {
"test": "./node_modules/.bin/mocha --compilers ts:ts-node/register ./test/*.spec.ts",
"server": "cd server && node server.js",
"build": "gulp"
},
"author": "",
"license": "ISC"
}