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.