Skip to content

Commit

Permalink
test(util): add tests for util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
manushak committed Dec 10, 2024
1 parent 9da3b73 commit d9974e0
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 43 deletions.
63 changes: 62 additions & 1 deletion src/__tests__/common/util/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import {execFileSync} from 'child_process';

jest.mock('node:readline/promises', () =>
require('../../../__mocks__/readline')
);

import {parseManifestFromStdin} from '../../../common/util/helpers';
jest.mock('child_process', () => {
const originalModule = jest.requireActual('child_process');
return {
...originalModule,
execFileSync: jest.fn(() => {
return 'Command executed successfully';
}),
};
});

import {
parseManifestFromStdin,
runHelpCommand,
} from '../../../common/util/helpers';

describe('common/util/helpers: ', () => {
describe('parseManifestFromStdin(): ', () => {
Expand Down Expand Up @@ -41,4 +56,50 @@ describe('common/util/helpers: ', () => {
expect(response).toEqual(expectedMessage);
});
});

describe('runHelpCommand(): ', () => {
const originalEnv = process.env;
it('calls process.exit with code 1 on error.', () => {
expect.assertions(3);

jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
expect(code).toEqual(1);
throw new Error(`process.exit(${code}) called`);
});

expect(() => runHelpCommand('if-run')).toThrow('process.exit(1) called');
expect(execFileSync).toHaveBeenCalledWith(
'npm',
['run', 'if-run', '--silent', '--', '-h'],
{
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: false,
}
);
});

it('executes when the script runs from the global.', () => {
expect.assertions(3);
process.env.npm_config_global = 'true';

jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
expect(code).toEqual(1);
throw new Error(`process.exit(${code}) called`);
});

expect(() => runHelpCommand('if-run')).toThrow('process.exit(1) called');
expect(execFileSync).toHaveBeenCalledWith(
'if-run',
['--silent', '--', '-h'],
{
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: false,
}
);
});

process.env = originalEnv;
});
});
39 changes: 31 additions & 8 deletions src/__tests__/if-check/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import {execFileSync} from 'child_process';

jest.mock('child_process', () => {
const originalModule = jest.requireActual('child_process');
return {
...originalModule,
execFileSync: jest.fn(() => {
return 'Command executed successfully';
}),
};
});

jest.mock('../../../common/util/fs', () => ({
isFileExists: () => {
if (process.env.fileExists === 'true') {
Expand Down Expand Up @@ -151,17 +163,28 @@ describe('if-check/util: ', () => {
}
});

it('throws an error if parsing failed.', async () => {
it('runs help command if the passed argument is incorrect.', async () => {
expect.assertions(3);
jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
expect(code).toEqual(1);
throw new Error(`process.exit(${code}) called`);
});

process.env.result = 'env-throw-error';
expect.assertions(1);

try {
await parseIfCheckArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new ParseCliParamsError('mock-error'));
await expect(parseIfCheckArgs()).rejects.toThrow(
'process.exit(1) called'
);

expect(execFileSync).toHaveBeenCalledWith(
'npm',
['run', 'if-check', '--silent', '--', '-h'],
{
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: false,
}
}
);
});

it('throws error if parsing failed (not instance of error).', async () => {
Expand Down
147 changes: 147 additions & 0 deletions src/__tests__/if-csv/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import {execFileSync} from 'child_process';

jest.mock('child_process', () => {
const originalModule = jest.requireActual('child_process');
return {
...originalModule,
execFileSync: jest.fn(() => {
return 'Command executed successfully';
}),
};
});

jest.mock('../../../common/util/fs', () => ({
isFileExists: () => {
if (process.env.fileExists === 'true') {
return true;
}
return false;
},
}));

jest.mock('ts-command-line-args', () => ({
__esModule: true,
parse: () => {
switch (process.env.result) {
case 'manifest':
return {
manifest: 'manifest-mock.yml',
};
case 'manifest-is-missing':
return {};
case 'manifest-is-not-yaml':
return {manifest: 'manifest'};
case 'env-throw-error':
throw new Error('mock-error');
case 'env-throw':
throw 'mock-error';
default:
return {
manifest: 'mock-manifest.yaml',
output: 'mock-output',
};
}
},
}));

import {ERRORS} from '@grnsft/if-core/utils';

import {parseIfCsvArgs} from '../../../if-csv/util/args';

import {STRINGS} from '../../../common/config';

const {CliSourceFileError, ParseCliParamsError} = ERRORS;

const {SOURCE_IS_NOT_YAML, MANIFEST_NOT_FOUND} = STRINGS;

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

describe('parseIfCsvArgs(): ', () => {
it('successfully executes when `manifest` is specified.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifest';
const response = await parseIfCsvArgs();

expect.assertions(1);

expect(response).toEqual({
manifest: 'manifest-mock.yml',
output: undefined,
params: undefined,
});
});

it('executes if `manifest` is missing.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifest-is-missing';
const response = await parseIfCsvArgs();

expect.assertions(1);

expect(response).toEqual({});
});

it('throws an error if `manifest` is not a yaml.', async () => {
process.env.fileExists = 'true';
process.env.result = 'manifest-is-not-yaml';
expect.assertions(1);

try {
await parseIfCsvArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new CliSourceFileError(SOURCE_IS_NOT_YAML));
}
}
});

it('throws an error if `manifest` path is invalid.', async () => {
process.env.fileExists = 'false';
expect.assertions(1);

try {
await parseIfCsvArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new ParseCliParamsError(MANIFEST_NOT_FOUND));
}
}
});

it('runs help command if the passed argument is incorrect.', async () => {
expect.assertions(3);
jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
expect(code).toEqual(1);
throw new Error(`process.exit(${code}) called`);
});

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

await expect(parseIfCsvArgs()).rejects.toThrow('process.exit(1) called');

expect(execFileSync).toHaveBeenCalledWith(
'npm',
['run', 'if-csv', '--silent', '--', '-h'],
{
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: false,
}
);
});

