试图在Webpack中导入对象进行测试,我迷路了

问题描述 投票:0回答:1

我正在为一个项目进行上课,我对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)
javascript webpack mocha chai
1个回答
0
投票
import gameData from './data.js'

或者你可以将dataData导出到data.js中的global。如果您在浏览器中,请设置window.gameData=gameData或者如果您在node.js中,请设置global.gameData=gameData

© www.soinside.com 2019 - 2024. All rights reserved.