我正在为一个项目进行上课,我对webpack和chai / mocha测试感到失望。
gameData
的数组的对象,它存在于一个名为data.js
的文件中。gameData
导出export default gameData
变量。Game.js
文件,该文件有一个名为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)
import gameData from './data.js'
或者你可以将dataData导出到data.js中的global。如果您在浏览器中,请设置window.gameData=gameData
或者如果您在node.js中,请设置global.gameData=gameData