it('throws error if parsing failed (not instance of error).', async () => {
process.env.result = 'env-throw';
expect.assertions(1);

try {
await parseIfCsvArgs();
} catch (error) {
expect(error).toEqual('mock-error');
}
});
});

process.env = originalEnv;
});
37 changes: 29 additions & 8 deletions src/__tests__/if-diff/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {execFileSync} from 'child_process';

jest.mock('ts-command-line-args', () => ({
__esModule: true,
parse: () => {
Expand Down Expand Up @@ -33,6 +35,16 @@ jest.mock('ts-command-line-args', () => ({
},
}));

jest.mock('child_process', () => {
const originalModule = jest.requireActual('child_process');
return {
...originalModule,
execFileSync: jest.fn(() => {
return 'Command executed successfully';
}),
};
});

import {ERRORS} from '@grnsft/if-core/utils';

import {parseIfDiffArgs} from '../../../if-diff/util/args';
Expand Down Expand Up @@ -104,17 +116,26 @@ describe('util/args: ', () => {
expect(response).toHaveProperty('sourcePath');
});

it('throws error if parsing failed.', () => {
it('runs help command if the passed argument is incorrect.', () => {
expect.assertions(3);
jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
expect(code).toEqual(1);
throw new Error(`process.exit(${code}) called`);
});

process.env.result = 'diff-throw-error';
expect.assertions(1);

try {
parseIfDiffArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new ParseCliParamsError('mock-error'));
expect(() => parseIfDiffArgs()).toThrow('process.exit(1) called');

expect(execFileSync).toHaveBeenCalledWith(
'npm',
['run', 'if-diff', '--silent', '--', '-h'],
{
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: false,
}
}
);
});

it('throws error if parsing failed (not instance of error).', () => {
Expand Down
37 changes: 29 additions & 8 deletions src/__tests__/if-env/util/args.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
import {execFileSync} from 'child_process';

jest.mock('child_process', () => {
const originalModule = jest.requireActual('child_process');
return {
...originalModule,
execFileSync: jest.fn(() => {
return 'Command executed successfully';
}),
};
});

jest.mock('../../../common/util/fs', () => ({
isFileExists: () => {
if (process.env.fileExists === 'true') {
Expand Down Expand Up @@ -95,17 +107,26 @@ describe('util/args: ', () => {
}
});

it('throws an error if parsing failed.', async () => {
it('runs help command if the passed argument is incorrect.', async () => {
expect.assertions(3);
jest.spyOn(process, 'exit').mockImplementation((code?: number) => {
expect(code).toEqual(1);
throw new Error(`process.exit(${code}) called`);
});

process.env.result = 'env-throw-error';
expect.assertions(1);

try {
await parseIfEnvArgs();
} catch (error) {
if (error instanceof Error) {
expect(error).toEqual(new ParseCliParamsError('mock-error'));
await expect(parseIfEnvArgs()).rejects.toThrow('process.exit(1) called');

expect(execFileSync).toHaveBeenCalledWith(
'npm',
['run', 'if-env', '--silent', '--', '-h'],
{
cwd: process.env.CURRENT_DIR || process.cwd(),
stdio: 'inherit',
shell: false,
}
}
);
});

it('throws error if parsing failed (not instance of error).', async () => {
Expand Down
Loading

0 comments on commit d9974e0

Please sign in to comment.