Skip to content

REST helper config: onResponse provided in config does not work #5042

@apatelia

Description

@apatelia

I am using default REST helper for API testing, and I want to measure and assert the API's response time. For that I am trying following.

In my codecept.conf.ts:

  helpers: {
    REST: {
      endpoint: 'https://myServerUrl',

      onRequest: (request) => {
        console.log('onRequest');
        request.headers[ 'start-time' ] = new Date().getTime();
      },

      onResponse: (response) => {
        console.log('onResponse');
        const startTime = response.request.headers[ 'start-time' ];
        const endTime = new Date().getTime();
        const responseTime = endTime - startTime;
        response. Headers[ 'x-response-time' ] = responseTime;
      },
    },

    JSONResponse: {}
  }

and then in my custom helper:

    const response = this.helpers.JSONResponse.response;
    
    console.log(`Response time: ${response.headers[ 'x-response-time' ]} ms`);

I can see that onRequest from REST config works as expected but onResponse does not work.

My output:

onRequest
Response time: undefined ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions