Skip to content

Commit 86ee9b1

Browse files
sheerungnapse
authored andcommitted
fix: Allow running in node environment (#64)
1 parent af4c490 commit 86ee9b1

13 files changed

+38
-80
lines changed

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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
testURL: 'http://localhost/',
66
setupTestFrameworkScriptFile: '<rootDir>/setupTests.js',
77
})

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
"build": "kcd-scripts build",
1414
"lint": "kcd-scripts lint",
1515
"test": "kcd-scripts test",
16+
"test:all": "npm test && npm test -- --env jsdom",
1617
"test:update": "npm test -- --updateSnapshot --coverage",
17-
"validate": "kcd-scripts validate",
18+
"validate": "kcd-scripts validate build,lint,test:all",
1819
"setup": "npm install && npm run validate -s",
1920
"precommit": "kcd-scripts precommit"
2021
},
@@ -40,6 +41,7 @@
4041
"redent": "^2.0.0"
4142
},
4243
"devDependencies": {
44+
"jsdom": "^12.2.0",
4345
"kcd-scripts": "^0.44.0"
4446
},
4547
"eslintConfig": {

src/__tests__/helpers/document.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if (global.document) {
2+
module.exports = global.document
3+
} else {
4+
const {JSDOM} = require('jsdom')
5+
const {window} = new JSDOM()
6+
7+
module.exports = window.document
8+
}

src/__tests__/helpers/test-utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import document from './document'
2+
13
function render(html) {
24
const container = document.createElement('div')
35
container.innerHTML = html

src/__tests__/to-be-in-the-document.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import document from './helpers/document'
2+
13
test('.toBeInTheDocument', () => {
24
document.body.innerHTML = `
35
<span data-testid="html-element"><span>Html Element</span></span>

src/__tests__/to-have-style.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {render} from './helpers/test-utils'
2+
import document from './helpers/document'
23

34
describe('.toHaveStyle', () => {
45
test('handles positive test cases', () => {

src/__tests__/utils.js

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import {checkDocumentKey, deprecate} from '../utils'
2-
3-
function matcherMock() {}
1+
import {deprecate} from '../utils'
42

53
test('deprecate', () => {
64
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
@@ -16,28 +14,3 @@ test('deprecate', () => {
1614

1715
spy.mockRestore()
1816
})
19-
20-
test('checkDocumentKey', () => {
21-
const fakeKey = 'fakeKey'
22-
const realKey = 'documentElement'
23-
const badKeyMessage = `${fakeKey} is undefined on document but is required to use ${
24-
matcherMock.name
25-
}.`
26-
27-
const badDocumentMessage = `document is undefined on global but is required to use ${
28-
matcherMock.name
29-
}.`
30-
31-
expect(() =>
32-
checkDocumentKey(document, realKey, matcherMock),
33-
).not.toThrowError()
34-
35-
expect(() => {
36-
checkDocumentKey(document, fakeKey, matcherMock)
37-
}).toThrow(badKeyMessage)
38-
39-
expect(() => {
40-
//eslint-disable-next-line no-undef
41-
checkDocumentKey(undefined, realKey, matcherMock)
42-
}).toThrow(badDocumentMessage)
43-
})

src/to-be-in-the-document.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import {
33
stringify,
44
RECEIVED_COLOR as receivedColor,
55
} from 'jest-matcher-utils'
6-
import {checkHtmlElement, checkDocumentKey} from './utils'
6+
import {checkHtmlElement} from './utils'
77

88
export function toBeInTheDocument(element) {
99
if (element !== null) {
1010
checkHtmlElement(element, toBeInTheDocument, this)
1111
}
1212

13-
checkDocumentKey(global.document, 'documentElement', toBeInTheDocument)
13+
const pass =
14+
element === null ? false : element.ownerDocument.contains(element)
1415

1516
return {
16-
pass: document.documentElement.contains(element),
17+
pass,
1718
message: () => {
1819
return [
1920
matcherHint(
@@ -22,11 +23,9 @@ export function toBeInTheDocument(element) {
2223
'',
2324
),
2425
'',
25-
receivedColor(`${stringify(
26-
document.documentElement.cloneNode(false),
27-
)} ${this.isNot ? 'contains:' : 'does not contain:'} ${stringify(
28-
element ? element.cloneNode(false) : element,
29-
)}
26+
receivedColor(`${stringify(element.ownerDocument.cloneNode(false))} ${
27+
this.isNot ? 'contains:' : 'does not contain:'
28+
} ${stringify(element ? element.cloneNode(false) : element)}
3029
`),
3130
].join('\n')
3231
},

src/to-be-visible.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import {matcherHint, printReceived} from 'jest-matcher-utils'
22
import {checkHtmlElement} from './utils'
33

44
function isStyleVisible(element) {
5+
const {getComputedStyle} = element.ownerDocument.defaultView
6+
57
const {display, visibility, opacity} = getComputedStyle(element)
68
return (
79
display !== 'none' &&

src/to-contain-html.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import {matcherHint, printReceived} from 'jest-matcher-utils'
22
import {checkHtmlElement} from './utils'
33

4-
function checkHtmlText(htmlText, ...args) {
4+
function checkHtmlText(element, htmlText, ...args) {
5+
const DOMParser = element.ownerDocument.defaultView.DOMParser
56
const htmlElement =
67
typeof htmlText === 'string'
78
? new DOMParser().parseFromString(htmlText, 'text/html').body.firstChild
@@ -11,7 +12,7 @@ function checkHtmlText(htmlText, ...args) {
1112

1213
export function toContainHTML(container, htmlText) {
1314
checkHtmlElement(container, toContainHTML, this)
14-
checkHtmlText(htmlText, toContainHTML, this)
15+
checkHtmlText(container, htmlText, toContainHTML, this)
1516

1617
return {
1718
pass: container.outerHTML.includes(htmlText),

0 commit comments

Comments
 (0)