]> Piment Noir Git Repositories - e-mobility-charging-stations-simulator.git/blob - tests/exception/BaseError.test.ts
d9a5335de7c96523bfe72a689f4cfe38aa5e17af
[e-mobility-charging-stations-simulator.git] / tests / exception / BaseError.test.ts
1 /**
2 * @file Tests for BaseError
3 * @description Unit tests for base error class functionality
4 */
5 import assert from 'node:assert/strict'
6 import { afterEach, describe, it } from 'node:test'
7
8 import { BaseError } from '../../src/exception/BaseError.js'
9 import { standardCleanup } from '../helpers/TestLifecycleHelpers.js'
10
11 await describe('BaseError', async () => {
12 afterEach(() => {
13 standardCleanup()
14 })
15 await it('should create instance with default values', () => {
16 const baseError = new BaseError()
17 assert.ok(baseError instanceof BaseError)
18 assert.strictEqual(baseError.name, 'BaseError')
19 assert.strictEqual(baseError.message, '')
20 assert.ok(typeof baseError.stack === 'string')
21 assert.notStrictEqual(baseError.stack, '')
22 assert.strictEqual(baseError.cause, undefined)
23 assert.ok(baseError.date instanceof Date)
24 })
25
26 await it('should create instance with custom message', () => {
27 const baseError = new BaseError('Test message')
28 assert.ok(baseError instanceof BaseError)
29 assert.strictEqual(baseError.message, 'Test message')
30 })
31
32 await it('should be an instance of Error', () => {
33 const baseError = new BaseError()
34 assert.ok(baseError instanceof Error)
35 })
36
37 await it('should contain stack trace with class name', () => {
38 const baseError = new BaseError()
39 assert.ok(baseError.stack?.includes('BaseError'))
40 })
41
42 await it('should set date close to current time', () => {
43 const beforeNow = Date.now()
44 const baseError = new BaseError()
45 const afterNow = Date.now()
46 assert.ok(baseError.date.getTime() >= beforeNow - 1000)
47 assert.ok(baseError.date.getTime() <= afterNow + 1000)
48 })
49
50 await it('should set name to subclass name when extended', () => {
51 class TestSubError extends BaseError {}
52
53 const testSubError = new TestSubError()
54 assert.strictEqual(testSubError.name, 'TestSubError')
55 assert.ok(testSubError instanceof BaseError)
56 assert.ok(testSubError instanceof Error)
57 })
58 })