This commit fixes compiler bug where it fails when optional values are compiled into absent values in nested calls. - Throw exception with more context for easier future debugging. - Add better validation of argument values for nested calls. - Refactor `FunctionCallCompiler` for better clarity and modularize it to make it more maintainable and testable. - Refactor related interface to not have `I` prefix, and function/variable names for better clarity. Context: Discovered this issue while attempting to call `RunInlineCodeAsTrustedInstaller` which in turn invokes `RunPowerShell` for issue #246. This led to the realization that despite parameters flagged as optional, the nested argument compilation didn't support them.
49 lines
1.6 KiB
TypeScript
49 lines
1.6 KiB
TypeScript
import { expect } from 'vitest';
|
|
|
|
// `toThrowError` does not assert the error type (https://github.com/vitest-dev/vitest/blob/v0.34.2/docs/api/expect.md#tothrowerror)
|
|
export function expectDeepThrowsError<T extends Error>(delegate: () => void, expected: T) {
|
|
// arrange
|
|
if (!expected) {
|
|
throw new Error('missing expected');
|
|
}
|
|
let actual: T | undefined;
|
|
// act
|
|
try {
|
|
delegate();
|
|
} catch (error) {
|
|
actual = error;
|
|
}
|
|
// assert
|
|
expect(Boolean(actual)).to.equal(true, `Expected to throw "${expected.name}" but delegate did not throw at all.`);
|
|
expect(Boolean(actual?.stack)).to.equal(true, 'Empty stack trace.');
|
|
expect(expected.message).to.equal(actual.message);
|
|
expect(expected.name).to.equal(actual.name);
|
|
expectDeepEqualsIgnoringUndefined(expected, actual);
|
|
}
|
|
|
|
function expectDeepEqualsIgnoringUndefined(expected: unknown, actual: unknown) {
|
|
const actualClean = removeUndefinedProperties(actual);
|
|
const expectedClean = removeUndefinedProperties(expected);
|
|
expect(expectedClean).to.deep.equal(actualClean);
|
|
}
|
|
|
|
function removeUndefinedProperties(obj: unknown): unknown {
|
|
return Object.keys(obj ?? {})
|
|
.reduce((acc, key) => {
|
|
const value = obj[key];
|
|
switch (typeof value) {
|
|
case 'object': {
|
|
const cleanValue = removeUndefinedProperties(value); // recurse
|
|
if (!Object.keys(cleanValue).length) {
|
|
return { ...acc };
|
|
}
|
|
return { ...acc, [key]: cleanValue };
|
|
}
|
|
case 'undefined':
|
|
return { ...acc };
|
|
default:
|
|
return { ...acc, [key]: value };
|
|
}
|
|
}, {});
|
|
}
|