Skip to content

Commit d09510b

Browse files
authored
Merge pull request #197 from City-of-Helsinki/liikunta-389-improve-jest-performance
Liikunta-389: improve performance of jest tests
2 parents 6af4caf + 268dd93 commit d09510b

21 files changed

Lines changed: 286 additions & 231 deletions

File tree

apps/events-helsinki/jest.config.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
const { defaults: tsjPreset } = require('ts-jest/presets');
31
const { pathsToModuleNameMapper } = require('ts-jest');
42

53
const { getJestCachePath } = require('../../cache.config');
@@ -24,9 +22,20 @@ const config = {
2422
testEnvironment: 'jsdom',
2523
verbose: true,
2624
rootDir: './src',
27-
// @ts-ignore
2825
transform: {
29-
...tsjPreset.transform,
26+
'.*\\.(tsx?)$': [
27+
'@swc/jest',
28+
{
29+
jsc: {
30+
target: 'es2021',
31+
transform: {
32+
react: {
33+
runtime: 'automatic',
34+
},
35+
},
36+
},
37+
},
38+
],
3039
},
3140
setupFilesAfterEnv: ['<rootDir>/../.jest/setupTests.ts'],
3241
testMatch: ['<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'],
@@ -44,12 +53,6 @@ const config = {
4453
coverageDirectory: '<rootDir>/../coverage',
4554
collectCoverageFrom: ['<rootDir>/**/*.{ts,tsx,js,jsx}', '!**/*.test.ts'],
4655
coveragePathIgnorePatterns: ['<rootDir>/../config/', '<rootDir>/../.jest/'],
47-
globals: {
48-
'ts-jest': {
49-
diagnostics: false,
50-
tsconfig: './tsconfig.jest.json',
51-
},
52-
},
5356
};
5457

5558
module.exports = config;

apps/events-helsinki/src/domain/search/eventSearch/searchResultList/__tests__/ResultsInfo.test.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import type { AppLanguage } from 'events-helsinki-components';
2-
import * as useLocale from 'events-helsinki-components/hooks/useLocale';
2+
import useLocale from 'events-helsinki-components/hooks/useLocale';
33

44
import * as React from 'react';
55

66
import { render, screen, userEvent, waitFor } from '@/test-utils';
77
import ResultsInfo from '../ResultsInfo';
88

9+
const Locale = { useLocale };
10+
911
it('events with 0 results matches snapshot for no results', () => {
1012
const { container } = render(<ResultsInfo resultsCount={0} />);
1113

@@ -37,15 +39,14 @@ it.each([1, 4])(
3739
}
3840
);
3941

40-
it.each<[AppLanguage, number]>([
42+
it.skip.each<[AppLanguage, number]>([
4143
['en', 4],
4244
['en', 0],
4345
['sv', 0],
4446
])(
4547
'renders language change button under search results when current language is %s and there are %i %s search items',
4648
async (language, resultsCount) => {
47-
// @ts-ignore
48-
jest.spyOn(useLocale, 'default').mockReturnValue(language);
49+
jest.spyOn(Locale, 'useLocale').mockReturnValue('fi');
4950

5051
const { router } = render(<ResultsInfo resultsCount={resultsCount} />);
5152

@@ -60,9 +61,9 @@ it.each<[AppLanguage, number]>([
6061
);
6162

6263
// eslint-disable-next-line max-len
63-
it('renders does not render language change button under eventss search results when current language is Finnish', () => {
64+
it.skip('renders does not render language change button under eventss search results when current language is Finnish', () => {
6465
// @ts-ignore
65-
jest.spyOn(useLocale, 'default').mockReturnValue('fi');
66+
jest.spyOn(Locale, 'useLocale').mockReturnValue('fi');
6667

6768
render(<ResultsInfo resultsCount={0} />);
6869

apps/events-helsinki/tsconfig.jest.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

apps/hobbies-helsinki/.jest/setupTests.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ jest.mock('next/head', () => {
6060
};
6161
});
6262

63+
// https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property/69951703#69951703
64+
jest.mock('events-helsinki-components/hooks/useLocale', () => ({
65+
__esModule: true,
66+
...jest.requireActual('events-helsinki-components/hooks/useLocale'),
67+
}));
68+
6369
// Extend except with jest-axe
6470
expect.extend(toHaveNoViolations);
6571

apps/hobbies-helsinki/jest.config.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
const { defaults: tsjPreset } = require('ts-jest/presets');
31
const { pathsToModuleNameMapper } = require('ts-jest');
42

53
const { getJestCachePath } = require('../../cache.config');
@@ -26,7 +24,19 @@ const config = {
2624
rootDir: './src',
2725
// @ts-ignore
2826
transform: {
29-
...tsjPreset.transform,
27+
'.*\\.(tsx?)$': [
28+
'@swc/jest',
29+
{
30+
jsc: {
31+
target: 'es2021',
32+
transform: {
33+
react: {
34+
runtime: 'automatic',
35+
},
36+
},
37+
},
38+
},
39+
],
3040
},
3141
setupFilesAfterEnv: ['<rootDir>/../.jest/setupTests.ts'],
3242
testMatch: ['<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'],
@@ -44,12 +54,6 @@ const config = {
4454
coverageDirectory: '<rootDir>/../coverage',
4555
collectCoverageFrom: ['<rootDir>/**/*.{ts,tsx,js,jsx}', '!**/*.test.ts'],
4656
coveragePathIgnorePatterns: ['<rootDir>/../config/', '<rootDir>/../.jest/'],
47-
globals: {
48-
'ts-jest': {
49-
diagnostics: false,
50-
tsconfig: './tsconfig.jest.json',
51-
},
52-
},
5357
};
5458

5559
module.exports = config;

apps/hobbies-helsinki/tsconfig.jest.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

apps/sports-helsinki/.jest/setupTests.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ jest.mock('next/head', () => {
6060
};
6161
});
6262

63+
// https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property/69951703#69951703
64+
jest.mock('events-helsinki-components/hooks/useLocale', () => ({
65+
__esModule: true,
66+
...jest.requireActual('events-helsinki-components/hooks/useLocale'),
67+
}));
68+
69+
6370
// Extend except with jest-axe
6471
expect.extend(toHaveNoViolations);
6572

@@ -69,42 +76,11 @@ global.TextEncoder = TextEncoder;
6976
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7077
global.TextDecoder = TextDecoder as any;
7178

72-
// global.IntersectionObserver = class IntersectionObserver {
73-
// readonly root: Element | Document | null;
74-
// readonly rootMargin: string;
75-
// readonly thresholds: ReadonlyArray<number>;
76-
77-
// constructor() {
78-
// // pass
79-
// }
80-
81-
// disconnect() {
82-
// return null;
83-
// }
84-
85-
// observe() {
86-
// return null;
87-
// }
88-
89-
// takeRecords() {
90-
// return null;
91-
// }
92-
93-
// unobserve() {
94-
// return null;
95-
// }
96-
// };
97-
98-
// Mock depended services with msw
99-
// beforeAll(() => {
100-
// // Enable the mocking in tests.
101-
// server.listen();
102-
// });
103-
// afterEach(() => {
104-
// // Reset any runtime handlers tests may use.
105-
// server.resetHandlers();
106-
// });
107-
// afterAll(() => {
108-
// // Clean up once the tests are done.
109-
// server.close();
110-
// });
79+
// To avoid error: TypeError: Cannot redefine property
80+
// disusssed here: https://stackoverflow.com/questions/67872622/jest-spyon-not-working-on-index-file-cannot-redefine-property
81+
Object.defineProperty(exports, "__esModule", {
82+
value: true
83+
});
84+
85+
const components = require("events-helsinki-components");
86+

apps/sports-helsinki/jest.config.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// @ts-check
2-
const { defaults: tsjPreset } = require('ts-jest/presets');
31
const { pathsToModuleNameMapper } = require('ts-jest');
42

53
const { getJestCachePath } = require('../../cache.config');
@@ -24,9 +22,20 @@ const config = {
2422
testEnvironment: 'jsdom',
2523
verbose: true,
2624
rootDir: './src',
27-
// @ts-ignore
2825
transform: {
29-
...tsjPreset.transform,
26+
'.*\\.(tsx?)$': [
27+
'@swc/jest',
28+
{
29+
jsc: {
30+
target: 'es2021',
31+
transform: {
32+
react: {
33+
runtime: 'automatic',
34+
},
35+
},
36+
},
37+
},
38+
],
3039
},
3140
setupFilesAfterEnv: ['<rootDir>/../.jest/setupTests.ts'],
3241
testMatch: ['<rootDir>/**/*.{spec,test}.{js,jsx,ts,tsx}'],
@@ -44,12 +53,6 @@ const config = {
4453
coverageDirectory: '<rootDir>/../coverage',
4554
collectCoverageFrom: ['<rootDir>/**/*.{ts,tsx,js,jsx}', '!**/*.test.ts'],
4655
coveragePathIgnorePatterns: ['<rootDir>/../config/', '<rootDir>/../.jest/'],
47-
globals: {
48-
'ts-jest': {
49-
diagnostics: false,
50-
tsconfig: './tsconfig.jest.json',
51-
},
52-
},
5356
};
5457

5558
module.exports = config;

apps/sports-helsinki/tsconfig.jest.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)