Skip to content

Commit 20c23d3

Browse files
committed
Update test handling in favicon class
1 parent d6ea235 commit 20c23d3

2 files changed

Lines changed: 18 additions & 10 deletions

File tree

‎scripts/build/__tests__/favicon.spec.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest'
22
import sharp, { type Metadata } from 'sharp'
33
import toIco from 'to-ico'
44
import { writeFile } from 'fs/promises'
5+
import { UnitTestError } from '@test/errors'
56

67
// Mock external dependencies
78
vi.mock('sharp')
@@ -35,7 +36,7 @@ describe('favicon.ts', () => {
3536
// Since generateIcoFavicon is not exported, we'll create a local test
3637
const generateIcoFavicon = ({ width, height, density }: Partial<Metadata>) => {
3738
if (!width || !height || !density) {
38-
throw new Error(`Required option not passed to generateIcoFavicon`)
39+
throw new UnitTestError(`Required option not passed to generateIcoFavicon`)
3940
}
4041
}
4142

@@ -47,7 +48,7 @@ describe('favicon.ts', () => {
4748
it('should throw error when height is missing', () => {
4849
const generateIcoFavicon = ({ width, height, density }: Partial<Metadata>) => {
4950
if (!width || !height || !density) {
50-
throw new Error(`Required option not passed to generateIcoFavicon`)
51+
throw new UnitTestError(`Required option not passed to generateIcoFavicon`)
5152
}
5253
}
5354
const metadata: Partial<Metadata> = { width: 512, density: 72 }
@@ -60,7 +61,7 @@ describe('favicon.ts', () => {
6061
it('should throw error when density is missing', () => {
6162
const generateIcoFavicon = ({ width, height, density }: Partial<Metadata>) => {
6263
if (!width || !height || !density) {
63-
throw new Error(`Required option not passed to generateIcoFavicon`)
64+
throw new UnitTestError(`Required option not passed to generateIcoFavicon`)
6465
}
6566
}
6667
const metadata: Partial<Metadata> = { width: 512, height: 512 }
@@ -111,7 +112,7 @@ describe('favicon.ts', () => {
111112
it('should throw error when required parameters are missing', () => {
112113
const generatePngFavicon = ({ width, height, density }: Partial<Metadata>) => {
113114
if (!width || !height || !density) {
114-
throw new Error(`Required option not passed to generatePngFavicon`)
115+
throw new UnitTestError(`Required option not passed to generatePngFavicon`)
115116
}
116117
}
117118

‎scripts/build/favicon.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
import sharp, { type Metadata } from 'sharp'
2222
import toIco from 'to-ico'
2323
import { writeFile, mkdir } from 'fs/promises'
24+
import { ClientScriptError } from '@components/scripts/errors'
2425

2526
type IconGenerator = (_options: Metadata) => Promise<Buffer>
2627

2728
const faviconPath = `src/assets/favicon.svg`
2829
const generateIcoFavicon: IconGenerator = async ({ width, height, density }) => {
29-
if (!width || !height || !density)
30-
throw new Error(`Required option not passed to generateIcoFavicon`)
30+
if (!width || !height || !density) {
31+
const message = `Required option not passed to generateIcoFavicon`
32+
throw new ClientScriptError({ message })
33+
}
3134
const faviconDimensions = [32, 64]
3235
// Create buffer for each size
3336
return Promise.all(
@@ -42,8 +45,10 @@ const generateIcoFavicon: IconGenerator = async ({ width, height, density }) =>
4245
}
4346

4447
const generatePngFavicon: IconGenerator = ({ density, width, height }) => {
45-
if (!width || !height || !density)
46-
throw new Error(`Required option not passed to generatePngFavicon`)
48+
if (!width || !height || !density) {
49+
const message = `Required option not passed to generatePngFavicon`
50+
throw new ClientScriptError({ message })
51+
}
4752
return sharp(faviconPath, {
4853
density: (180 / Math.max(width, height)) * density,
4954
})
@@ -53,8 +58,10 @@ const generatePngFavicon: IconGenerator = ({ density, width, height }) => {
5358
}
5459

5560
const generatePwaIcon = (size: number): IconGenerator => ({ density, width, height }) => {
56-
if (!width || !height || !density)
57-
throw new Error(`Required option not passed to generatePwaIcon`)
61+
if (!width || !height || !density) {
62+
const message = `Required option not passed to generatePwaIcon`
63+
throw new ClientScriptError({ message })
64+
}
5865
return sharp(faviconPath, {
5966
density: (size / Math.max(width, height)) * density,
6067
})

0 commit comments

Comments
 (0)