vendredi 28 février 2020

How to get Jest test name and result statistics

I have a number of Jest test files and in each of them, I have something like this:

const dName = 'A';
describe( dName, () => {
  beforeAll( () => {
    _.timings[dName] = Date.now();
  }
  afterAll( () => {
    _.timings[dName] = Date.now() - _.timings[dName];
  }

  const tName1 = 'B';
  test( tName1, () => {
    const startTime = Date.now();

    //do test here and set isPassed=true if test passed

    _.timings[tName1] = Date.now() - startTime;
    _.result[tName1].passed = isPassed;
  } 
}

afterAll( () => {
  generateReport( _ );
}

Each test file contains multiple describes and in each describe there are multiple tests.

Firstly, is there a better way to retrieve the describe (or test) name, rather than what I am doing currently which is defining a variable (dName, tName1) and passing that through? Ideally I could do something like:

test( 'B', () => {
  _.timings[ getTestName() ] = ... // where getTestName() would return B somehow.

I was hoping I could use Javascripts arguments variable to see the contents of the first argument (since that should be the name), however arguments doesn't seem to contain the information that I can extract ... or at least I don't know how to decipher the info stored in arguments.

Secondly, is there a better way to get the results of a test run? As it is now, I need to define a variable (isPassed) and populate it accordingly in each test so that at the end I can record this information in the _.results[testName] object. This object is used at the very end in generateReport to create a file containing the names of all the tests and the status of whether each test passed or failed.

Aucun commentaire:

Enregistrer un commentaire