【发布时间】:2019-03-23 14:55:10
【问题描述】:
我正在做一个课堂项目,但我对 webpack 和 chai/mocha 测试感到迷茫。
我有一个对象,其中包含名为
gameData的数组,该对象位于名为data.js的文件中。我正在使用
export default gameData导出gameData变量。我将它导入到我的
Game.js文件中,该文件有一个名为 Game 的类。
我可以使用什么方法让我的 Game 类访问我的 data.js 文件中的 gameData 对象,以及如何测试我的类是否可以访问 gameData?
如果这是一个愚蠢的问题,我们深表歉意,但目前处理 webpack 和单元测试对我来说还是很新鲜的。
import './data.js';
class Game {
constructor(){
this.currentTurn = 'p1';
this.game = gameData;
}
startGame(){
//import the data structures
//copy the data structures
//select 1 survey at random and remove it from the source array
//append the question to the DOM
//create a array with the three associated answers and remove them from source array
}
restartGame(){
//clear all fields
//revert back to starting arrays
}
whoseTurn(){
if (this.currentTurn === 'p2'){
//inputs and fields should reflect that it's players X's turn
//player Y disabled
} else {
//inputs and fields should reflect that it's players Y's turn
//player X disabled
}
}
}
export default Game;
import chai from 'chai';
import Game from '../src/Game.js';
import '../src/data.js';
const expect = chai.expect;
describe('Game', function() {
it('Should have access to the data structures', function() {
let game = new Game();
})
}
测试错误:
1) Game
Should have access to the data structures:
ReferenceError: gameData is not defined
at new Game (dist/webpack:/src/Game.js:6:1)
at Context.<anonymous> (dist/webpack:/test/Game-test.js:8:1)
【问题讨论】:
标签: javascript webpack mocha.js chai