Skip to content

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

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

Activity

  1. myrepojuly commented on Aug 23, 2025

    @myrepojuly

    @kobenguyent @DavertMik - The issue is with the onResponse Handler Overridden in lib/helper/JSONResponse.js file

    The problem lies in the lib/helper/JSONResponse.js file, specifically in the _beforeSuite() method.

    Currently, the following code unconditionally overrides any existing onResponse handler set in the request helper config:

    _beforeSuite() {
       this.response = null
       if (!this.helpers[this.options.requestHelper]) {
         throw new Error(`Error setting JSONResponse, helper ${this.options.requestHelper} is not enabled in config, helpers: ${Object.keys(this.helpers)}`)
       }
       // connect to REST helper
       this.helpers[this.options.requestHelper].config.onResponse = response => {
         this.response = response
       }
     }
    

    Problem:
    This implementation overrides any custom onResponse handler that may have already been set in the user's configuration, causing their logic to be lost.

    Suggested Fix:
    Preserve the original onResponse handler by wrapping it, rather than replacing it:

    const originalOnResponse = this.helpers[this.options.requestHelper].config.onResponse
    this.helpers[this.options.requestHelper].config.onResponse = response => {
      if (originalOnResponse) originalOnResponse(response)
      this.response = response
    }
    

    This ensures that both the user-defined handler and the JSONResponse logic are executed.

  2. added 2 commits that reference this issue on Aug 24, 2025
    aa987ae
    b37b187
  3. added a commit that references this issue on Aug 26, 2025
    e94fc98
  4. added a commit that references this issue on Sep 1, 2025
    e371c09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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