Closed bhvngt closed 2 years ago
Can you please describe the intent of your global setup when running in Wallaby context?
As you are no doubt aware, Wallaby runs your tests multiple times. In your case, you have Wallaby configured to start a new process for each test execution (because you're using mocha
with esm
).
Can you describe what global setup is doing? Do you need it to run for each process when it starts? Or is the code in your global setup OK to run just once when Wallaby starts? How you solve the problem may differ a little depending on what you're looking to do.
Regardless of when you need the code to run, we also put together some sample code that should help:
package.json
In our project root package.json, we had type
set to module
. I assume you also have this.
setup.js Sample setup file in project root that is an ES module (doesn't actually do anything in our case)
import { readFileSync } from 'fs';
console.log(readFileSync);
wallaby.cjs
Make sure Wallaby configuration file has .cjs
extension instead of .js
extension (because of package.json). Make setup() function async
and then import your setup file.
module.exports = function (wallaby) {
return {
...
setup: async function (wallaby) {
const path = require("path");
await import(path.join(wallaby.localProjectDir, "setup.js"));
},
};
};
If you're still having problems getting it working, please let us know what you need to execute and when. We should be able to help.
Hi @smcenlly I am seeding my local db with few records before my tests began. So thats the code that is running within global fixture. It needs to run only once and need not run for each test run or for each process.
Code within the globalFixture is as follows
import pg from "pg";
import { AUTH_USER_EMAIL, AUTH_PASSWORD } from "./_constants";
export async function mochaGlobalSetup() {
// inserting few records using pg.
}
Typically this fixture is called by mocha using its require configuration flag.
I was able to make it run within the wallaby setup using dynamic import
based on your example.
For my understanding, does the setup code runs for each test process spawned by wallaby or does it run only once per wallaby instance? If it is former then Is there a way to run a code only once per wallaby instance?
The setup
code is run every time in the context of the process running your tests. By default, you should have multiple processes running your tests at the same time (assuming you have more than one test file).
If you only want to initialise the database once on Wallaby startup then you may instead change your module.export
to return an async function. For example:
module.exports = async function (wallaby) {
if (!process.env.DB_INITIALIZED) {
await someAsynchronousSetupFunction();
process.env.DB_INITIALIZED = true;
}
return {
...
}
};
In the example above, we use a process environment variable that is passed to all child worker processes to ensure that the initialisation code only runs once.
If your tests have not been designed to run in multiple processes at the same time and you run into problems, you can use Wallaby's workers setting to limit the concurrency.
Issue description or question
My project is currently using mocha's Global Fixtures to do one time setup. I have a mono repo. Hence I have multiple such fixtures configured for each of the sub-projects. I have tried following configurations
Here it throws
[ERR_REQUIRE_ESM]: require() of ES Module
since myglobalSetup.js
files areES modules
. Dynamic import usingawait import
and making setup functionasync
does not compile.Here
mocha.require
is not a method. Settingmocha.options.require
also does not work.Appreciate your help here.
Wallaby diagnostics report