Skip to content

Commit 661c289

Browse files
committed
Minor refactoring Testing util.
Test expected output even in failure case. Make 'unchanged' asseriton explicit.
1 parent 038a180 commit 661c289

File tree

1 file changed

+44
-34
lines changed

1 file changed

+44
-34
lines changed

ts/kpt-functions/src/testing.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class TestRunner {
2323
constructor(private readonly fn: KptFunc) {}
2424

2525
/**
26-
* Runs the KptFunc and asserts the expected output or exception.
26+
* Runs the KptFunc and asserts the expected output or error.
2727
*
2828
* Example usage:
2929
*
@@ -37,23 +37,23 @@ export class TestRunner {
3737
*
3838
* @param input input Configs passed to the function. It is deep-copied before running the function.
3939
* If undefined, assumes an empty Configs.
40-
* @param expectedOutput expected resultant Configs after KptFunc has successfully completed.
41-
* If undefined, assumes the output should remain unchanged (NO OP).
42-
* @param expectedException expected exception to be thrown. If given, expectedOutput is ignored.
43-
* @param expectedExceptionMessage expected message of expection to be thrown. If given, expectedOutput is ignored.
40+
* @param expectedOutput expected resultant Configs after running the function regardless of success or failure.
41+
* Use 'unchanged' if the function is not expected to change input Configs.
42+
* @param expectedErrorType expected error type to be thrown.
43+
* @param expectedErrorMessage expected message of expection to be thrown.
4444
*/
4545
async assert(
4646
input: Configs = new Configs(),
47-
expectedOutput?: Configs,
48-
expectedException?: new (...args: any[]) => Error,
49-
expectedExceptionMessage?: string | RegExp
47+
expectedOutput?: Configs | 'unchanged',
48+
expectedErrorType?: new (...args: any[]) => Error,
49+
expectedErrorMessage?: string | RegExp,
5050
) {
5151
await testFn(
5252
this.fn,
5353
input,
5454
expectedOutput,
55-
expectedException,
56-
expectedExceptionMessage
55+
expectedErrorType,
56+
expectedErrorMessage,
5757
);
5858
}
5959

@@ -70,53 +70,63 @@ export class TestRunner {
7070
*
7171
* @param input input Configs passed to the function. It is deep-copied before running the function.
7272
* If undefined, assumes an empty Configs.
73-
* @param expectedOutput expected resultant Configs after KptFunc has successfully completed.
74-
* If undefined, assumes the output should remain unchanged (NO OP).
75-
* @param expectedException expected exception to be thrown. If given, expectedOutput is ignored.
76-
* @param expectedExceptionMessage expected message of expection to be thrown. If given, expectedOutput is ignored.
73+
* @param expectedOutput expected resultant Configs after running the function regardless of success or failure.
74+
* Use 'unchanged' if the function is not expected to change input Configs.
75+
* @param expectedErrorType expected error type to be thrown.
76+
* @param expectedErrorMessage expected message of expection to be thrown.
7777
*/
7878
assertCallback(
7979
input: Configs = new Configs(),
80-
expectedOutput?: Configs,
81-
expectedException?: new (...args: any[]) => Error,
82-
expectedExceptionMessage?: string | RegExp
80+
expectedOutput?: Configs | 'unchanged',
81+
expectedErrorType?: new (...args: any[]) => Error,
82+
expectedErrorMessage?: string | RegExp,
8383
): () => Promise<void> {
8484
return async () =>
8585
await this.assert(
8686
input,
8787
expectedOutput,
88-
expectedException,
89-
expectedExceptionMessage
88+
expectedErrorType,
89+
expectedErrorMessage,
9090
);
9191
}
9292
}
9393

9494
async function testFn(
9595
fn: KptFunc,
9696
input: Configs = new Configs(),
97-
expectedOutput?: Configs,
98-
expectedException?: new (...args: any[]) => Error,
99-
expectedExceptionMessage?: string | RegExp
97+
expectedOutput?: Configs | 'unchanged',
98+
expectedErrorType?: new (...args: any[]) => Error,
99+
expectedErrorMessage?: string | RegExp,
100100
) {
101101
// We must clone the input as the function may mutate its input Configs.
102102
const configs = deepClone(input);
103103

104-
if (expectedException) {
105-
await expectAsync(fn(configs)).toBeRejectedWithError(
106-
expectedException,
107-
expectedExceptionMessage
104+
const matcher = expectAsync(fn(configs));
105+
106+
if (expectedErrorType) {
107+
await matcher.toBeRejectedWithError(
108+
expectedErrorType,
109+
expectedErrorMessage
108110
);
109-
return;
110-
} else if (expectedExceptionMessage) {
111-
await expectAsync(fn(configs)).toBeRejectedWithError(
112-
expectedExceptionMessage
111+
} else if (expectedErrorMessage) {
112+
await matcher.toBeRejectedWithError(expectedErrorMessage);
113+
} else if (expectedOutput) {
114+
await matcher.toBeResolved();
115+
} else {
116+
throw new Error(
117+
'Either specify expectedOutput or one of expectedError* parameters'
113118
);
114-
return;
115119
}
116120

117-
await fn(configs);
118-
119-
expect(valueOf(configs)).toEqual(valueOf(expectedOutput) || valueOf(input));
121+
if (expectedOutput) {
122+
let o: Configs;
123+
if (expectedOutput === 'unchanged') {
124+
o = input;
125+
} else {
126+
o = expectedOutput;
127+
}
128+
expect(valueOf(configs)).toEqual(valueOf(o));
129+
}
120130
}
121131

122132
function deepClone(configs: Configs): Configs {

0 commit comments

Comments
 (0)