Hi,
in Workflows we had to do some adjustments regarding the test setup to run the tests with Jest.
I looked in our PRs and I will summarize the changes we did.
Let’s start with the package.json.
The following dependencies inside our package.json were added as dev dependencies:
"@jest/globals": "^29.7.0",
"@swc/core": "^1.11.16",
"@swc/jest": "^0.2.37",
"@types/jest": "^29.5.3",
"jest-environment-jsdom": "^29.7.0",
"jest-environment-node": "^29.7.0",
Babel related dependencies like “babel-jest” were removed.
Next a setup.ts was added inside the tests directory of our frontend-subproject workflows-core. This is the path to the setup.ts: “workflows-core/src/tests/setup.ts”
The content of the setups.ts:
import { jest } from "@jest/globals";
globalThis.jest = jest;
The babel.config.js was removed and jest.config.js was renamed to jest.config.mjs. This is the relevant content:
export default {
testEnvironment: "jsdom",
extensionsToTreatAsEsm: [".ts", ".tsx"],
transform: {
"^.+\\.(t|j)s$": "@swc/jest"
},
// necessary because diagram-js and bpmn-js are not in the right format
transformIgnorePatterns: ["node_modules/(?!bpmn-js|diagram-js|react-dnd|dnd-core|@react-dnd|antlr4)"],
// Fixes .js extension resolution for ESM imports
moduleNameMapper: { "^(\\.{1,2}/.*)\\.js$": "$1" },
globals: {
NODE_ENV: "test"
},
verbose: true,
// The root directory that Jest should scan for tests and modules within
rootDir: "src",
// An array of directory names to be searched recursively up from the requiring module's location
moduleDirectories: ["<rootDir>/../../node_modules", "<rootDir>/../node_modules"],
// A list of paths to directories that Jest should use to search for files in
roots: [
"tests"
],
// A list of paths to modules that run some code to configure or set up the testing framework before each test
setupFilesAfterEnv: ["<rootDir>/tests/setup.ts"],
[...]
};
Note that with […] I just want to hint that there are some other configurations in our files, that are not relevant for the ESM + Jest test setup.
Inside our package.json we had some scripts to run the tests. They looked like this:
"scripts": {
[...]
"test:unit": "jest unit --passWithNoTests",
"test:int": "jest int --passWithNoTests",
"test:all": "jest --passWithNoTests",
"test:coverage": "jest --coverage",
[...]
}
These scripts would not work and had to be adjusted like this:
"scripts": {
[...]
"test:unit": "npm run jest -- unit --passWithNoTests",
"test:int": "npm run jest -- int --passWithNoTests",
"test:all": "npm run jest -- --passWithNoTests",
"test:coverage": "npm run jest -- --coverage",
[...]
"jest": "node --experimental-vm-modules ../node_modules/jest/bin/jest.js"
}
The flag “–experimental-vm-modules” is also needed to be configured in the “Run Configuration”, if you want to run tests via IntelliJ (probably works similar with different IDEs).
Open the “Run configuration” and paste “–experimental-vm-modules” in the “Node options” field.