Skip to content

Commit

Permalink
Merge pull request #578 from Green-Software-Foundation/args-unit-test
Browse files Browse the repository at this point in the history
Add unit test for args.ts
  • Loading branch information
MariamKhalatova authored Apr 9, 2024
2 parents 1d6509f + df7fece commit 5bd07f8
Showing 1 changed file with 82 additions and 13 deletions.
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;
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 {
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

0 comments on commit 5bd07f8

Please sign in to comment.