Skip to content

Commit

Permalink
Modify jest config and tests to get it to work with ESM
Browse files Browse the repository at this point in the history
https://jestjs.io/docs/ecmascript-modules

Also remove a test that isn't necessary at the moment.
  • Loading branch information
Coteh committed May 22, 2024
1 parent 443c2d3 commit 0eb2d69
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 43 deletions.
48 changes: 16 additions & 32 deletions __tests__/boardmovements-test.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,43 @@
import { jest } from '@jest/globals';
import { init, selectSpot, flagSpot, getBoardInfo } from '../client/game';

jest.dontMock('../client/game');

describe('select', function () {
it('will select a spot on the board by marking it as revealed.', function () {
var app = require('../client/game');

app.init({ width: 10, height: 10, mines: 10 });
app.selectSpot(2, 2);
var boardInfo = app.getBoardInfo();
init({ width: 10, height: 10, mines: 10 });
selectSpot(2, 2);
var boardInfo = getBoardInfo();

expect(boardInfo.revealed[2][2]).toBe(true);
});
});

describe('already-select', function () {
it('will prevent the user from selecting the same spot on the board twice.', function () {
var app = require('../client/game');

app.init({ width: 10, height: 10, mines: 10 });
app.selectSpot(2, 2);
init({ width: 10, height: 10, mines: 10 });
selectSpot(2, 2);

expect(app.selectSpot(2, 2).hitInfo).toBe('alreadyhit');
expect(selectSpot(2, 2).hitInfo).toBe('alreadyhit');
});
});

//TODO
//Export all modules from app.js somehow
// describe("reveal", function(){
// it("will reveal a spot on the board using a direct call to revealSpot.", function(){
// var app = require('../client/game');
//
// app.init({width: 10, height: 10, mines: 10});
// app.revealSpot
// });
// });

describe('flag', function () {
it('will flag a spot on the board by marking it as flagged.', function () {
var app = require('../client/game');

app.init({ width: 10, height: 10, mines: 10 });
expect(app.flagSpot(2, 2).flagInfo).toBe('flagged');
var boardInfo = app.getBoardInfo();
init({ width: 10, height: 10, mines: 10 });
expect(flagSpot(2, 2).flagInfo).toBe('flagged');
var boardInfo = getBoardInfo();

expect(boardInfo.flagged[2][2]).toBe(true);
});
});

describe('unflag', function () {
it('will unflag a spot on the board by calling the flag function twice.', function () {
var app = require('../client/game');

app.init({ width: 10, height: 10, mines: 10 });
expect(app.flagSpot(2, 2).flagInfo).toBe('flagged');
expect(app.flagSpot(2, 2).flagInfo).toBe('unflagged');
var boardInfo = app.getBoardInfo();
init({ width: 10, height: 10, mines: 10 });
expect(flagSpot(2, 2).flagInfo).toBe('flagged');
expect(flagSpot(2, 2).flagInfo).toBe('unflagged');
var boardInfo = getBoardInfo();

expect(boardInfo.flagged[2][2]).toBe(false);
});
Expand Down
15 changes: 8 additions & 7 deletions __tests__/fullboard-test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { jest } from '@jest/globals';
import { init, getBoardInfo, } from '../client/game';
import { BoardOverfillException } from '../client/errors';

jest.dontMock('../client/game');
jest.dontMock('../client/errors');

describe('fullboard', function () {
it('contains a full board with default values of 10 wide, 10 high, and 10 mines.', function () {
var app = require('../client/game');
app.init();
var boardInfo = app.getBoardInfo();
init();
var boardInfo = getBoardInfo();
expect(boardInfo.width).toBe(10);
expect(boardInfo.height).toBe(10);
var mineCount = 0;
Expand All @@ -22,12 +25,10 @@ describe('fullboard', function () {

describe('overfill', function () {
it('should be able to throw an error if the board has more mines than it can physically handle.', function () {
var app = require('../client/game');
var errors = require('../client/errors');
expect(function () {
app.init({ width: 10, height: 10, mines: 101 });
init({ width: 10, height: 10, mines: 101 });
}).toThrow(
new errors.BoardOverfillException(
new BoardOverfillException(
'Amount of mines to generate exceeds amount of board pieces.'
)
);
Expand Down
8 changes: 5 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/** @type {import('jest').Config} */
const config = {
export default {
verbose: true,
reporters: ["default", "jest-junit"],
// NOTE: Node needs to be run with --experimental-vm-modules flag
// and code transforms need to be disabled in order for Jest to work with ESM.
// https://jestjs.io/docs/ecmascript-modules
transform: {}
};

module.exports = config;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dev": "vite serve",
"build": "vite build",
"format": "prettier --config ./.prettierrc --write .",
"test": "jest"
"test": "node --experimental-vm-modules node_modules/jest-cli/bin/jest.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 0eb2d69

Please sign in to comment.