Javascript 在每次测试中为所需的模式模块创建新对象

Javascript 在每次测试中为所需的模式模块创建新对象,javascript,node.js,Javascript,Node.js,既然Node.js为所需模块创建了一个全局单例,那么我如何在每个测试中创建下面游戏的唯一实例呢?我想确保每次我启动游戏时,它都是从一个新的游戏对象启动的,该对象的初始值为false 现在game.start,game是每个测试中使用的同一个单例,我不想这样,我不应该在每个测试中都共享这个单例,这显然很糟糕 let chai = require('chai'), should = chai.should(), game = require('../src/game'); desc

既然Node.js为所需模块创建了一个全局单例,那么我如何在每个测试中创建下面游戏的唯一实例呢?我想确保每次我启动游戏时,它都是从一个新的游戏对象启动的,该对象的初始值为false

现在game.start,game是每个测试中使用的同一个单例,我不想这样,我不应该在每个测试中都共享这个单例,这显然很糟糕

let chai = require('chai'),
    should = chai.should(),
    game = require('../src/game');

describe('Starting the Game', () => {

    it('should be able to start the game', () => {
        game.start();

        game.started.should.be.true;
    });

    it('should contain a new board to play on when game starts', () => {
        game.start();

        game.started.should.be.true;
        should.exist(game.board);
    });
});
game.js

var board = require('./board'),
    player = require('./player');

var game = module.exports = {
    start: start,
    started: false,
    board: board.create()
};

function start(){
    game.started = true;
};
var Board = require('./board'),
    Player = require('./player');

class Game {
    constructor() {
        this.started = false;
        this.board = new Board();
    }
    start() {
        this.started = true;
    }
}

export default Game;
const chai = require('chai'),
    should = chai.should(),
    Game = require('../../test');

let game;

describe.only('Starting the Game', () => {
    beforeEach((done) => {
        game = new Game();
        done();
    });

    it('should be able to start the game', () => {
        game.start();

        game.started.should.be.true;
    });

    it('should contain a new board to play on when game starts', () => {
        game.start();

        game.started.should.be.true;
        should.exist(game.board);
    });
});

如果您需要在每个测试中实例化一个新实例,那么您需要将
game
board
定义为一个类

然后,您可以在每个测试用例之前执行的
beforeach
方法中实例化游戏的新实例

Game.js

var board = require('./board'),
    player = require('./player');

var game = module.exports = {
    start: start,
    started: false,
    board: board.create()
};

function start(){
    game.started = true;
};
var Board = require('./board'),
    Player = require('./player');

class Game {
    constructor() {
        this.started = false;
        this.board = new Board();
    }
    start() {
        this.started = true;
    }
}

export default Game;
const chai = require('chai'),
    should = chai.should(),
    Game = require('../../test');

let game;

describe.only('Starting the Game', () => {
    beforeEach((done) => {
        game = new Game();
        done();
    });

    it('should be able to start the game', () => {
        game.start();

        game.started.should.be.true;
    });

    it('should contain a new board to play on when game starts', () => {
        game.start();

        game.started.should.be.true;
        should.exist(game.board);
    });
});
游戏单元测试.js

var board = require('./board'),
    player = require('./player');

var game = module.exports = {
    start: start,
    started: false,
    board: board.create()
};

function start(){
    game.started = true;
};
var Board = require('./board'),
    Player = require('./player');

class Game {
    constructor() {
        this.started = false;
        this.board = new Board();
    }
    start() {
        this.started = true;
    }
}

export default Game;
const chai = require('chai'),
    should = chai.should(),
    Game = require('../../test');

let game;

describe.only('Starting the Game', () => {
    beforeEach((done) => {
        game = new Game();
        done();
    });

    it('should be able to start the game', () => {
        game.start();

        game.started.should.be.true;
    });

    it('should contain a new board to play on when game starts', () => {
        game.start();

        game.started.should.be.true;
        should.exist(game.board);
    });
});

我之前做的是通过这样做来克隆它:JSON.parse(JSON.stringify(game));其中游戏是必选模块。这对我来说很有效,但我正在寻找替代方案,以及任何人告诉我在我的情况下这样做是安全的还是正常的。是的,所以你基本上改变了这一点,不再使用节点模块,而是使用带有导出的新ES6类?有趣的是,我不必添加done(),我的测试也会在应该的时候以绿色运行并失败。还有,最后一个问题,…因此,假设我们没有为此使用ES6语法。假设它仍然是一个节点模块,语法与我的相同。那么,我的克隆是合理的,还是用我现有的语法实现它的唯一方法?我想知道人们在开始使用ES6类之前是如何创建实例的,就像我对stringify所做的那样?我使用了
const
,而不是
let/var
,因为我们只给变量赋值一次。使用
let/var
而不是
const
不会影响功能。您可以使用函数模拟ES5中的类,然后使用
new
关键字创建对象。比如
函数Apple(type){this.type=type;this.color=“red”;this.getInfo=getAppleInfo;}var Apple=new Apple('macintosh')如果答案有助于您解决问题,请接受。