-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added assertDate util and unit tests
- Loading branch information
1 parent
970a0ac
commit ab89c9e
Showing
13 changed files
with
222 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: 'ts-jest/presets/default-esm', | ||
extensionsToTreatAsEsm: ['.ts'], | ||
transform: { | ||
'^.+\\.ts$': [ | ||
'ts-jest', | ||
{ | ||
useESM: true, | ||
tsconfig: 'tsconfig.jest.json', | ||
}, | ||
], | ||
}, | ||
testMatch: ['**/*/*.test.ts'], | ||
testEnvironment: 'node', | ||
testTimeout: 20000, | ||
collectCoverage: true, | ||
coverageDirectory: 'coverage', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { expect } from '@jest/globals' | ||
import { CancellationPromise } from './CancellablePromise' | ||
|
||
test(`CancellationPromise can be resolved`, async () => { | ||
const promise = new CancellationPromise() | ||
|
||
expect(promise).toBeInstanceOf(Promise) | ||
expect(typeof promise.resolve).toEqual('function') | ||
|
||
promise.resolve('test') | ||
|
||
await expect(promise).resolves.toBe('test') | ||
}) | ||
|
||
test(`CancellationPromise can be rejected`, async () => { | ||
const promise = new CancellationPromise() | ||
|
||
expect(promise).toBeInstanceOf(Promise) | ||
expect(typeof promise.reject).toEqual('function') | ||
|
||
promise.reject('error') | ||
|
||
await expect(promise).rejects.toBe('error') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
|
||
import { afterAll, beforeAll, describe, expect, jest } from '@jest/globals' | ||
import { setTimeout } from 'node:timers/promises' | ||
import { addDisposeOnSigterm } from './addDisposeOnSigterm' | ||
|
||
describe('addDisposeOnSigterm', () => { | ||
const _processOn = process.on | ||
|
||
beforeAll(() => { | ||
process.on = jest.fn<(event: NodeJS.Signals, listener: (signal: NodeJS.Signals) => void) => NodeJS.Process>() as any | ||
}) | ||
afterAll(() => { | ||
process.on = _processOn | ||
}) | ||
|
||
test(`it registers a callback for SIGTERM and SIGINT`, async () => { | ||
const cleanMock = jest.fn<() => Promise<void>>().mockResolvedValue(undefined) | ||
|
||
addDisposeOnSigterm(cleanMock) | ||
|
||
expect(cleanMock).not.toHaveBeenCalled() | ||
expect(process.on).toHaveBeenCalledTimes(2) | ||
expect(process.on).toHaveBeenCalledWith('SIGTERM', expect.any(Function)) | ||
|
||
const callback1 = jest.mocked(process.on).mock.calls[0][1] | ||
const callback2 = jest.mocked(process.on).mock.calls[1][1] | ||
|
||
callback1() | ||
callback2() | ||
|
||
await setTimeout(200) | ||
|
||
expect(cleanMock).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect } from '@jest/globals' | ||
import { assertDate } from './assert' | ||
import { AssertionError } from './errors' | ||
|
||
test(`assertDate throws an AssertionError is the value is not a Date`, () => { | ||
expect(() => assertDate(1)).toThrowError(AssertionError) | ||
expect(() => assertDate('1')).toThrowError(AssertionError) | ||
expect(() => assertDate(new Date('no date'))).toThrowError(AssertionError) | ||
expect(() => assertDate(true)).toThrowError(AssertionError) | ||
expect(() => assertDate({})).toThrowError(AssertionError) | ||
expect(() => assertDate(undefined)).toThrowError(AssertionError) | ||
expect(() => assertDate(null)).toThrowError(AssertionError) | ||
expect(() => assertDate(new Date().getTime())).toThrowError(AssertionError) | ||
|
||
try { | ||
assertDate('test') | ||
} catch (error) { | ||
expect(error).toEqual(new AssertionError({ forValue: 'test', forKey: 'date' }, `Value is not a date`)) | ||
} | ||
|
||
assertDate(new Date()) // no exception | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { AssertionError } from './errors' | ||
|
||
export function assertDate(value: unknown): asserts value is Date { | ||
if (!(value instanceof Date) || isNaN(new Date(value).getTime())) { | ||
throw new AssertionError({ forValue: value, forKey: 'date' }, `Value is not a date`) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { expect } from '@jest/globals' | ||
import { AssertionError, HermesError, HermesErrorCode } from './errors' | ||
|
||
test(`HermesError is instance of Error`, () => { | ||
expect(HermesError.prototype.name).toEqual('Error') | ||
}) | ||
|
||
test(`AssertionError works`, () => { | ||
const error = new AssertionError({ forValue: 'test', forKey: 'date' }, `Value is not a date`) | ||
expect(error).toBeInstanceOf(Error) | ||
expect(error).toBeInstanceOf(HermesError) | ||
expect(error.code).toEqual(HermesErrorCode.Assertion) | ||
expect(error.details?.forKey).toEqual('date') | ||
expect(error.details?.forValue).toEqual('test') | ||
expect(error.message).toEqual('Value is not a date') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
enum HermesErrorCode { | ||
Assertion = 'Assertion', | ||
} | ||
|
||
type HermesErrorDetails = { | ||
forKey?: string | ||
forValue?: unknown | ||
} | ||
|
||
abstract class HermesError extends Error { | ||
constructor( | ||
public code: HermesErrorCode, | ||
public details?: HermesErrorDetails, | ||
message?: string, | ||
) { | ||
super(message) | ||
} | ||
} | ||
|
||
class AssertionError extends HermesError { | ||
constructor( | ||
public details?: HermesErrorDetails, | ||
message?: string, | ||
) { | ||
super(HermesErrorCode.Assertion, details, message) | ||
} | ||
} | ||
|
||
export { AssertionError, HermesError, HermesErrorCode } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { CancellationPromise } from './CancellablePromise' | ||
export { addDisposeOnSigterm } from './addDisposeOnSigterm' | ||
export { assertDate } from './assert' | ||
export { OutboxConsumersCollectionName, OutboxMessagesCollectionName } from './consts' | ||
export { AssertionError, HermesError, HermesErrorCode } from './errors' | ||
export { isNil, noop, swallow } from './utils' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { expect } from '@jest/globals' | ||
import { isNil, noop, swallow } from './utils' | ||
|
||
test(`noop is a function that does nothing`, () => { | ||
expect(typeof noop).toEqual('function') | ||
expect(noop()).toBeUndefined() | ||
}) | ||
|
||
test(`isNil checks whether the value is null or undefined`, () => { | ||
expect(typeof isNil).toEqual('function') | ||
expect(isNil(null)).toBeTruthy() | ||
expect(isNil(undefined)).toBeTruthy() | ||
expect(isNil(0)).toBeFalsy() | ||
expect(isNil('')).toBeFalsy() | ||
expect(isNil(NaN)).toBeFalsy() | ||
expect(isNil({})).toBeFalsy() | ||
expect(isNil([])).toBeFalsy() | ||
expect(isNil('test')).toBeFalsy() | ||
}) | ||
|
||
test(`swallow cacthes a sync function error and do nothing`, () => { | ||
swallow(() => { | ||
throw new Error() | ||
}) // no error | ||
swallow(() => 'some value') | ||
}) | ||
|
||
test(`swallow cacthes an async function error and do nothing`, async () => { | ||
await swallow(async () => await Promise.reject(new Error())) // no error | ||
await swallow(async () => await Promise.resolve()) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": ["./src/**/*", "./test/**/*"], | ||
"compilerOptions": { | ||
"composite": true, | ||
"noEmit": true, | ||
"baseUrl": ".", | ||
"outDir": "./dist", | ||
"module": "NodeNext", | ||
"target": "ESNext", | ||
"moduleResolution": "NodeNext", | ||
"esModuleInterop": true, | ||
"noImplicitAny": true, | ||
"removeComments": true, | ||
"preserveConstEnums": true, | ||
"sourceMap": true | ||
} | ||
} |