How to use `it.each`/ `describe.each` in Node.js native test runner?

204 Views Asked by At

How can I use it.each/describe.each, like in Jest or Vitest, for the native Node.js test runner?

1

There are 1 best solutions below

0
Raz Luvaton On BEST ANSWER

Until and unless nodejs/node#47902 is resolved:

  1. install jest-each (npm install --save-dev jest-each)

  2. 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;
    };
    
  3. when running tests, require this file:

    node --require ./add-each-support.js <rest-of-your-test-command>