Skip to content

Commit d65d029

Browse files
author
martin
committed
Fix test configuration and DashboardPage mock
- Use null-loader to exclude test files from webpack build - Fix DashboardPage.test.tsx mock pattern (proper Jest hoisting) - Fix tasksSlice.ts error handling (optional chaining) - Update jest.config.js with proper ts-jest configuration
1 parent 732ed9e commit d65d029

7 files changed

Lines changed: 122 additions & 24 deletions

File tree

frontend/craco.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ module.exports = {
77
// Add .vue extension to resolve
88
webpackConfig.resolve.extensions.push('.vue');
99

10-
// Exclude test files from webpack compilation
10+
// Exclude test files from webpack compilation - use null-loader pattern
11+
// This replaces test files with empty modules
1112
webpackConfig.module.rules.unshift({
1213
test: /\.(test|spec)\.(ts|tsx|js|jsx)$/,
13-
loader: 'ignore-loader',
14+
use: 'null-loader',
1415
});
1516

16-
// Also exclude __tests__ directories
17+
// Exclude __tests__ directories
1718
webpackConfig.module.rules.unshift({
18-
test: /[\\/]__tests__[\\/]/,
19-
loader: 'ignore-loader',
19+
test: /[\\/]__tests__[\\/].*\.(ts|tsx|js|jsx)$/,
20+
use: 'null-loader',
2021
});
2122

2223
// Add vue-loader rule BEFORE the oneOf rule

frontend/jest.config.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
module.exports = {
2+
// Use jsdom test environment for React tests
23
testEnvironment: 'jsdom',
34
// Ensure Jest globals (describe, it, expect, jest) are available
45
injectGlobals: true,
6+
// Globals available in test files
7+
globals: {
8+
'ts-jest': {
9+
tsconfig: {
10+
jsx: 'react-jsx',
11+
},
12+
},
13+
},
514
// Setup files that run BEFORE test environment
615
setupFiles: ['<rootDir>/src/testSetupBeforeEnv.ts'],
716
// Setup files that run AFTER test environment (for DOM matchers etc.)
@@ -11,11 +20,7 @@ module.exports = {
1120
'^@/(.*)$': '<rootDir>/src/$1',
1221
},
1322
transform: {
14-
'^.+\\.(ts|tsx)$': ['ts-jest', {
15-
tsconfig: {
16-
jsx: 'react-jsx',
17-
},
18-
}],
23+
'^.+\\.(ts|tsx)$': 'ts-jest',
1924
'^.+\\.(js|jsx)$': 'babel-jest',
2025
},
2126
// Transform axios and other ES modules
@@ -26,6 +31,11 @@ module.exports = {
2631
'**/__tests__/**/*.(ts|tsx|js)',
2732
'**/*.(test|spec).(ts|tsx|js)',
2833
],
34+
// Ignore E2E test directory
35+
testPathIgnorePatterns: [
36+
'/node_modules/',
37+
'/e2e/',
38+
],
2939
collectCoverageFrom: [
3040
'src/**/*.{ts,tsx}',
3141
'!src/**/*.d.ts',

frontend/package-lock.json

Lines changed: 75 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"@playwright/test": "^1.40.1",
7575
"@vue/compiler-sfc": "^3.5.27",
7676
"ignore-loader": "^0.1.2",
77+
"null-loader": "^4.0.1",
7778
"vue-loader": "^17.4.2"
7879
}
7980
}

frontend/src/pages/__tests__/DashboardPage.test.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,22 @@ import DashboardPage from '../DashboardPage';
77
import authReducer from '../../store/slices/authSlice';
88
import tasksReducer from '../../store/slices/tasksSlice';
99

10-
// Mock functions must be prefixed with 'mock' to be hoisted correctly
11-
const mockGet = jest.fn();
12-
const mockPost = jest.fn();
13-
14-
// Mock axios
15-
jest.mock('../../api/axios', () => ({
16-
__esModule: true,
17-
default: {
18-
get: mockGet,
19-
post: mockPost,
20-
},
21-
}));
10+
// Mock axios - define mock functions inside the factory
11+
jest.mock('../../api/axios', () => {
12+
const mockAxios = {
13+
get: jest.fn(),
14+
post: jest.fn(),
15+
};
16+
return {
17+
__esModule: true,
18+
default: mockAxios,
19+
};
20+
});
21+
22+
// Import the mocked module to access mock functions
23+
import axiosInstance from '../../api/axios';
24+
const mockGet = axiosInstance.get as jest.Mock;
25+
const mockPost = axiosInstance.post as jest.Mock;
2226

2327
// Mock recharts to avoid canvas issues in tests
2428
jest.mock('recharts', () => ({

frontend/src/store/slices/tasksSlice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ const tasksSlice = createSlice({
6767
})
6868
.addCase(fetchTasks.rejected, (state, action) => {
6969
console.error('Failed to fetch tasks:', action.error);
70-
console.error('Error message:', action.error.message);
70+
console.error('Error message:', action.error?.message);
7171
state.loading = false;
72-
state.error = action.error.message || 'Failed to fetch tasks';
72+
state.error = action.error?.message || 'Failed to fetch tasks';
7373
})
7474
.addCase(createTask.fulfilled, (state, action) => {
7575
state.tasks.push(action.payload);

frontend/src/testSetupBeforeEnv.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ Object.defineProperty(global, 'sessionStorage', {
4242
value: new StorageMock(),
4343
writable: true,
4444
});
45+
46+
// Ensure Jest globals are defined (they should be, but this is a safety net)
47+
if (typeof global.jest === 'undefined') {
48+
// This should never happen in a proper Jest environment
49+
// If it does, something is wrong with the test setup
50+
console.warn('Jest globals not found - this file should only run in Jest');
51+
}

0 commit comments

Comments
 (0)