Skip to content

Commit

Permalink
test: refactor tests and use new matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
kurpav committed Dec 12, 2023
1 parent 0a883a2 commit cb20877
Show file tree
Hide file tree
Showing 9 changed files with 1,019 additions and 1,040 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test:e2e": "node ./test/mock-server & jest -c test/jest.e2e.config.js",
"posttest:e2e": "kill -9 $(lsof -t -i:3001)",
"clean": "rm -rf dist/ && rm -rf supergood-*.log",
"build": "yarn run clean && tsc"
"build": "yarn run clean && tsc -p ./tsconfig.lib.json"
},
"dependencies": {
"headers-polyfill": "^4.0.2",
Expand Down Expand Up @@ -64,6 +64,7 @@
"eslint-plugin-jest": "^27.1.3",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.2.1",
"jest-extended": "^4.0.2",
"json-server": "^0.17.0",
"openai": "^4.10.0",
"postgres": "^3.3.4",
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import NodeCache from 'node-cache';
import { serialize } from 'v8';
import {
getHeaderOptions,
logger,
Expand Down Expand Up @@ -138,7 +139,7 @@ const Supergood = () => {
config: supergoodConfig,
metadata: {
requestUrl: request.url.toString(),
payloadSize: new Blob([request as any]).size,
payloadSize: serialize(request).length,
...supergoodMetadata
}
},
Expand Down Expand Up @@ -183,9 +184,7 @@ const Supergood = () => {
metadata: {
...supergoodMetadata,
requestUrl: requestData.url,
payloadSize: responseData
? new Blob([responseData as BlobPart]).size
: 0
payloadSize: responseData ? serialize(responseData).length : 0
}
},
e as Error
Expand Down Expand Up @@ -275,7 +274,7 @@ const Supergood = () => {
config: supergoodConfig,
metadata: {
numberOfEvents: data.length,
payloadSize: new Blob([(data || {}) as any]).size,
payloadSize: serialize(data).length,
requestUrls: data.map((event) => event?.request?.url),
...supergoodMetadata
}
Expand Down
61 changes: 43 additions & 18 deletions test/e2e/core.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ describe('core functionality', () => {
await axios.get(`${MOCK_DATA_SERVER}/posts`);
}
await Supergood.close();
// checking that all events were posted
expect(postEventsMock).toHaveBeenCalledWith(
expect.any(String),
Array(numberOfHttpCalls).fill(expect.anything()),
expect.toBeArrayOfSize(numberOfHttpCalls),
expect.any(Object)
);
expect(postEventsMock).toHaveBeenCalledWith(
Expand Down Expand Up @@ -69,21 +70,32 @@ describe('core functionality', () => {
},
SUPERGOOD_SERVER
);
for (let i = 0; i < httpErrorCodes.length; i++) {
for (const code of httpErrorCodes) {
try {
await axios.get(`${MOCK_DATA_SERVER}/${httpErrorCodes[i]}`);
await axios.get(`${MOCK_DATA_SERVER}/${code}`);
} catch (e) {
// ignore
}
}
await Supergood.close();
const eventsPosted = getEvents(postEventsMock);
expect(eventsPosted.length).toEqual(httpErrorCodes.length);
expect(
eventsPosted.every((event) =>
httpErrorCodes.includes(event.response.status)
)
).toBeTruthy();

// checking that all events were posted
expect(postEventsMock).toHaveBeenCalledWith(
expect.any(String),
expect.toBeArrayOfSize(httpErrorCodes.length),
expect.any(Object)
);
expect(postEventsMock).toHaveBeenCalledWith(
expect.any(String),
expect.arrayContaining([
expect.objectContaining({
response: expect.objectContaining({
status: expect.toBeOneOf(httpErrorCodes)
})
})
]),
expect.any(Object)
);
});
});

Expand All @@ -100,16 +112,29 @@ describe('core functionality', () => {
axios.get(`${MOCK_DATA_SERVER}/200?sleep=2000`);
await sleep(1000);
await Supergood.close();
const eventsPosted = getEvents(postEventsMock);
const firstEvent = eventsPosted[0];
expect(eventsPosted.length).toEqual(1);
expect(firstEvent.request.requestedAt).toBeTruthy();
expect(firstEvent?.response?.respondedAt).toBeFalsy();
});

// checking that all events were posted
expect(postEventsMock).toHaveBeenCalledWith(
expect.any(String),
expect.toBeArrayOfSize(1),
expect.any(Object)
);
expect(postEventsMock).toHaveBeenCalledWith(
expect.any(String),
expect.arrayContaining([
expect.objectContaining({
request: expect.objectContaining({
requestedAt: expect.any(Date)
})
})
]),
expect.any(Object)
);
}, 10000);

// Causes the github actions to hang for some reason
test.skip('posting errors', async () => {
postEventsMock.mockImplementation(() => {
test('posting errors', async () => {
postEventsMock.mockImplementationOnce(() => {
throw new Error();
});
await Supergood.init(
Expand Down
2 changes: 1 addition & 1 deletion test/jest.e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
clearMocks: true,
roots: ['<rootDir>/e2e'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
'^.+\\.(ts|tsx)$': ['ts-jest', { tsconfig: './test/tsconfig.test.json' }],
'^.+\\.(js)$': 'babel-jest'
},
transformIgnorePatterns: [],
Expand Down
3 changes: 3 additions & 0 deletions test/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// add all jest-extended matchers
import * as matchers from 'jest-extended';
expect.extend(matchers);
6 changes: 6 additions & 0 deletions test/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["jest", "node"]
}
}
6 changes: 1 addition & 5 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist",
"allowJs": true,
"removeComments": true,
"target": "es2016",
Expand All @@ -15,7 +13,5 @@
"moduleResolution": "node",
"sourceMap": true,
"allowSyntheticDefaultImports": true
},
"include": ["./src"],
"exclude": ["node_modules", "**/*.test.ts"]
}
}
9 changes: 9 additions & 0 deletions tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist"
},
"include": ["./src"],
"exclude": ["node_modules", "**/*.test.ts"]
}
Loading

0 comments on commit cb20877

Please sign in to comment.