Skip to content

Commit 98eecb3

Browse files
Polyfill missing JSDOM functionality
1 parent 2af513b commit 98eecb3

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

.eslintrc.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ module.exports = {
150150
},
151151
{
152152
// Configure ESLint in test files
153-
files: ['**/*.test.{cjs,js,mjs}', 'jest.config.*', 'jest.setup.*'],
153+
files: [
154+
'**/*.test.{cjs,js,mjs}',
155+
'jest?(.*).config.*',
156+
'jest?(.*).setup.*'
157+
],
154158
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
155159
env: {
156160
browser: true,

jest.config.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ module.exports = {
4747
{
4848
...config,
4949
displayName: 'JavaScript behaviour tests',
50-
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
50+
setupFilesAfterEnv: [
51+
'<rootDir>/shared/helpers/jest/environment/jest.jsdom.setup.js'
52+
],
5153
testEnvironment: 'jsdom',
5254
testMatch: ['<rootDir>/**/*.jsdom.test.{js,mjs}']
5355
},
5456
{
5557
...config,
5658
displayName: 'JavaScript component tests',
57-
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
59+
setupFilesAfterEnv: [
60+
'<rootDir>/shared/helpers/jest/environment/jest.puppeteer.setup.js'
61+
],
5862
testEnvironment: 'jest-environment-puppeteer',
5963
testMatch: ['<rootDir>/**/*.puppeteer.test.{js,mjs}'],
6064

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require('@testing-library/jest-dom')
2+
3+
/**
4+
* Polyfill HTMLLabelElement methods for error summary
5+
*
6+
* @see {@link https://github.com/jsdom/jsdom/issues/1695}
7+
*/
8+
Object.defineProperties(HTMLLabelElement.prototype, {
9+
scrollIntoView: { value: jest.fn() }
10+
})
11+
12+
/**
13+
* Polyfill HTMLLabelElement methods for error summary
14+
*
15+
* @see {@link https://github.com/jsdom/jsdom/issues/1695}
16+
*/
17+
Object.defineProperties(HTMLLegendElement.prototype, {
18+
scrollIntoView: { value: jest.fn() }
19+
})
20+
21+
/**
22+
* Polyfill `window.matchMedia()` for GOV.UK Frontend
23+
*/
24+
Object.defineProperty(window, 'matchMedia', {
25+
writable: true,
26+
value: jest.fn().mockImplementation((query) => ({
27+
matches: true,
28+
media: query,
29+
onchange: null,
30+
addEventListener: jest.fn(),
31+
removeEventListener: jest.fn(),
32+
dispatchEvent: jest.fn()
33+
}))
34+
})

0 commit comments

Comments
 (0)