Skip to content

Commit

Permalink
fix: Implemented error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pawfa committed Mar 19, 2021
1 parent f02264c commit 643d24f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export async function run(process: NodeJS.Process) {
if (argv._.includes('publish')) {
try {
await publish(argv);
} catch (e) {
} catch (error) {
if (error.isAxiosError) {
console.error('Error', error.toJSON());
} else {
console.error(error);
}
process.exit(1);
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/utils/read-pacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import * as fs from 'fs';
import * as path from 'path';

export function readPacts(dir: string): Pact[] {
return fs.readdirSync(dir).map((fileName) => {
const fileNames = fs.readdirSync(dir);

if (fileNames.length === 0) {
throw new Error(`Pact directory ${dir} is empty.`);
}

return fileNames.map((fileName) => {
const filePath = path.join(dir, fileName);
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
});
Expand Down
18 changes: 18 additions & 0 deletions test/mocks/process.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { makeFactory } from 'factory.ts';

export const processMockFactory = makeFactory(({
argv: [
'node-param',
'node-param',
'publish',
'--url',
'judge-d.instance.com',
'--pactsDir',
'/pacts',
'--serviceName',
'example-service',
'--serviceVersion',
'1.0.0',
],
exit: jest.fn(),
} as unknown) as NodeJS.Process);
8 changes: 8 additions & 0 deletions test/read-pacts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ describe('readPacts', () => {

expect(pacts).toEqual([service1PactMock, service2PactMock]);
});

test('throws error when pact directory is empty', () => {
mockFs({ './pacts': {} });

expect(() => readPacts('./pacts')).toThrow(
'Pact directory ./pacts is empty.'
);
});
});
50 changes: 35 additions & 15 deletions test/run.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { run } from '../src';
import { publish } from '../src/commands/publish';
import { mocked } from 'ts-jest/utils';
import { processMockFactory } from './mocks/process.mock';

jest.mock('../src/commands/publish');

describe('run', () => {
beforeEach(() =>
jest.spyOn(console, 'error').mockImplementationOnce(jest.fn())
);
test('it invokes publish function with correct arguments', () => {
const processMock = {
argv: [
'node-param',
'node-param',
'publish',
'--url',
'judge-d.instance.com',
'--pactsDir',
'/pacts',
'--serviceName',
'example-service',
'--serviceVersion',
'1.0.0',
],
} as NodeJS.Process;
const processMock = processMockFactory.build();

run(processMock);

Expand All @@ -33,4 +24,33 @@ describe('run', () => {
})
);
});

test('catches axios post error, forwards message to console.error and call process exit', async () => {
jest.spyOn(console, 'error').mockImplementationOnce(() => {});
mocked(publish).mockImplementation(() => {
throw {
isAxiosError: true,
message: 'error message',
toJSON: jest.fn(),
};
});
const processMock = processMockFactory.build();

await run(processMock);

expect(console.error).toHaveBeenCalled();
expect(processMock.exit).toHaveBeenCalledWith(1);
});

test('catches error other than axios, forwards message to console.error and call process exit', async () => {
mocked(publish).mockImplementation(() => {
throw { message: 'error message' };
});
const processMock = processMockFactory.build();

await run(processMock);

expect(console.error).toHaveBeenCalled();
expect(processMock.exit).toHaveBeenCalledWith(1);
});
});

0 comments on commit 643d24f

Please sign in to comment.