How to pass data from a test to a reporter in jest?

Viewed 406

I'm using a custom jest reporter to populate data in testrail (a test case management software) and would like my jest tests to be the source of truth for all data being fed into the test case management software.

I've been struggling a bit to understand how I could pass additional data from the test to the reporter. I'm testing a GraphQL API, and would like the actual API payload to make its way, from the test to testrail, plus eventually additional metadata later on.

The only data elements I'm able to use are:

  ancestorTitles: [Array],
  duration: 52,
  failureMessages: [],
  fullName: 'Test suite - test case',
  location: null,
  numPassingAsserts: 0,
  status: 'passed',
  title: 'test case'

For example, a test case looks like this:

describe('My Test Suite', () => {
  test('My test case', async done => {        
    const query = `
       {
         query {
           documents {
             totalCount
           }
         }
       }`
    const response = await graphQL(query, global.apiConfig)
    const hits = response.data.documents.totalCount
    expect(hits).toHaveLength(4)
    done();
  }
)

How could I have query passed down to the reporter?

1 Answers

@FrancoiG Until I find something better I did following. I am using TestRail as well. So my each test name starts with case number from testRail.

import testCustomData from ('./config/testCustomData.json')

describe('My Test Suite', () => {
  test('C111: My test case', async done => {        
    const customData = testCustomData.C111;
    ...
    ...
  }
)

With this same code I can access test data from Jest TestRail Reporter as case id is present in test name. In my situation data was not dynamically generated so I can use this approach.

In your case if query id dynamically created then you can generate file on the fly where you will store it with testrail or any other unique identifier key. Here in my case was: C111. Then you can access it from Jest Reporter. It worked or me, but I hope there is some better solution, like extending testResult with custom data, etc.

Related