I am using the phaser-ce version 2.20.0 in my project.
Although being aware of the newer phaser version, I have to stick to the mentioned CE (community edition) version.
When I run a simple test written with Jest, the test suite fails with the following error:
ReferenceError: PIXI is not defined
(PixiJS (commonly referred to as Pixi) is a 2D graphics rendering using within Phaser.)
Here is my setup:
Project Structure:
phaser-jest-project/
│
├── dist/
├── src/
│ ├── index.js
│ └── game.js
├── test/
│ └── game.test.js
├── package.json
├── webpack.config.js
└── .babelrc
package.json:
{
"name": "phaser-ce-jest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "webpack serve --open",
"build": "webpack",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"phaser-ce": "^2.20.0"
},
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.3",
"@jest/globals": "^29.7.0",
"babel-loader": "^9.1.3",
"jest": "^29.7.0",
"webpack": "^5.93.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
},
"jest": {
"transform": {
"^.+\.js$": "babel-jest"
}
}
}
webpack.config.js:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env'],
},
},
},
],
},
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
compress: true,
port: 9000,
},
};
.babelrc:
{
"presets": ["@babel/preset-env"]
}
src/index.js:
import Phaser from 'phaser-ce';
import { createGame } from './game';
const game = createGame();
src/game.js:
import Phaser from 'phaser-ce';
export function createGame() {
const game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload, create, update });
function preload() {
// Load assets
}
function create() {
// Initialize game
}
function update() {
// Game loop
}
return game;
}
test/game.test.js:
import { createGame } from '../src/game';
import { it, expect } from '@jest/globals';
it('game should be created', () => {
const game = createGame();
expect(game).toBeDefined();
});
Question:
How can I resolve the “ReferenceError: PIXI is not defined” error when running Jest tests in a Phaser-CE project? Any help or guidance would be greatly appreciated.
What I’ve Tried:
- Ensuring that Phaser-CE is correctly installed and imported.
- Checking for any missing dependencies related to PIXI.
- Searching for similar issues online but haven’t found a solution that works with my setup.
Carsten Linder is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.