This commit applies `strictNullChecks` to the entire codebase to improve maintainability and type safety. Key changes include: - Remove some explicit null-checks where unnecessary. - Add necessary null-checks. - Refactor static factory functions for a more functional approach. - Improve some test names and contexts for better debugging. - Add unit tests for any additional logic introduced. - Refactor `createPositionFromRegexFullMatch` to its own function as the logic is reused. - Prefer `find` prefix on functions that may return `undefined` and `get` prefix for those that always return a value.
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { expect } from 'vitest';
|
|
import { expectExists } from './ExpectExists';
|
|
|
|
// `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
|
|
let actual: T | undefined;
|
|
// act
|
|
try {
|
|
delegate();
|
|
} catch (error) {
|
|
actual = error;
|
|
}
|
|
// assert
|
|
expectExists(actual);
|
|
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: object | undefined,
|
|
actual: object | undefined,
|
|
) {
|
|
const actualClean = removeUndefinedProperties(actual);
|
|
const expectedClean = removeUndefinedProperties(expected);
|
|
expect(expectedClean).to.deep.equal(actualClean);
|
|
}
|
|
|
|
function removeUndefinedProperties(obj: object | undefined): object | undefined {
|
|
if (!obj) {
|
|
return obj;
|
|
}
|
|
return Object.keys(obj).reduce((acc, key) => {
|
|
const value = obj[key];
|
|
switch (typeof value) {
|
|
case 'object': {
|
|
const cleanValue = removeUndefinedProperties(value); // recurse
|
|
if (!cleanValue || !Object.keys(cleanValue).length) {
|
|
return { ...acc };
|
|
}
|
|
return { ...acc, [key]: cleanValue };
|
|
}
|
|
case 'undefined':
|
|
return { ...acc };
|
|
default:
|
|
return { ...acc, [key]: value };
|
|
}
|
|
}, {});
|
|
}
|