Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for args.ts #578

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 82 additions & 13 deletions src/__tests__/unit/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
const processRunningPath = process.cwd();

jest.mock('ts-command-line-args', () => ({
__esModule: true,
parse: () => {
switch (process.env.result) {
case 'throw-error-object':
throw new Error(MANIFEST_IS_MISSING);
case 'error':
throw MANIFEST_IS_MISSING;
narekhovhannisyan marked this conversation as resolved.
Show resolved Hide resolved
case 'manifest-is-missing':
return {};
case 'manifest':
return {
manifest: 'manifest-mock.yml',
};
case 'absolute-path':
return {
manifest: path.normalize(`${processRunningPath}/manifest-mock.yml`),
};
case 'manifest-output':
return {
manifest: 'manifest-mock.yml',
output: 'output-mock.yml',
};
case 'override-params':
return {
manifest: 'manifest-mock.yml',
'override-params': 'override-params-mock.yml',
};
case 'help':
return {
manifest: path.normalize(`${processRunningPath}/manifest-mock.yml`),
help: true,
};
case 'not-yaml':
Expand All @@ -35,28 +51,47 @@ import path = require('path');
import {parseArgs} from '../../../util/args';
import {ERRORS} from '../../../util/errors';

import {STRINGS} from '../../../config';
import {STRINGS, CONFIG} from '../../../config';
const {impact} = CONFIG;
const {HELP} = impact;

const {CliInputError} = ERRORS;

const {MANIFEST_IS_MISSING, FILE_IS_NOT_YAML} = STRINGS;

const info = jest.spyOn(console, 'info').mockImplementation(() => {});

describe('util/args: ', () => {
const originalEnv = process.env;

describe('parseArgs(): ', () => {
it('throws error if there is no argument passed.', () => {
expect.assertions(2);
expect.assertions(5);

process.env.result = 'error'; // used for mocking

try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't try-catch blocks be in separate unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've combined all of them because the meaning is the same

parseArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error.message).toEqual(MANIFEST_IS_MISSING);
}
expect(error).toEqual(MANIFEST_IS_MISSING);
}

process.env.result = 'throw-error-object';

try {
parseArgs();
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toEqual(new CliInputError(MANIFEST_IS_MISSING));
}

process.env.result = 'manifest-is-missing';

try {
parseArgs();
} catch (error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error).toEqual(new CliInputError(MANIFEST_IS_MISSING));
}
});

Expand All @@ -66,8 +101,20 @@ describe('util/args: ', () => {
process.env.result = 'manifest';

const result = parseArgs();
const processRunningPath = process.cwd();
const manifestPath = 'manifest-mock.yml';
const expectedResult = {
inputPath: path.normalize(`${processRunningPath}/${manifestPath}`),
};

expect(result).toEqual(expectedResult);
});

it('returns manifest with absolute path.', () => {
expect.assertions(1);

process.env.result = 'absolute-path';

const result = parseArgs();
const manifestPath = 'manifest-mock.yml';
const expectedResult = {
inputPath: path.normalize(`${processRunningPath}/${manifestPath}`),
Expand All @@ -76,14 +123,38 @@ describe('util/args: ', () => {
expect(result).toEqual(expectedResult);
});

it('returns manifest with `paramPath`.', () => {
expect.assertions(1);

process.env.result = 'override-params';

const result = parseArgs();
const manifestPath = 'manifest-mock.yml';
const expectedResult = {
inputPath: path.normalize(`${processRunningPath}/${manifestPath}`),
paramPath: 'override-params-mock.yml',
};

expect(result).toEqual(expectedResult);
});

it('returns manifest with help.', () => {
expect.assertions(2);

process.env.result = 'help';

const result = parseArgs();

expect(info).toHaveBeenCalledWith(HELP);
expect(result).toEqual(undefined);
});

it('returns manifest and output path.', () => {
expect.assertions(1);

process.env.result = 'manifest-output';

const result = parseArgs();
const processRunningPath = process.cwd();

const manifestPath = 'manifest-mock.yml';
const outputPath = 'output-mock.yml';
const expectedResult = {
Expand All @@ -102,10 +173,8 @@ describe('util/args: ', () => {
try {
parseArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toBeInstanceOf(CliInputError);
expect(error.message).toEqual(FILE_IS_NOT_YAML);
}
expect(error).toBeInstanceOf(CliInputError);
expect(error).toEqual(new CliInputError(FILE_IS_NOT_YAML));
}
});
});
Expand Down
Loading