-
Notifications
You must be signed in to change notification settings - Fork 30.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Global setup file for native node test runner #49732
Comments
You should run it via 'node --require ts-node/register setup.ts'. I use similar solution - kind of wrapper around 'run' function to setup different reporters (CI / local run), collect coverage with c8, globs for .ts files, additional loaders, etc. Because this wrapper is a package with 'bin' script named 'test', I can run tests via 'yarn test'. Also, adding your own config (as an example, to exclude some files from coverage) is a few lines: // wrapper
let config;
try {
config = await import('test.config.js');
} catch {
config = { /* default */ };
}
run(/* your test config */)
// config
export default {
// anything you may need
} |
Yeah this is just a syntax thing, If you build useful stuff on top of the native test runner or notice missing features please do tell us and put it on npm if you're comfortable with it being open source :) |
In my case it's an internal and very specific package created to keep some (a lot of...) defaults inside. I believe when we get native coverage reporter (with source maps & lcov output) and globs, such wrappers will be redundant for most of us. Only one thing may be really required - some way to organize reporters (but we already have .env support, right?). |
Right, and since the programatic invocation is just a Node.js file you can use whatever you want for config there :) |
Sure I'll try to come up with something |
Reopening as you also can't do global teardown |
actually you can do: const {tap} = require('node:test/reporters');
const process = require('node:process');
const path = require("node:path");
const {run} = require("node:test");
const {finished} = require('node:stream');
const stream = run({
files: ['./src/a.test.js', './src/b.test.js'],
concurrency: false,
setup: () => {
console.log('setup');
}
})
.compose(tap);
finished(stream, () => {
console.log('teardown');
});
stream.pipe(process.stdout); But it's only for when using |
There has been some discussion about Node.js as a whole supporting a configuration file. I'm not sure if that will lead to anything, but having a separate file for just the test runner would not be great. |
Would be awesome to have it documented. What would be even better is to have it working with some sort of file detection, like: My global test setup is the following:
I have a couple of test suites coming. Would be nice, if all tests unit-integration-e2e could be imported with a one-liner. |
I think the issue I'm having is related to this. I'm writing an internal utility for our tests that is built around the native test-runner. I need to make sure a module executes inside each test process before the test file is actually run. The edit: process.execArgv.push('--import', path.resolve(__dirname, 'index.js')); |
It's always been here - https://nodejs.org/dist/latest-v21.x/docs/api/cli.html#node_optionsoptions. Just set that variable in process.env before |
@koshic I'd still consider that a hack compared to explicitly having a means of specifying flags and env on the |
Thanks for sharing the workaround @Twipped! I agree it feels like a hack, especially because both using I guess a way to fix that is to start your setup module with |
My use cases for global setup file is to e.g. start a database container or create local cloud resources before running integration tests. This functionality is supported by popular test runners:
Using |
Landed here looking for a global setup. Ideally in such a way that each of my tests receives the context I created in the global setup function. globalSetup(context) {
context.driver = something
}
test("Can ....", context){
context.driver.do()
} |
I have some ideas how to define 1. Use
|
I think option 1 or 2 sound the best (they honestly sound like the same thing just a difference of where the hook function lives). A few things to note are:
We should probably add this anyway. EDIT: After thinking a bit, option 1 is probably better. With option 2 the test runner will have to search for the exported functions in all cases, which slows things down. |
### Details: - Remove testing models from repository - Add `setup.js` script that will be called once to download testing models - Before running the suite check if model is downloaded, add top describe to *.test.js files ### Notes: Currently there is no global setup file for native [node test runner](nodejs/node#49732) as in other test runner e.g. [jest](https://jestjs.io/docs/configuration#globalsetup-string) ### Tickets: - [CVS-146345](https://jira.devtools.intel.com/browse/CVS-146345) --------- Co-authored-by: Vishniakov Nikolai <nikolai.vishniakov@intel.com>
So as mentioned in #54675, in Node 22.9 I managed to achieve node --import ./globalHooks.mjs --experimental-test-isolation=none --test 'src/**/*.test.js'`
import { after, before } from 'node:test';
before(async () => {
await startDockerContainers();
});
after(async () => {
await closeDatabaseConnections();
await stopDockerContainers();
}); Works as expected 🎉 also with TypeScript (I need to test TS with ESM more though) Does it mean that adding |
I'm wondering if we can do something like
import { after, before, isMainRunner } from 'node:test';
if (isMainRunner) {
before(async () => {
await startDockerContainers();
});
after(async () => {
await closeDatabaseConnections();
await stopDockerContainers();
});
} Which would work regardless of isolation mode. |
I think we ultimately need something similar to Mocha's global fixtures, which is guaranteed to run once and only once no matter the isolation mode. Otherwise we're pushing work on to users (work that will be duplicated for all projects that want this functionality) when it would be significantly easier for us to do it in the test runner. That way, users also only need to remember that these hooks always run in the same process as the test runner itself. |
Just wanted to add to the thread because I was searching for TypeScript support with global setup and couldn't quite figure it out. I finally did, so I thought this might be helpful for others. Using tsx, what the Node v22.x.x docs reference, I was able to write a TypeScript setup and test with no configuration tsx --import ./tests/node/globalHooks.ts --experimental-test-isolation=none --test './**/*.test.{js,ts}'
import { after, before } from 'node:test';
before(async () => {
console.log('before');
});
after(async () => {
console.log('after');
}); |
This is gold @olingern. Been struggling with this for so long myself 🙇🏼 |
Just what I needed! |
What is the problem this feature will solve?
I'm sorry if there's already such a feature and I haven't found a way how to solve this. Basically what I'm looking for is to run some function that would run beofre all the tests and setup the environment. So far the only way how to setup some environment is doing it with the run function but that is only limited for each test suite from what I've tried. I would love to have the have some setup file in the way how does it work for example in jest or mocha or other testing framework.
What is the feature you are proposing to solve the problem?
Have a
setup.js/setup.ts
file that will run before all the tests, you would somehow specify in command that starts the tests this setup file.What alternatives have you considered?
node version:
v20.6.1
npm version:
9.8.1
I've tried this setup file
setup.ts
suite.test.ts
and I'm running this command to execute tests:
node --require ts-node/register --test
but neither the setup.ts file is executed nor the suite.test.ts file is executed
The text was updated successfully, but these errors were encountered: