-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests and error handling for ExtraVarsTransformer
In this commit, tests have been added for the ExtraVarsTransformer function to ensure proper functioning and error handling. The function now includes a try-catch block to manage potential errors during the transformation of extraVars. In addition, import statements in the ExtraVars.test.ts file have been rearranged.
- Loading branch information
manu
committed
Jul 2, 2024
1 parent
e3db5cd
commit 32a88d9
Showing
3 changed files
with
73 additions
and
10 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
59 changes: 59 additions & 0 deletions
59
server/src/tests/integrations/ansible/ExtraVarsTransformer.test.ts
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,59 @@ | ||
import { describe, expect, test } from 'vitest'; | ||
import { API } from 'ssm-shared-lib'; | ||
import ExtraVarsTransformer from '../../../integrations/ansible/utils/ExtraVarsTransformer'; | ||
|
||
describe('transformExtraVars function', () => { | ||
test('should transform list of extraVars into object correctly', () => { | ||
const input: API.ExtraVar[] = [ | ||
{ extraVar: 'var1', value: 'value1' }, | ||
{ extraVar: 'var2', value: 'value2' }, | ||
{ extraVar: 'var3', value: 'value3' }, | ||
]; | ||
const result = ExtraVarsTransformer.transformExtraVars(input); | ||
expect(result).toEqual({ | ||
var1: 'value1', | ||
var2: 'value2', | ||
var3: 'value3', | ||
}); | ||
}); | ||
|
||
test('empty input', () => { | ||
const input: API.ExtraVar[] = []; | ||
expect(() => ExtraVarsTransformer.transformExtraVars(input)).toThrow( | ||
'Error during transformExtraVars', | ||
); | ||
}); | ||
|
||
test('should handle missing value field in extraVar', () => { | ||
const input: API.ExtraVar[] = [{ extraVar: 'var1', value: 'value1' }, { extraVar: 'var2' }]; | ||
const result = ExtraVarsTransformer.transformExtraVars(input); | ||
expect(result).toEqual({ | ||
var1: 'value1', | ||
var2: 'undefined', | ||
}); | ||
}); | ||
|
||
test('should handle missing extraVar field', () => { | ||
// @ts-expect-error incorrect typing test | ||
const input: API.ExtraVar[] = [{ value: 'value1' }, { extraVar: 'var2', value: 'value2' }]; | ||
const result = ExtraVarsTransformer.transformExtraVars(input); | ||
expect(result).toEqual({ | ||
undefined: 'value1', | ||
var2: 'value2', | ||
}); | ||
}); | ||
|
||
test('undefined input', () => { | ||
// @ts-expect-error incorrect typing test | ||
expect(() => ExtraVarsTransformer.transformExtraVars(undefined)).toThrow( | ||
'Error during transformExtraVars', | ||
); | ||
}); | ||
|
||
test('null input', () => { | ||
// @ts-expect-error incorrect typing test | ||
expect(() => ExtraVarsTransformer.transformExtraVars(null)).toThrow( | ||
'Error during transformExtraVars', | ||
); | ||
}); | ||
}); |