-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(util): add tests for util functions
- Loading branch information
Showing
7 changed files
with
364 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.