Skip to content

Commit ab992c2

Browse files
author
andrewjtgh
committed
Suppress JSDOM errors
1 parent 1631e3e commit ab992c2

File tree

4 files changed

+26
-13
lines changed

4 files changed

+26
-13
lines changed

__mocks__/styleMock.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/(main)/quiz/practice/page.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from 'react';
2-
import { render, screen, waitFor } from '@testing-library/react';
2+
import { render, screen, waitFor, act } from '@testing-library/react';
33
import '@testing-library/jest-dom'; // Import jest-dom matchers
44
import Page from '../../../../app/(main)/quiz/practice/page';
55
import { useRouter } from 'next/navigation';
66
import { QuizService } from '../../../../service/QuizService';
7-
import { QuestionsService } from '../../../../service/QuestionsService';
87

98
// Mock the useRouter hook
109
vi.mock('next/navigation', () => ({
@@ -45,8 +44,10 @@ describe('Page', () => {
4544
})
4645
) as jest.Mock;
4746

48-
// Render the page
49-
render(<Page />);
47+
// Wrap the rendering and state updates in act()
48+
await act(async () => {
49+
render(<Page />);
50+
});
5051

5152
// Simulate loading the URL
5253
const response = await fetch('http://localhost:3000/quiz/practice');

vitest.config.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { defineConfig } from 'vitest/config';
22
import react from '@vitejs/plugin-react';
3+
import { resolve } from 'path';
34

45
export default defineConfig({
56
resolve: {
67
alias: {
7-
'@': '/',
8+
'@': resolve(__dirname, './'),
89
},
910
},
1011
test: {
11-
globals: true, // Enable global APIs
12-
environment: 'jsdom', // Use jsdom environment for browser-like testing
13-
css: false,
14-
setupFiles: './vitest.setup.ts', // Optional setup file
12+
globals: true,
13+
environment: 'jsdom',
14+
setupFiles: './vitest.setup.ts',
1515
},
16-
plugins: [react()], // Add the React plugin
17-
});
16+
plugins: [react()],
17+
});

vitest.setup.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
11
// vitest.setup.ts
2-
import '@testing-library/jest-dom'; // Import this to use jest-dom matchers
2+
import '@testing-library/jest-dom'; // Import this to use jest-dom matchers
3+
4+
// Suppress jsdom CSS parsing errors
5+
const suppressedErrors = /(Could not parse CSS stylesheet)/;
6+
7+
const originalConsoleError = console.error;
8+
console.error = (...args) => {
9+
if (args[0] && suppressedErrors.test(args[0])) {
10+
// Suppress the specific jsdom CSS error
11+
return;
12+
}
13+
// Call the original console.error for other errors
14+
originalConsoleError(...args);
15+
};

0 commit comments

Comments
 (0)