Skip to content

Commit

Permalink
chore: Remove manual mocks (#4465)
Browse files Browse the repository at this point in the history
  • Loading branch information
marikaner authored Feb 5, 2024
1 parent ff3f4d8 commit a9014f2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 85 deletions.
57 changes: 0 additions & 57 deletions packages/test-util/src/__mocks__/fs.js

This file was deleted.

40 changes: 14 additions & 26 deletions packages/test-util/src/test-destination-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { fail } from 'assert';
import mock from 'mock-fs';
import { credentials, systems } from '../test/test-util/test-destinations';
import {
getTestDestinationByAlias,
getTestDestinations
} from './test-destination-provider';
// This replaces the fs module with the mocked one defined in __mock__/fs.js
jest.mock('fs');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = require('fs');

describe('test-destination-provider', () => {
describe('getDestinations', () => {
afterEach(() => {
mock.restore();
});
afterEach(() => {
mock.restore();
});

describe('getDestinations', () => {
it('returns a list of destinations taken from the first matching file(s) found by recursively traversing the file hierarchy upwards starting at "./"', () => {
mock({
'systems.json': JSON.stringify(systems),
Expand Down Expand Up @@ -183,22 +178,20 @@ describe('test-destination-provider', () => {
});

it('throws a reasonable error when the JSON file cannot be found', () => {
fs.switchMockOn();
expect(() => getTestDestinations()).toThrowError(
mock();
expect(() => getTestDestinations()).toThrow(
/^No systems.json could be found when searching in directory.*/
);
fs.switchMockOff();
});

it('throws a reasonable error when the file does not contain proper JSON', () => {
fs.switchMockOn();
fs.setReadDirSync(['systems.json']);
fs.setReadFileSync('not proper JSON');
expect(() => getTestDestinations()).toThrowError(
mock({
'systems.json': 'not proper JSON'
});

expect(() => getTestDestinations()).toThrow(
/^File read from path.*is not valid JSON./
);

fs.switchMockOff();
});
});

Expand All @@ -207,14 +200,9 @@ describe('test-destination-provider', () => {
'systems.json': '{"systems":[{"alias":"Foo"}]}'
});

try {
getTestDestinations();
fail('Expected an error to be thrown, but none has been.');
} catch (error) {
expect(error.message).toMatch(
/A system in .* is not valid - Mandatory alias or url missing./
);
}
expect(() => getTestDestinations()).toThrow(
/A system in .* is not valid - Mandatory alias or url missing./
);
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/test-util/src/test-destination-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function readCredentialsOrFail(
function mergeSystemsAndCredentials(systems, credentials) {
return systems.map(system => ({
...system,
...credentials.find(cred => cred.alias === system.alias)
...credentials.find(({ alias }) => alias === system.alias)
}));
}

Expand Down Expand Up @@ -244,7 +244,7 @@ function readCredentials(filePath: string): CredentialsFile {
function readJson(filePath: string): { [key: string]: any } {
let content;
try {
content = readFileSync(filePath, 'utf8');
content = readFileSync(filePath, 'utf-8');
} catch (error) {
throw new Error(
`Failed to read file at path: ${filePath}.
Expand Down

0 comments on commit a9014f2

Please sign in to comment.