简体   繁体   中英

How to write unit test for a RequireJS project

I am trying to write unit tests with chai mocha, but getting the following error.

ReferenceError: define is not defined

This application is written in TypeScript and if I tried to create some dummy file to create unit test, it works fine without having any import.

unit test code is below

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 file for which I want to write unit test is

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();
    }

}

While running unit test, I am getting bellow error

错误图片

tsconfig file is bellow

 "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"
    },

and package.json is bellow

{
  "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"
}

Go to the project root directory and Install amd-loader module and try running below command, this issue will be resolved.

node node_modules/mocha/bin/mocha --require ts-node/register -r amd-loader --timeout=0 tests/**/*.ts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM