Skip to content

Commit 256b171

Browse files
fix(eds-color-palette-generator): fix vitest and playwright tests (#3936)
1 parent ae54d33 commit 256b171

File tree

6 files changed

+36
-7
lines changed

6 files changed

+36
-7
lines changed

.github/workflows/playwright.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ jobs:
1919
- uses: actions/setup-node@v4
2020
with:
2121
node-version: lts/*
22+
- name: Install pnpm
23+
run: npm install -g pnpm
2224
- name: Install dependencies
23-
run: npm ci
25+
run: pnpm install
2426
working-directory: packages/eds-color-palette-generator
2527
- name: Install Playwright Browsers
26-
run: npx playwright install --with-deps
28+
run: pnpm exec playwright install --with-deps
2729
working-directory: packages/eds-color-palette-generator
2830
- name: Run Playwright tests
29-
run: npx playwright test
31+
run: pnpm run test:e2e
3032
working-directory: packages/eds-color-palette-generator
3133
- uses: actions/upload-artifact@v4
3234
if: ${{ !cancelled() }}

packages/eds-color-palette-generator/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"description": "EDS Color Palette Generator",
44
"version": "0.1.0",
55
"private": true,
6+
"type": "module",
67
"scripts": {
78
"dev": "next dev --turbopack",
89
"build": "next build",

packages/eds-color-palette-generator/src/utils/color.test.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ describe('Color Contrast Tests', () => {
4343
foreground: 'invalid-color',
4444
background: '#ffffff',
4545
algorithm: 'WCAG21',
46+
silent: true,
4647
})
4748

4849
expect(result).toBe('0')
@@ -258,7 +259,12 @@ describe('Edge Cases and Error Handling', () => {
258259
const foreground = 'oklch(invalid values)'
259260
const background = '#ffffff'
260261

261-
const result = contrast({ foreground, background, algorithm: 'APCA' })
262+
const result = contrast({
263+
foreground,
264+
background,
265+
algorithm: 'APCA',
266+
silent: true,
267+
})
262268

263269
expect(result).toBe('0')
264270
})
@@ -268,6 +274,7 @@ describe('Edge Cases and Error Handling', () => {
268274
foreground: '',
269275
background: '#ffffff',
270276
algorithm: 'APCA',
277+
silent: true,
271278
})
272279

273280
expect(result).toBe('0')
@@ -296,7 +303,12 @@ describe('Edge Cases and Error Handling', () => {
296303
const foreground = 'oklch(0.2 0 NaN)' // Grayscale with NaN hue
297304
const background = 'oklch(0.8 0 180)' // Light gray
298305

299-
const result = contrast({ foreground, background, algorithm: 'APCA' })
306+
const result = contrast({
307+
foreground,
308+
background,
309+
algorithm: 'APCA',
310+
silent: true,
311+
})
300312

301313
// Should handle gracefully and calculate based on lightness
302314
expect(typeof result).toBe('string')

packages/eds-color-palette-generator/src/utils/color.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,19 @@ export function generateColorScale(
7272
* @param foreground - Text/foreground color (OKLCH or HEX string)
7373
* @param background - Background color (OKLCH or HEX string)
7474
* @param algorithm - Contrast calculation algorithm ("WCAG21" or "APCA")
75+
* @param silent - Whether to suppress error logging (useful for tests)
7576
* @returns Contrast score as number or string
7677
*/
7778
export function contrast({
7879
foreground,
7980
background,
8081
algorithm,
82+
silent = false,
8183
}: {
8284
foreground: string
8385
background: string
8486
algorithm: Algorithms
87+
silent?: boolean
8588
}): string | number {
8689
try {
8790
const fgColor = new Color(foreground)
@@ -93,7 +96,9 @@ export function contrast({
9396
decimals,
9497
)
9598
} catch (error) {
96-
console.error('Error calculating contrast:', error)
99+
if (!silent) {
100+
console.error('Error calculating contrast:', error)
101+
}
97102
// Return default values in case of error
98103
return '0'
99104
}

packages/eds-color-palette-generator/tests/e2e/change-color.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ test('should change color name and hex', async ({ page }) => {
1111
await page.getByTestId('color-hex-input-0').fill('#ee7e17')
1212

1313
await expect(page.getByTestId('brand-10')).toMatchAriaSnapshot(
14-
'- \'button "Color 11: #9f561b, Click for details"\'',
14+
'- \'button "Color 11: oklch(0.420 0.113 54.7), Click for details"\'',
1515
)
1616
})

packages/eds-color-palette-generator/vitest.config.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ export default defineConfig({
77
globals: true,
88
include: ['src/**/*.{test,spec}.{js,ts}'],
99
exclude: ['tests/**/*', 'node_modules/**/*'],
10+
silent: false,
11+
reporters: ['verbose'],
12+
onConsoleLog(log, type) {
13+
// Suppress expected error logs from color.js during tests
14+
if (type === 'stderr' && log.includes('Error calculating contrast:')) {
15+
return false
16+
}
17+
return true
18+
},
1019
},
1120
resolve: {
1221
alias: {

0 commit comments

Comments
 (0)