How can I use it.each
/describe.each
, like in Jest or Vitest, for the native Node.js test runner?
Until and unless nodejs/node#47902
is resolved:
install jest-each
(npm install --save-dev jest-each
)
create add-each-support.js
file with the code below
const {default: each} = require('jest-each');
const { it, test, describe } = require('node:test');
const eachWithNode = each.withGlobal({
describe,
it,
test
})
// This is needed for the jest-each to work
it.concurrent = test.concurrent = () => {
throw new Error('concurrent is not supported, added just to allow it.each and describe.each');
};
describe.each = function (...args) {
return eachWithNode(...args).describe;
};
it.each = (...args) => {
return eachWithNode(...args).it;
};
test.each = (...args) => {
return eachWithNode(...args).test;
};
when running tests, require this file:
node --require ./add-each-support.js <rest-of-your-test-command>