Hello,
I’m trying to get Jest setup with p5.js. I’ve managed to get it all working with npm and able to run some basic unit tests however I’m having an issue when Jest hits a piece of p5 code.
I have a sketch.js file which has this piece of code in it:
export const config = {
'lanes': null,
'tileColours': {
'red': color(255, 0, 0),
'green': color(0, 255, 0),
'blue': color(0, 0, 255),
'yellow': color(255, 255, 0),
'purple': color(255, 0, 255)
}
};
I then have a belt.js file which is the file I’m wanting to unit test. It includes this code:
import config from ‘./sketch’;
/* exported Belt */
export class Belt {
constructor() {
this.belt = this.make2DArray(config.lanes, config.lanes);
}
...
};
In my unit test when I import the Belt class with import {Belt} from '../belt';
I get an error from Jest:
FAIL test/belt.test.js
● Test suite failed to run
ReferenceError: color is not defined
8 | 'tileColours': {
> 9 | 'red': color(255, 0, 0),
| ^
10 | 'green': color(0, 255, 0),
11 | 'blue': color(0, 0, 255),
12 | 'yellow': color(255, 255, 0),
at Object.color (sketch.js:9:16)
at Object.<anonymous> (belt.js:2:1)
at Object.<anonymous> (test/belt.test.js:1:1)
In other languages when something like this occurs I’d normally override the color variable in my test. I’ve tried different ways of doing this in JS but nothing seems to be working.
Any help you can give would be appreciated.
Thanks,
Tim