|
| 1 | +/** |
| 2 | + * Tests for error handling routines and custom errors |
| 3 | + */ |
| 4 | +import { describe, expect, test } from 'vitest' |
| 5 | +import { BuildError, isBuildError } from '@lib/errors/BuildError' |
| 6 | + |
| 7 | +describe(`BuildError class is constructible`, () => { |
| 8 | + test(`Class is properly constructed`, () => { |
| 9 | + try { |
| 10 | + throw new BuildError(`Test error`) |
| 11 | + } catch (err) { |
| 12 | + if (!isBuildError(err)) throw new Error() |
| 13 | + // The name property should be set to the error`s name |
| 14 | + expect(err.name).toBe(`BuildError`) |
| 15 | + |
| 16 | + // The error should be an instance of its class |
| 17 | + expect(err).toBeInstanceOf(BuildError) |
| 18 | + |
| 19 | + // The error should be an instance of builtin Error |
| 20 | + expect(err).toBeInstanceOf(Error) |
| 21 | + |
| 22 | + // toString should return the default error message formatting |
| 23 | + expect(err.toString()).toBe(`BuildError: Test error`) |
| 24 | + |
| 25 | + // The error should have recorded a stack |
| 26 | + expect(err.stack).toEqual(expect.any(String)) |
| 27 | + } |
| 28 | + }) |
| 29 | +}) |
| 30 | + |
| 31 | +describe(`BuildError has proper inheritance and props set`, () => { |
| 32 | + test(`Instance`, () => { |
| 33 | + expect(BuildError).toHaveInProtoChain(Error) |
| 34 | + }) |
| 35 | + |
| 36 | + test(`Extended`, () => { |
| 37 | + class SubError extends BuildError {} |
| 38 | + const sut = new SubError(`test message`) |
| 39 | + expect(SubError).toHaveInProtoChain(Error, BuildError) |
| 40 | + //expect(sut).toHaveProperty(`name`, `SubError`) |
| 41 | + expect(sut).toHaveProperty(`message`, `test message`) |
| 42 | + }) |
| 43 | + |
| 44 | + test(`Extended with constructor`, () => { |
| 45 | + class HttpError extends BuildError { |
| 46 | + constructor( |
| 47 | + /* eslint-disable-next-line no-unused-vars */ |
| 48 | + public code: number, |
| 49 | + message?: string |
| 50 | + ) { |
| 51 | + super(message) |
| 52 | + } |
| 53 | + } |
| 54 | + const sut = new HttpError(404, `test message`) |
| 55 | + expect(HttpError).toHaveInProtoChain(Error, BuildError) |
| 56 | + //expect(sut).toHaveProperty(`name`, `HttpError`) |
| 57 | + expect(sut).toHaveProperty(`message`, `test message`) |
| 58 | + expect(sut).toHaveProperty(`code`, 404) |
| 59 | + }) |
| 60 | + |
| 61 | + test(`Extended with name`, () => { |
| 62 | + class RenamedError extends BuildError { |
| 63 | + constructor(name: string, message?: string) { |
| 64 | + super(message) |
| 65 | + Object.defineProperty(this, `name`, { value: name }) |
| 66 | + } |
| 67 | + } |
| 68 | + const sut = new RenamedError(`test`, `test message`) |
| 69 | + expect(RenamedError).toHaveInProtoChain(Error, BuildError) |
| 70 | + expect(sut).toHaveProperty(`name`, `test`) |
| 71 | + expect(sut).toHaveProperty(`message`, `test message`) |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe(`BuildError construction works with new`, () => { |
| 76 | + test(`Basic properties`, () => { |
| 77 | + const sut = new BuildError(`test message`) |
| 78 | + expect(sut).toHaveProperty(`name`, `BuildError`) |
| 79 | + expect(sut).toHaveProperty(`message`, `test message`) |
| 80 | + }) |
| 81 | + |
| 82 | + test(`Without message`, () => { |
| 83 | + const sut = new BuildError() |
| 84 | + expect(sut).toHaveProperty(`name`, `BuildError`) |
| 85 | + expect(sut).toHaveProperty(`message`, ``) |
| 86 | + }) |
| 87 | + |
| 88 | + test(`With build-specific properties`, () => { |
| 89 | + const sut = new BuildError(`test message`, { |
| 90 | + phase: 'compilation', |
| 91 | + tool: 'astro', |
| 92 | + exitCode: 1, |
| 93 | + filePath: '/src/test.ts', |
| 94 | + lineNumber: 10, |
| 95 | + columnNumber: 5 |
| 96 | + }) |
| 97 | + expect(sut).toHaveProperty(`name`, `BuildError`) |
| 98 | + expect(sut).toHaveProperty(`message`, `test message`) |
| 99 | + expect(sut.phase).toBe('compilation') |
| 100 | + expect(sut.tool).toBe('astro') |
| 101 | + expect(sut.exitCode).toBe(1) |
| 102 | + expect(sut.filePath).toBe('/src/test.ts') |
| 103 | + expect(sut.lineNumber).toBe(10) |
| 104 | + expect(sut.columnNumber).toBe(5) |
| 105 | + }) |
| 106 | +}) |
| 107 | + |
| 108 | +describe(`toString behavior in logging`, () => { |
| 109 | + test(`Outputs error with message when coerced to string`, () => { |
| 110 | + expect(`${new BuildError(`Hello`)}`).toMatch(`BuildError: Hello`) |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +describe(`toJSON behavior overridden`, () => { |
| 115 | + test(`Outputs structured JSON error with toJSON`, () => { |
| 116 | + const sut = new BuildError(`test message`) |
| 117 | + expect(sut.toJSON()).toEqual( |
| 118 | + expect.objectContaining({ |
| 119 | + error: expect.objectContaining({ |
| 120 | + message: expect.any(String), |
| 121 | + name: expect.any(String), |
| 122 | + stack: expect.any(String), |
| 123 | + }), |
| 124 | + }) |
| 125 | + ) |
| 126 | + }) |
| 127 | +}) |
| 128 | + |
| 129 | +describe(`Static factory methods`, () => { |
| 130 | + test(`compilation creates error with compilation phase`, () => { |
| 131 | + const sut = BuildError.compilation( |
| 132 | + `TypeScript compilation failed`, |
| 133 | + '/src/component.ts', |
| 134 | + 15, |
| 135 | + 5 |
| 136 | + ) |
| 137 | + expect(sut.message).toBe('TypeScript compilation failed') |
| 138 | + expect(sut.phase).toBe('compilation') |
| 139 | + expect(sut.filePath).toBe('/src/component.ts') |
| 140 | + expect(sut.lineNumber).toBe(15) |
| 141 | + expect(sut.columnNumber).toBe(5) |
| 142 | + }) |
| 143 | + |
| 144 | + test(`fileOperation creates error with file-operation phase`, () => { |
| 145 | + const sut = BuildError.fileOperation( |
| 146 | + `Could not read config file`, |
| 147 | + '/astro.config.ts', |
| 148 | + 'read' |
| 149 | + ) |
| 150 | + expect(sut.message).toBe('Could not read config file') |
| 151 | + expect(sut.phase).toBe('file-operation') |
| 152 | + expect(sut.filePath).toBe('/astro.config.ts') |
| 153 | + }) |
| 154 | + |
| 155 | + test(`bundling creates error with bundling phase`, () => { |
| 156 | + const sut = BuildError.bundling( |
| 157 | + `Bundling failed`, |
| 158 | + 'vite', |
| 159 | + 1 |
| 160 | + ) |
| 161 | + expect(sut.message).toBe('Bundling failed') |
| 162 | + expect(sut.phase).toBe('bundling') |
| 163 | + expect(sut.tool).toBe('vite') |
| 164 | + expect(sut.exitCode).toBe(1) |
| 165 | + }) |
| 166 | +}) |
0 commit comments