Can I capture tests from test.each tagged template literals for another context?

636 Views Asked by At

Hundreds of automated tests to verify calculations, written using Jest's jest.each feature, need to be presented to decision-makers in another, more legible form. A typical test suite would start something like this (example numbers):

describe('Drug 1', () => {
test.each`
weight  | vial   | expectedMg | expectedMl  | clinicalCaseNumber
${140}  | ${250} | ${10}      | ${2}        | ${'I13'}
${140}  | ${500} | ${10}      | ${1}        | ${'I14'}

I would like to access the test data in my *.test.js files from the React app being tested so that permissioned users can review the automated tests being run.

I had hoped to do something like this:

export const DRUGS = {};
DRUGS['Drug 1'] = `
    weight  | vial   | expectedMg | expectedMl  | clinicalCaseNumber
    ${140}  | ${250} | ${10}      | ${2}        | ${'I13'}
    ${140}  | ${500} | ${10}      | ${1}        | ${'I14'}

And then use the template literals both with test.each here and within the application. However, this doesn't seem to be possible?

1

There are 1 best solutions below

2
On BEST ANSWER

You can create a separate script as an entry point

package.json
{
  "scripts": {
    "test": "jest",
    "dump": "node scripts/dump.js",
    "posttest": "npm run dump"
  }
}

You can reuse jest-each to define a global test function and create the desired output

scripts/dump.js
const bindEach = require('jest-each').bind;
const glob = require('glob');

global.describe = (desc, cb) => {
  console.log(desc);
  cb();
};

const test = (desc, cb) => {
  console.log(desc);
};

test.each = bindEach(test);

global.test = test;

glob.sync('**/?(*.)+(spec|test).[tj]s?(x)').forEach((testFile) => {
  require(`./${testFile}`);
});