Skip to content

Commit 1b12385

Browse files
sheerunKent C. Dodds
authored andcommitted
fix(node): allow running in node environment (#114)
Now that jest-dom supports node environment I could add support for it to this package as well. Fixes #100
1 parent a7fe808 commit 1b12385

15 files changed

+173
-122
lines changed

.size-snapshot.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"dist/dom-testing-library.umd.js": {
3-
"bundled": 115443,
4-
"minified": 50960,
5-
"gzipped": 15294
3+
"bundled": 144335,
4+
"minified": 52790,
5+
"gzipped": 15639
66
}
77
}

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const jestConfig = require('kcd-scripts/jest')
22

33
module.exports = Object.assign(jestConfig, {
4-
testEnvironment: 'jest-environment-jsdom',
4+
testEnvironment: 'jest-environment-node',
55
})

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
"build": "kcd-scripts build && kcd-scripts build --bundle umd --no-clean",
2828
"lint": "kcd-scripts lint",
2929
"test": "kcd-scripts test",
30+
"test:all": "npm test && npm test -- --env jsdom",
3031
"test:update": "npm test -- --updateSnapshot --coverage",
31-
"validate": "kcd-scripts validate",
32+
"validate": "kcd-scripts validate build,lint,test:all",
3233
"setup": "npm install && npm run validate -s",
3334
"precommit": "kcd-scripts precommit",
3435
"dtslint": "dtslint typings"
@@ -38,14 +39,15 @@
3839
"typings"
3940
],
4041
"dependencies": {
41-
"mutationobserver-shim": "^0.3.2",
42+
"@sheerun/mutationobserver-shim": "^0.3.2",
4243
"pretty-format": "^23.6.0",
4344
"wait-for-expect": "^1.0.0"
4445
},
4546
"devDependencies": {
4647
"dtslint": "^0.3.0",
47-
"jest-dom": "^1.7.0",
48+
"jest-dom": "^2.0.4",
4849
"jest-in-case": "^1.0.2",
50+
"jsdom": "^12.2.0",
4951
"kcd-scripts": "^0.41.0",
5052
"microbundle": "^0.4.4"
5153
},

src/__tests__/element-queries.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import 'jest-dom/extend-expect'
22
import {render} from './helpers/test-utils'
3+
import document from './helpers/document'
34

45
beforeEach(() => {
5-
window.Cypress = null
6+
document.defaultView.Cypress = null
67
})
78

89
test('query can return null', () => {
@@ -148,9 +149,7 @@ test('get element by its alt text', () => {
148149
<img alt="finding nemo poster" src="/finding-nemo.png" />
149150
</div>,
150151
`)
151-
expect(getByAltText(/fin.*nem.*poster$/i).src).toBe(
152-
'http://localhost/finding-nemo.png',
153-
)
152+
expect(getByAltText(/fin.*nem.*poster$/i).src).toContain('/finding-nemo.png')
154153
})
155154

156155
test('query/get element by its title', () => {
@@ -489,7 +488,7 @@ test('test the debug helper prints the dom state here', () => {
489488
})
490489

491490
test('get throws a useful error message without DOM in Cypress', () => {
492-
window.Cypress = {}
491+
document.defaultView.Cypress = {}
493492
const {
494493
getByLabelText,
495494
getBySelectText,

src/__tests__/events.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {fireEvent} from '..'
2+
import document from './helpers/document'
23

34
const eventTypes = [
45
{
@@ -171,7 +172,7 @@ test('assigning a value to a target that cannot have a value throws an error', (
171172

172173
test('assigning the files property on an input', () => {
173174
const node = document.createElement('input')
174-
const file = new File(['(⌐□_□)'], 'chucknorris.png', {
175+
const file = new document.defaultView.File(['(⌐□_□)'], 'chucknorris.png', {
175176
type: 'image/png',
176177
})
177178
fireEvent.change(node, {target: {files: [file]}})

src/__tests__/example.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {getByLabelText, getByText, getByTestId, queryByTestId, wait} from '../'
33
// adds special assertions like toHaveTextContent
44
import 'jest-dom/extend-expect'
5+
import document from './helpers/document'
56

67
function getExampleDOM() {
78
// This is just a raw example of setting up some DOM

src/__tests__/get-queries-for-element.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {getQueriesForElement} from '../get-queries-for-element'
22
import {queries} from '..'
3+
import document from './helpers/document'
34

45
test('uses default queries', () => {
56
const container = document.createElement('div')

src/__tests__/helpers/document.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
let testWindow = typeof window === 'undefined' ? undefined : window
2+
3+
if (typeof window === 'undefined') {
4+
const {JSDOM} = require('jsdom')
5+
const dom = new JSDOM()
6+
testWindow = dom.window
7+
}
8+
9+
// TODO: these events are not supported by JSDOM so we need to shim them
10+
11+
if (!testWindow.ClipboardEvent) {
12+
testWindow.ClipboardEvent = class ClipboardEvent extends testWindow.Event {}
13+
}
14+
15+
if (!testWindow.DragEvent) {
16+
testWindow.DragEvent = class DragEvent extends testWindow.Event {}
17+
}
18+
19+
if (!testWindow.TransitionEvent) {
20+
testWindow.TransitionEvent = class TransitionEvent extends testWindow.Event {}
21+
}
22+
23+
if (!testWindow.AnimationEvent) {
24+
testWindow.AnimationEvent = class AnimationEvent extends testWindow.Event {}
25+
}
26+
27+
if (!testWindow.AnimationEvent) {
28+
testWindow.AnimationEvent = class AnimationEvent extends testWindow.Event {}
29+
}
30+
31+
if (!testWindow.InputEvent) {
32+
testWindow.InputEvent = class InputEvent extends testWindow.Event {}
33+
}
34+
35+
module.exports = testWindow.document

src/__tests__/helpers/test-utils.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {getQueriesForElement} from '../../get-queries-for-element'
2+
import document from './document'
23

34
function render(html) {
45
const container = document.createElement('div')

src/__tests__/pretty-dom.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {prettyDOM} from '../pretty-dom'
22
import {render} from './helpers/test-utils'
3+
import document from './helpers/document'
34

45
test('it prints out the given DOM element tree', () => {
56
const {container} = render('<div>Hello World!</div>')

0 commit comments

Comments
 (0)