Refactor to enforce strictNullChecks
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.
This commit is contained in:
51
tests/shared/Assertions/ExpectDeepThrowsError.ts
Normal file
51
tests/shared/Assertions/ExpectDeepThrowsError.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
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 };
|
||||
}
|
||||
}, {});
|
||||
}
|
||||
14
tests/shared/Assertions/ExpectExists.ts
Normal file
14
tests/shared/Assertions/ExpectExists.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* Asserts that the provided value is neither null nor undefined.
|
||||
*
|
||||
* This function is a more explicit alternative to `expect(value).to.exist` in test cases.
|
||||
* It is particularly useful in situations where TypeScript's control flow analysis
|
||||
* does not recognize standard assertions, ensuring that `value` is treated as
|
||||
* non-nullable in the subsequent code. This can prevent potential type errors
|
||||
* and enhance code safety and clarity.
|
||||
*/
|
||||
export function expectExists<T>(value: T): asserts value is NonNullable<T> {
|
||||
if (value === null || value === undefined) {
|
||||
throw new Error('Expected value to exist');
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { expect } from 'vitest';
|
||||
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
|
||||
|
||||
export async function expectThrowsAsync(
|
||||
method: () => Promise<unknown>,
|
||||
@@ -10,11 +11,9 @@ export async function expectThrowsAsync(
|
||||
} catch (err) {
|
||||
error = err;
|
||||
}
|
||||
expect(error).toBeDefined();
|
||||
expectExists(error);
|
||||
expect(error).to.be.an(Error.name);
|
||||
if (errorMessage) {
|
||||
expect(error.message).to.equal(errorMessage);
|
||||
}
|
||||
expect(error.message).to.equal(errorMessage);
|
||||
}
|
||||
|
||||
export async function expectDoesNotThrowAsync(
|
||||
|
||||
Reference in New Issue
Block a user