Skip to content

Commit c4b4eff

Browse files
author
Kent C. Dodds
committed
chore: add tests for jsdom environment
1 parent d829034 commit c4b4eff

File tree

5 files changed

+89
-3
lines changed

5 files changed

+89
-3
lines changed

jest.config.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
const baseConfig = require('kcd-scripts/jest')
1+
const {
2+
collectCoverageFrom,
3+
coveragePathIgnorePatterns,
4+
coverageThreshold,
5+
watchPlugins,
6+
} = require('kcd-scripts/jest')
27

38
module.exports = {
4-
...baseConfig,
5-
testEnvironment: 'jest-environment-jsdom',
9+
collectCoverageFrom,
10+
coveragePathIgnorePatterns,
11+
coverageThreshold,
12+
watchPlugins: [
13+
...watchPlugins,
14+
require.resolve('jest-watch-select-projects'),
15+
],
16+
projects: [
17+
require.resolve('./tests/jest.config.dom.js'),
18+
require.resolve('./tests/jest.config.node.js'),
19+
],
620
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"lint": "kcd-scripts lint",
2929
"test": "kcd-scripts test",
3030
"test:update": "npm test -- --updateSnapshot --coverage",
31+
"test:debug": "node --inspect-brk ./node_modules/.bin/jest --watch --runInBand",
3132
"validate": "kcd-scripts validate",
3233
"setup": "npm install && npm run validate -s",
3334
"dtslint": "dtslint typings"
@@ -52,6 +53,8 @@
5253
"dtslint": "^0.7.7",
5354
"jest-dom": "^3.4.0",
5455
"jest-in-case": "^1.0.2",
56+
"jest-watch-select-projects": "^0.1.1",
57+
"jsdom": "^15.1.1",
5558
"kcd-scripts": "^1.4.0"
5659
},
5760
"eslintConfig": {

src/__node_tests__/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import {JSDOM} from 'jsdom'
2+
import * as dtl from '../'
3+
4+
test('works without a global dom', async () => {
5+
const container = new JSDOM(`
6+
<html>
7+
<body>
8+
<form id="login-form">
9+
<label for="username">Username</label>
10+
<input id="username" />
11+
<label for="password">Password</label>
12+
<input id="password" type="password" />
13+
<button type="submit">Submit</button>
14+
<div id="data-container"></div>
15+
</form>
16+
</body>
17+
</html>
18+
`).window.document.body
19+
container.querySelector('#login-form').addEventListener('submit', e => {
20+
e.preventDefault()
21+
const {username, password} = e.target.elements
22+
setTimeout(() => {
23+
const dataContainer = container.querySelector('#data-container')
24+
const pre = container.ownerDocument.createElement('pre')
25+
pre.dataset.testid = 'submitted-data'
26+
pre.textContent = JSON.stringify({
27+
username: username.value,
28+
password: password.value,
29+
})
30+
dataContainer.appendChild(pre)
31+
}, 20)
32+
})
33+
34+
const fakeUser = {username: 'chucknorris', password: 'i need no password'}
35+
const usernameField = dtl.getByLabelText(container, /username/i)
36+
const passwordField = dtl.getByLabelText(container, /password/i)
37+
usernameField.value = fakeUser.username
38+
passwordField.value = fakeUser.password
39+
const submitButton = dtl.getByText(container, /submit/i)
40+
dtl.fireEvent.click(submitButton)
41+
const submittedDataPre = await dtl.findByTestId(
42+
container,
43+
'submitted-data',
44+
{},
45+
{container},
46+
)
47+
const data = JSON.parse(submittedDataPre.textContent)
48+
expect(data).toEqual(fakeUser)
49+
})

tests/jest.config.dom.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('path')
2+
const baseConfig = require('kcd-scripts/jest')
3+
4+
module.exports = {
5+
...baseConfig,
6+
rootDir: path.join(__dirname, '..'),
7+
displayName: 'dom',
8+
testEnvironment: 'jest-environment-jsdom',
9+
}

tests/jest.config.node.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const path = require('path')
2+
const baseConfig = require('kcd-scripts/jest')
3+
4+
module.exports = {
5+
...baseConfig,
6+
rootDir: path.join(__dirname, '..'),
7+
setupFilesAfterEnv: undefined,
8+
displayName: 'node',
9+
testEnvironment: 'jest-environment-node',
10+
testMatch: ['**/__node_tests__/**.js'],
11+
}

0 commit comments

Comments
 (0)