Skip to content

Commit

Permalink
Bump i18next, opentelemetry and remove need for jest-fetch-mock
Browse files Browse the repository at this point in the history
in some tests

update opentelemetry packages

yarn.lock update to get only one version of @babel/runtime
update i18next, closes #1071

replace jest-fetch-mock in chaire-lib-backend and OSMOverpassDownloader
tests in chaire-lib-common by node-fetch in tests

See packages/chaire-lib-common/src/utils/osm/__tests__/OsmOverpassDownloader.test.ts
for the new mocking strategy.
  • Loading branch information
kaligrafy committed Nov 20, 2024
1 parent 1285160 commit 64fb15c
Show file tree
Hide file tree
Showing 9 changed files with 754 additions and 462 deletions.
21 changes: 10 additions & 11 deletions packages/chaire-lib-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"dependencies": {
"@casl/ability": "^5.4.4",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/core": "^1.27.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.54.2",
"@opentelemetry/instrumentation": "^0.54.2",
"@opentelemetry/instrumentation-express": "^0.44.0",
"@opentelemetry/instrumentation-http": "^0.54.2",
"@opentelemetry/resources": "^1.27.0",
"@opentelemetry/sdk-trace-node": "^1.27.0",
"@opentelemetry/sdk-trace-base": "^1.27.0",
"@opentelemetry/core": "^1.28.0",
"@opentelemetry/exporter-trace-otlp-grpc": "^0.55.0",
"@opentelemetry/instrumentation": "^0.55.0",
"@opentelemetry/instrumentation-express": "^0.45.0",
"@opentelemetry/instrumentation-http": "^0.55.0",
"@opentelemetry/resources": "^1.28.0",
"@opentelemetry/sdk-trace-node": "^1.28.0",
"@opentelemetry/sdk-trace-base": "^1.28.0",
"@turf/turf": "^7.1.0",
"@zeit/fetch-retry": "^5.0.1",
"bcryptjs": "^2.4.3",
Expand All @@ -48,8 +48,8 @@
"fs-extra": "^11.2.0",
"geojson": "^0.5.0",
"glob": "^7.2.3",
"i18next": "^22.4.15",
"i18next-fs-backend": "^2.1.1",
"i18next": "^23.16.5",
"i18next-fs-backend": "^2.3.2",
"inquirer": "^7.3.3",
"inquirer-file-tree-selection-prompt": "^1.0.7",
"knex": "^3.1.0",
Expand Down Expand Up @@ -103,7 +103,6 @@
"eslint-plugin-n": "^15.7.0",
"jest": "^29.7.0",
"jest-each": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"mock-spawn": "^0.2.6",
"nodemailer-mock": "^1.5.8",
"prettier-eslint-cli": "^8.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,37 @@
* License text available at https://opensource.org/licenses/MIT
*/
import json2CapnpService from '../Json2CapnpService';
import fetchMock from 'jest-fetch-mock';
import Preferences from 'chaire-lib-common/lib/config/Preferences';
import fetch from 'node-fetch';

jest.mock('node-fetch', () => jest.fn());
const mockedFetch = fetch as jest.MockedFunction<typeof fetch>;

const jsonCapnpDefaultPrefs = Preferences.get('json2Capnp');

beforeEach(() => {
fetchMock.doMock();
fetchMock.mockClear();
jest.resetAllMocks();
});

describe('Valid calls and return values', () => {
test('Read value', async () => {

const jsonObject = {
field1: 3
};
fetchMock.mockOnce(JSON.stringify(jsonObject));

const jsonResponse = jest.fn() as jest.MockedFunction<Response['json']>;
jsonResponse.mockResolvedValue(jsonObject);
const response = Promise.resolve({
ok: true,
status: 200,
json: jsonResponse
});
mockedFetch.mockResolvedValue(response);

const result = await json2CapnpService.readCache('test', {});
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('http://localhost:2000/test?', expect.objectContaining({ method: 'GET' }));
expect(mockedFetch).toHaveBeenCalledTimes(1);
expect(mockedFetch).toHaveBeenCalledWith('http://localhost:2000/test?', expect.objectContaining({ method: 'GET' }));
expect(result).toEqual(jsonObject);
});

Expand All @@ -32,10 +44,21 @@ describe('Valid calls and return values', () => {
field: 3,
data: 'hello'
};
fetchMock.mockOnce('{ "status": "OK" }');
const jsonResponse = jest.fn() as jest.MockedFunction<Response['json']>;
jsonResponse.mockResolvedValue({
status: 'OK'
});
const response = Promise.resolve({
ok: true,
status: 200,
json: jsonResponse
});
mockedFetch.mockResolvedValue(response);

//fetchMock.mockOnce('{ "status": "OK" }');
const result = await json2CapnpService.writeCache('test', jsonObject);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith('http://localhost:2000/test', expect.objectContaining({ method: 'POST', headers: expect.anything(), body: JSON.stringify(jsonObject) }));
expect(mockedFetch).toHaveBeenCalledTimes(1);
expect(mockedFetch).toHaveBeenCalledWith('http://localhost:2000/test', expect.objectContaining({ method: 'POST', headers: expect.anything(), body: JSON.stringify(jsonObject) }));
});
});

