簡體   English   中英

在沒有模塊的情況下使用TypeScript,Mocha和Chai

[英]Using TypeScript, Mocha, and Chai without Modules

我正在使用TypeScript,並嘗試這樣做而不創建任何模塊(即,沒有export語句)。 使用模塊將需要使用SystemJS或AMD,並且我試圖使我的項目盡可能簡單。

我想創建單元測試,而Mocha / Chai似乎是最受歡迎的方法。

我有3個文件:

// ../src/Cell.ts
class Cell {
    public Z: number;
    public Y: number;
    public X: number;

    constructor (z: number, y: number, x: number) {
        this.Z = z;
        this.Y = y;
        this.X = x;
    }
}

// ../src/Maze.ts
class Maze {
    public myCell: Cell;
    private width: number;

    constructor (width: number) {
        this.myCell = new Cell(-1, -1, -1);
    }

    protected directionModifier(cell: Cell, direction: string) {
        // does something
    }
}

// ../test/MazeTests.ts
let chai = require('chai');
import { expect } from 'chai';
var assert = require('assert');
var mocha = new Mocha();
mocha.addFile('../src/Cell.ts');
mocha.addFile('../src/Maze.ts');

describe('Maze Test Suite', function ()  {

    it('should return a cell x-1 of the current location (1 cell to the south)', function () {

        let myMaze = new Maze(4);
        let myCell = new Cell(0,0,0);

        const result =  myMaze.directionModifier(myCell,"South");

        assert.deepEqual(result, new Cell(0,1,0));

    });
});

運行npm test時出現一些錯誤:

test/MazeTests.ts(42,20): error TS2304: Cannot find name 'Maze'.
test/MazeTests.ts(43,20): error TS2304: Cannot find name 'Cell'.
test/MazeTests.ts(47,38): error TS2304: Cannot find name 'Cell'.

我很可能錯過了明顯的東西。

在測試文件中的任何位置都未定義Cell和Maze變量。 您不能做新的迷宮和新的牢房,因為它們不存在。

我從來沒有聽說過要從另一個文件中加載一個類而不將其導出,而且我也不知道mocha.addFile是做什么的,因為我更喜歡Jasmine而不是Mocha和Chai ...尤其是對於Typescript項目。

但是,如果行mocha.addFile使用文件中定義的內容創建輸出變量,請嘗試將其定義為Maze和Cell。 然后創建對象。

但是,最好的方法是在Cell和Maze類上進行導出,並將它們導入測試文件中。

萬一這對其他人有用,我想出了一個解決方法:

我安裝並修改了concat:

npm install concat

// in concat/index.js
// change lines 25-26 so that the `read` function is:
const read = (fName) => new Promise((res, rej) => {
    fs_1.readFile(path_1.resolve(fName), (err, str) => {
        if (err)
            rej(err);
        var newText = str.toString().replace(/^class /g, 'export class ')
        res(newText)
    });
});

// in the test file:
// first run: comment out the later lines
// next run: use whole document
import { expect } from 'chai';
import 'mocha';
var concat = require('concat')
var assert = require('assert');
concat([
    "./src/Cell.ts",
    "./src/Character.ts",
    "./src/Maze.ts",
    "./src/MazeView.ts",
    "./src/main.ts"], './test/testable.ts');

import { Cell } from '../test/testable';
import { Maze } from '../test/testable';


describe('Maze Test Suite', function () {

    it('should return a cell x-1 of the current location (1 cell to the south)', function () {

        let InstanceOfMaze = new Maze(4, 8, 8);

        const result = InstanceOfMaze.directionModifier(new Cell(0, 0, 0), "South");
        assert.deepEqual(result, new Cell(0, 1, 0));

    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM