0

I've tried a basic API test using two different libs - nock and msw - and both of them work locally. However, when running in Github Actions, they both fail with the same error:

nock:

FAIL src/CustomHeader.test.ts
  ● Test suite failed to run

    TypeError: Cannot set property request of [object Object] which has only a getter

      at node_modules/nock/lib/common.js:95:20
          at Array.forEach (<anonymous>)
      at Object.overrideRequests (node_modules/nock/lib/common.js:71:22)
      at activate (node_modules/nock/lib/intercept.js:373:10)
      at Object.setup (node_modules/nock/lib/back.js:115:5)
      at Function.Object.<anonymous>.Back.setMode (node_modules/nock/lib/back.js:330:9)
      at Object.<anonymous> (node_modules/nock/index.js:52:8)

msw:

 FAIL src/CustomHeader.test.ts
  ● Client request() with custom header › Valid additional header

    TypeError: Cannot set property request of [object Object] which has only a getter

      12 |
      13 | describe('Client request() with custom header', () => {
    > 14 |   beforeAll(() => server.listen());
         |                          ^
      15 |   afterEach(() => server.resetHandlers());
      16 |   afterAll(() => server.close());
      17 |

      at _loop_1 (node_modules/@mswjs/interceptors/src/interceptors/ClientRequest/index.ts:67:28)
      at ClientRequestInterceptor.Object.<anonymous>.ClientRequestInterceptor.setup (node_modules/@mswjs/interceptors/lib/interceptors/ClientRequest/index.js:106:17)
      at ClientRequestInterceptor.Object.<anonymous>.Interceptor.apply (node_modules/@mswjs/interceptors/src/Interceptor.ts:127:10)
      at _loop_1 (node_modules/@mswjs/interceptors/src/BatchInterceptor.ts:43:19)
      at BatchInterceptor.Object.<anonymous>.BatchInterceptor.setup (node_modules/@mswjs/interceptors/lib/BatchInterceptor.js:58:17)
      at BatchInterceptor.Object.<anonymous>.Interceptor.apply (node_modules/@mswjs/interceptors/src/Interceptor.ts:127:10)
      at Object.listen (node_modules/msw/src/node/createSetupServer.ts:122:21)
      at src/CustomHeader.test.ts:14:26

Here's the relevant parts of the test:

msw:

import { rest, RestContext } from 'msw';
import { setupServer } from 'msw/node';

const mockBody = { test: 'result' };
const server = setupServer(rest.get('https://localhost/path', (_: never, res: any, ctx: RestContext) => res(ctx.status(200), ctx.json(mockBody))));

describe('Client request() with custom header', () => {
  beforeAll(() => server.listen());
  afterEach(() => server.resetHandlers());
  afterAll(() => server.close());

  test('Valid additional header', async () => {
    const client = new Client(PROXY_HOST, API_KEY);
    const customHeaders = {
      'test-header': 'test-value',
    };

    const input = ['GET', '/path', 'query=string', 'body'];
    const event = getAPIGatewayProxyEvent(input);
    const response = await client.request(event, customHeaders);
    expect(response).toStrictEqual({
      body: JSON.stringify({ test: 'result' }),
      headers: {
        'content-type': 'application/json',
        'test-header': 'test-value',
      },
      statusCode: 200,
    });
  });

nock:

describe('Client request() with custom header', () => {
  afterEach(() => nock.cleanAll());

  test('Valid additional header', async () => {
    nock('https://localhost')
      .get('/path')
      .query(true) // Ignores query string
      .reply(200, { test: 'result' });
    const client = new Client(PROXY_HOST, API_KEY);
    const customHeaders = {
      'test-header': 'test-value',
    };

    const input = ['GET', '/path', 'query=string', 'body'];
    const event = getAPIGatewayProxyEvent(input);
    const response = await client.request(event, customHeaders);
    expect(response).toStrictEqual({
      body: JSON.stringify({ test: 'result' }),
      headers: {
        'content-type': 'application/json',
        'test-header': 'test-value',
      },
      statusCode: 200,
    });
  });

What needs to be changed so that the test will also work in Github Actions? I assume it has some sort of internal protection against intercepting requests.

Juha Untinen
  • 1,806
  • 1
  • 24
  • 40

1 Answers1

1

This is a very cryptic error you're experiencing. If you can reproduce it across different libraries, I suspect something higher in the chain is responsible.

Here are a couple of things to try:

  1. Ensure you're using the same environment (i.e. Node.js version) both locally and on the CI. Always have the Node.js version explicitly configured for the CI.
  2. Ensure the same dependency versions of the tools you're using across the local and the CI environments.
  3. Look into the stack trace that reports this error. What kind of foo.request the code is trying to set? What does it relate to? This can stir you in the right direction.
kettanaito
  • 974
  • 5
  • 12