Expand All @@ -54,10 +77,17 @@ describe('Valid calls, with default preferences changes', () => {
const jsonObject = {
field1: 3
};
fetchMock.mockOnce(JSON.stringify(jsonObject));
const jsonResponse = jest.fn() as jest.MockedFunction<Response['json']>;
jsonResponse.mockResolvedValue(jsonObject);
const response = Promise.resolve({
ok: true,
status: 200,
json: jsonResponse
});
mockedFetch.mockResolvedValue(response);
const result = await json2CapnpService.readCache('test', {});
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(`${host}:${port}/test?`, expect.objectContaining({ method: 'GET' }));
expect(mockedFetch).toHaveBeenCalledTimes(1);
expect(mockedFetch).toHaveBeenCalledWith(`${host}:${port}/test?`, expect.objectContaining({ method: 'GET' }));
expect(result).toEqual(jsonObject);
});

Expand All @@ -66,21 +96,30 @@ describe('Valid calls, with default preferences changes', () => {
field: 3,
data: 'hello'
};
fetchMock.mockOnce('{ "status": "OK" }');
const jsonResponse = jest.fn() as jest.MockedFunction<Response['json']>;
jsonResponse.mockResolvedValue({
status: 'OK'
});
const response = Promise.resolve({
ok: true,
status: 200,
json: jsonResponse
});
mockedFetch.mockResolvedValue(response);
const result = await json2CapnpService.writeCache('test', jsonObject);
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith(`${host}:${port}/test`, expect.objectContaining({ method: 'POST', headers: expect.anything(), body: JSON.stringify(jsonObject) }));
expect(mockedFetch).toHaveBeenCalledTimes(1);
expect(mockedFetch).toHaveBeenCalledWith(`${host}:${port}/test`, expect.objectContaining({ method: 'POST', headers: expect.anything(), body: JSON.stringify(jsonObject) }));
});
});

describe('Call errors', () => {
beforeEach(() => {
fetchMock.mockReject(new Error('wrong'));
mockedFetch.mockRejectedValue(new Error('wrong'));
});

afterEach(() => {
jest.setTimeout(5000);
fetchMock.mockClear();
mockedFetch.mockClear();
});

test('Read value', async () => {
Expand All @@ -89,9 +128,9 @@ describe('Call errors', () => {
jest.setTimeout(10000);
await expect(json2CapnpService.readCache('test', {}))
.rejects
.toThrowError();
expect(fetchMock).toHaveBeenCalledTimes(5);
expect(fetchMock).toHaveBeenCalledWith('http://localhost:2000/test?', expect.objectContaining({ method: 'GET' }));
.toThrow();
expect(mockedFetch).toHaveBeenCalledTimes(5);
expect(mockedFetch).toHaveBeenCalledWith('http://localhost:2000/test?', expect.objectContaining({ method: 'GET' }));
});

test('Read value', async () => {
Expand All @@ -104,8 +143,8 @@ describe('Call errors', () => {
};
await expect(json2CapnpService.writeCache('test', jsonObject))
.rejects
.toThrowError();
expect(fetchMock).toHaveBeenCalledTimes(5);
expect(fetchMock).toHaveBeenCalledWith('http://localhost:2000/test', expect.objectContaining({ method: 'POST', headers: expect.anything(), body: JSON.stringify(jsonObject) }));
.toThrow();
expect(mockedFetch).toHaveBeenCalledTimes(5);
expect(mockedFetch).toHaveBeenCalledWith('http://localhost:2000/test', expect.objectContaining({ method: 'POST', headers: expect.anything(), body: JSON.stringify(jsonObject) }));
});
});
});
Loading

0 comments on commit 64fb15c

Please sign in to comment.