Skip to content

Commit

Permalink
test: minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kurpav committed Nov 9, 2023
1 parent eb43f96 commit 2cc4f7f
Showing 1 changed file with 31 additions and 29 deletions.
60 changes: 31 additions & 29 deletions test/core.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import Supergood from '../src';
import { postEvents, postError } from '../src/api';
import { initialize } from './json-server-config';
import { errors } from '../src/constants';
import { request } from 'undici';
import { ErrorPayloadType, EventRequestType } from '../src/types';
import initialDB from './initial-db';
import http from 'http';
import fs from 'fs';
import path from 'path';
import get from 'lodash.get';

// HTTP libraries
import { request } from 'undici';
import superagent from 'superagent';
import axios from 'axios';
import fetch from 'node-fetch';

import Supergood from '../src';
import { postEvents, postError } from '../src/api';
import { initialize } from './json-server-config';
import { errors } from '../src/constants';
import { ErrorPayloadType, EventRequestType } from '../src/types';
import initialDB from './initial-db';

import { sleep } from '../src/utils';

const base64Regex =
Expand All @@ -40,7 +42,19 @@ const defaultConfig = {

let server: http.Server;

beforeAll(async () => {
const getEvents = (mockedPostEvents: jest.Mock): Array<EventRequestType> => {
return Object.values(
mockedPostEvents.mock.calls.flat()[1] as EventRequestType
);
};

const getErrors = (mockedPostError: jest.Mock): ErrorPayloadType => {
return Object.values(
mockedPostError.mock.calls.flat()
)[1] as ErrorPayloadType;
};

const resetDatabase = () => {
fs.writeFileSync(
path.join(__dirname, 'db.json'),
JSON.stringify(initialDB, null, 2),
Expand All @@ -49,33 +63,21 @@ beforeAll(async () => {
flag: 'w'
}
);
};

beforeAll(async () => {
resetDatabase();
server = await initialize();
});

afterAll(async () => {
server.close();
fs.writeFileSync(
path.join(__dirname, 'db.json'),
JSON.stringify(initialDB, null, 2),
{
encoding: 'utf8',
flag: 'w'
}
);
resetDatabase();
});

const getEvents = (mockedPostEvents: jest.Mock): Array<EventRequestType> =>
Object.values(mockedPostEvents.mock.calls.flat()[1] as EventRequestType);

const getErrors = (mockedPostError: jest.Mock): ErrorPayloadType => {
return Object.values(
mockedPostError.mock.calls.flat()
)[1] as ErrorPayloadType;
};

jest.mock('../src/api', () => ({
postEvents: jest.fn(async (eventSinkUrl, data) => ({ data })),
postError: jest.fn(async (errorSinkUrl, payload) => ({
postEvents: jest.fn(async (_, data) => ({ data })),
postError: jest.fn(async (_, payload) => ({
payload
}))
}));
Expand Down Expand Up @@ -171,7 +173,7 @@ describe('testing failure states', () => {
await axios.get(`${HTTP_OUTBOUND_TEST_SERVER}/posts`);
await Supergood.close();
const postedErrors = getErrors(postError as jest.Mock);
expect(postError as jest.Mock).toBeCalled();
expect(postError as jest.Mock).toHaveBeenCalled();
expect(postedErrors.message).toEqual(errors.POSTING_EVENTS);
});
});
Expand Down Expand Up @@ -498,7 +500,7 @@ describe('local client id and secret', () => {
});
});

xdescribe('testing openAI', () => {
describe('testing openAI', () => {
test('simple chat completion call being logged', async () => {
/* eslint-disable-next-line @typescript-eslint/no-var-requires */
const OpenAI = require('openai');
Expand Down

0 comments on commit 2cc4f7f

Please sign in to comment.