Files
privacy.sexy/tests/shared/Assertions/ExpectDeepThrowsError.ts
undergroundwires e17744faf0 Bump to TypeScript 5.5 and enable noImplicitAny
This commit upgrades TypeScript from 5.4 to 5.5 and enables the
`noImplicitAny` option for stricter type checking. It refactors code to
comply with `noImplicitAny` and adapts to new TypeScript features and
limitations.

Key changes:

- Migrate from TypeScript 5.4 to 5.5
- Enable `noImplicitAny` for stricter type checking
- Refactor code to comply with new TypeScript features and limitations

Other supporting changes:

- Refactor progress bar handling for type safety
- Drop 'I' prefix from interfaces to align with new code convention
- Update TypeScript target from `ES2017` and `ES2018`.
  This allows named capturing groups. Otherwise, new TypeScript compiler
  does not compile the project and shows the following error:
  ```
  ...
  TimestampedFilenameGenerator.spec.ts:105:23 - error TS1503: Named capturing groups are only available when targeting 'ES2018' or later
  const pattern = /^(?<timestamp>\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-(?<scriptName>[^.]+?)(?:\.(?<extension>[^.]+))?$/;// timestamp-scriptName.extension
  ...
  ```
- Refactor usage of `electron-progressbar` for type safety and
  less complexity.
2024-09-26 16:07:37 +02:00

52 lines
1.6 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 as keyof (typeof obj)];
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 };
}
}, {});
}