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:
@@ -9,7 +9,7 @@ describe('ConsoleLogger', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing console';
|
||||
const console = absentValue;
|
||||
const console = absentValue as never;
|
||||
// act
|
||||
const act = () => new ConsoleLogger(console);
|
||||
// assert
|
||||
@@ -32,10 +32,25 @@ describe('ConsoleLogger', () => {
|
||||
expect(consoleMock.callHistory[0].args).to.deep.equal(expectedParams);
|
||||
});
|
||||
});
|
||||
describe('throws if log function is missing', () => {
|
||||
itEachLoggingMethod((functionName, testParameters) => {
|
||||
// arrange
|
||||
const expectedError = `missing "${functionName}" function`;
|
||||
const consoleMock = {} as Partial<Console>;
|
||||
consoleMock[functionName] = undefined;
|
||||
const logger = new ConsoleLogger(consoleMock);
|
||||
|
||||
// act
|
||||
const act = () => logger[functionName](...testParameters);
|
||||
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class MockConsole
|
||||
extends StubWithObservableMethodCalls<Partial<Console>>
|
||||
extends StubWithObservableMethodCalls<Console>
|
||||
implements Partial<Console> {
|
||||
public info(...args: unknown[]) {
|
||||
this.registerMethodCall({
|
||||
|
||||
@@ -10,31 +10,48 @@ describe('ElectronLogger', () => {
|
||||
itEachAbsentObjectValue((absentValue) => {
|
||||
// arrange
|
||||
const expectedError = 'missing logger';
|
||||
const electronLog = absentValue;
|
||||
const electronLog = absentValue as never;
|
||||
// act
|
||||
const act = () => createElectronLogger(electronLog);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
}, { excludeUndefined: true });
|
||||
});
|
||||
itEachLoggingMethod((functionName, testParameters) => {
|
||||
// arrange
|
||||
const expectedParams = testParameters;
|
||||
const electronLogMock = new MockElectronLog();
|
||||
const logger = createElectronLogger(electronLogMock);
|
||||
describe('throws if log function is missing', () => {
|
||||
itEachLoggingMethod((functionName, testParameters) => {
|
||||
// arrange
|
||||
const expectedError = `missing "${functionName}" function`;
|
||||
const electronLogMock = {} as Partial<ElectronLog>;
|
||||
electronLogMock[functionName] = undefined;
|
||||
const logger = createElectronLogger(electronLogMock);
|
||||
|
||||
// act
|
||||
logger[functionName](...expectedParams);
|
||||
// act
|
||||
const act = () => logger[functionName](...testParameters);
|
||||
|
||||
// assert
|
||||
expect(electronLogMock.callHistory).to.have.lengthOf(1);
|
||||
expect(electronLogMock.callHistory[0].methodName).to.equal(functionName);
|
||||
expect(electronLogMock.callHistory[0].args).to.deep.equal(expectedParams);
|
||||
// assert
|
||||
expect(act).to.throw(expectedError);
|
||||
});
|
||||
});
|
||||
describe('methods log the provided params', () => {
|
||||
itEachLoggingMethod((functionName, testParameters) => {
|
||||
// arrange
|
||||
const expectedParams = testParameters;
|
||||
const electronLogMock = new MockElectronLog();
|
||||
const logger = createElectronLogger(electronLogMock);
|
||||
|
||||
// act
|
||||
logger[functionName](...expectedParams);
|
||||
|
||||
// assert
|
||||
expect(electronLogMock.callHistory).to.have.lengthOf(1);
|
||||
expect(electronLogMock.callHistory[0].methodName).to.equal(functionName);
|
||||
expect(electronLogMock.callHistory[0].args).to.deep.equal(expectedParams);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
class MockElectronLog
|
||||
extends StubWithObservableMethodCalls<Partial<ElectronLog>>
|
||||
extends StubWithObservableMethodCalls<ElectronLog>
|
||||
implements Partial<ElectronLog> {
|
||||
public info(...args: unknown[]) {
|
||||
this.registerMethodCall({
|
||||
|
||||
@@ -2,13 +2,15 @@ import { it } from 'vitest';
|
||||
import { FunctionKeys } from '@/TypeHelpers';
|
||||
import { ILogger } from '@/infrastructure/Log/ILogger';
|
||||
|
||||
type TestParameters = [string, number, { some: string }];
|
||||
|
||||
export function itEachLoggingMethod(
|
||||
handler: (
|
||||
functionName: keyof ILogger,
|
||||
testParameters?: unknown[]
|
||||
testParameters: TestParameters,
|
||||
) => void,
|
||||
) {
|
||||
const testParameters = ['test', 123, { some: 'object' }];
|
||||
const testParameters: TestParameters = ['test', 123, { some: 'object' }];
|
||||
const loggerMethods: Array<FunctionKeys<ILogger>> = [
|
||||
'info',
|
||||
];
|
||||
|
||||
@@ -11,7 +11,7 @@ describe('WindowInjectedLogger', () => {
|
||||
// arrange
|
||||
const expectedError = 'missing log';
|
||||
const windowVariables = new WindowVariablesStub()
|
||||
.withLog(absentValue);
|
||||
.withLog(absentValue as never);
|
||||
// act
|
||||
const act = () => new WindowInjectedLogger(windowVariables);
|
||||
// assert
|
||||
|
||||
Reference in New Issue
Block a user