Skip to content

Commit 0d04699

Browse files
author
Kent C. Dodds
authored
chore: update all packages and get 100% coverage (#118)
1 parent fd747bf commit 0d04699

File tree

9 files changed

+142
-16
lines changed

9 files changed

+142
-16
lines changed

jest.config.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const jestConfig = require('kcd-scripts/jest')
1+
const config = require('kcd-scripts/jest')
22

3-
module.exports = Object.assign(jestConfig, {
4-
testEnvironment: 'jest-environment-node',
5-
testURL: 'http://localhost/',
6-
setupTestFrameworkScriptFile: '<rootDir>/setupTests.js',
7-
})
3+
module.exports = {
4+
...config,
5+
projects: [
6+
require.resolve('jest-watch-select-projects'),
7+
require.resolve('./tests/jest.config.dom'),
8+
require.resolve('./tests/jest.config.node'),
9+
],
10+
}

package.json

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
"build": "kcd-scripts build",
1414
"lint": "kcd-scripts lint",
1515
"test": "kcd-scripts test",
16-
"test:all": "npm test && npm test -- --env jsdom",
1716
"test:update": "npm test -- --updateSnapshot --coverage",
18-
"validate": "kcd-scripts validate build,lint,test:all",
19-
"setup": "npm install && npm run validate -s",
20-
"precommit": "kcd-scripts precommit"
17+
"validate": "kcd-scripts validate",
18+
"setup": "npm install && npm run validate -s"
19+
},
20+
"husky": {
21+
"hooks": {
22+
"pre-commit": "kcd-scripts pre-commit"
23+
}
2124
},
2225
"files": [
2326
"dist",
@@ -33,18 +36,20 @@
3336
"author": "Ernesto Garcia <[email protected]> (http://gnapse.github.io/)",
3437
"license": "MIT",
3538
"dependencies": {
39+
"@babel/runtime": "^7.5.1",
3640
"chalk": "^2.4.1",
3741
"css": "^2.2.3",
3842
"css.escape": "^1.5.1",
3943
"jest-diff": "^24.0.0",
4044
"jest-matcher-utils": "^24.0.0",
4145
"lodash": "^4.17.11",
4246
"pretty-format": "^24.0.0",
43-
"redent": "^2.0.0"
47+
"redent": "^3.0.0"
4448
},
4549
"devDependencies": {
50+
"jest-watch-select-projects": "^0.1.2",
4651
"jsdom": "^15.1.0",
47-
"kcd-scripts": "^0.44.0"
52+
"kcd-scripts": "^1.4.0"
4853
},
4954
"eslintConfig": {
5055
"extends": "./node_modules/kcd-scripts/eslint.js",

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import document from './helpers/document'
21
import {HtmlElementTypeError} from '../utils'
2+
import document from './helpers/document'
33

44
test('.toBeInTheDocument', () => {
55
document.body.innerHTML = `
@@ -33,10 +33,13 @@ test('.toBeInTheDocument', () => {
3333
expect(() => expect(fakeElement).toBeInTheDocument()).toThrowError(
3434
HtmlElementTypeError,
3535
)
36+
expect(() => expect(nullElement).toBeInTheDocument()).toThrowError(
37+
HtmlElementTypeError,
38+
)
3639
expect(() => expect(undefinedElement).toBeInTheDocument()).toThrowError(
3740
HtmlElementTypeError,
3841
)
39-
expect(() => expect(nullElement).toBeInTheDocument()).toThrowError(
42+
expect(() => expect(undefinedElement).not.toBeInTheDocument()).toThrowError(
4043
HtmlElementTypeError,
4144
)
4245
})

src/__tests__/to-have-value.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,86 @@ describe('.toHaveValue', () => {
103103
expect(queryByTestId('radio')).toHaveValue('')
104104
}).toThrow()
105105
})
106+
107+
test('throws when the expected input value does not match', () => {
108+
const {container} = render(`<input data-testid="one" value="foo" />`)
109+
const input = container.firstChild
110+
let errorMessage
111+
try {
112+
expect(input).toHaveValue('something else')
113+
} catch (error) {
114+
errorMessage = error.message
115+
}
116+
117+
expect(errorMessage).toMatchInlineSnapshot(`
118+
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>something else</><dim>)</>
119+
120+
Expected the element to have value:
121+
<green> something else</>
122+
Received:
123+
<red> foo</>"
124+
`)
125+
})
126+
127+
test('throws when using not but the expected input value does match', () => {
128+
const {container} = render(`<input data-testid="one" value="foo" />`)
129+
const input = container.firstChild
130+
let errorMessage
131+
132+
try {
133+
expect(input).not.toHaveValue('foo')
134+
} catch (error) {
135+
errorMessage = error.message
136+
}
137+
expect(errorMessage).toMatchInlineSnapshot(`
138+
"<dim>expect(</><red>element</><dim>).not.toHaveValue(</><green>foo</><dim>)</>
139+
140+
Expected the element not to have value:
141+
<green> foo</>
142+
Received:
143+
<red> foo</>"
144+
`)
145+
})
146+
147+
test('throws when the form has no a value but a value is expected', () => {
148+
const {container} = render(`<input data-testid="one" />`)
149+
const input = container.firstChild
150+
let errorMessage
151+
152+
try {
153+
expect(input).toHaveValue()
154+
} catch (error) {
155+
errorMessage = error.message
156+
}
157+
expect(errorMessage).toMatchInlineSnapshot(`
158+
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>expected</><dim>)</>
159+
160+
Expected the element to have value:
161+
<green> (any)</>
162+
Received:
163+
"
164+
`)
165+
})
166+
167+
test('throws when the form has a value but none is expected', () => {
168+
const {container} = render(`<input data-testid="one" value="foo" />`)
169+
const input = container.firstChild
170+
let errorMessage
171+
172+
try {
173+
expect(input).not.toHaveValue()
174+
} catch (error) {
175+
errorMessage = error.message
176+
}
177+
expect(errorMessage).toMatchInlineSnapshot(`
178+
"<dim>expect(</><red>element</><dim>).not.toHaveValue(</><green>expected</><dim>)</>
179+
180+
Expected the element not to have value:
181+
<green> (any)</>
182+
Received:
183+
<red> foo</>"
184+
`)
185+
})
106186
})
187+
188+
/* eslint max-lines-per-function:0 */

src/__tests__/utils.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,19 @@ describe('checkHtmlElement', () => {
5757
checkHtmlElement(() => {}, () => {}, {})
5858
}).toThrow(HtmlElementTypeError)
5959
})
60+
61+
it('throws for almost element-like objects', () => {
62+
class FakeObject {}
63+
expect(() => {
64+
checkHtmlElement(
65+
{
66+
ownerDocument: {
67+
defaultView: {HTMLElement: FakeObject, SVGElement: FakeObject},
68+
},
69+
},
70+
() => {},
71+
{},
72+
)
73+
}).toThrow(HtmlElementTypeError)
74+
})
6075
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function toBeInTheDocument(element) {
2525
'',
2626
receivedColor(`${stringify(element.ownerDocument.cloneNode(false))} ${
2727
this.isNot ? 'contains:' : 'does not contain:'
28-
} ${stringify(element ? element.cloneNode(false) : element)}
28+
} ${stringify(element.cloneNode(false))}
2929
`),
3030
].join('\n')
3131
},

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 config = require('kcd-scripts/jest')
3+
4+
module.exports = {
5+
rootDir: path.resolve(__dirname, '..'),
6+
displayName: 'jsdom',
7+
testEnvironment: 'dom',
8+
...config,
9+
}

tests/jest.config.node.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 config = require('kcd-scripts/jest')
3+
4+
module.exports = {
5+
rootDir: path.resolve(__dirname, '..'),
6+
displayName: 'node',
7+
testEnvironment: 'node',
8+
...config,
9+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import {plugins} from 'pretty-format'
2-
import './src/extend-expect'
2+
import '../src/extend-expect'
33

44
expect.addSnapshotSerializer(plugins.ConvertAnsi)

0 commit comments

Comments
 (0)