Skip to content

Commit 4a51589

Browse files
Merge pull request #25 from markusguenther/migrate-to-jest
FEATURE: Migrate to jest testing framework
2 parents 9b50dba + 09ec9cf commit 4a51589

File tree

6 files changed

+2262
-293
lines changed

6 files changed

+2262
-293
lines changed

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
],
2323
"dependencies": {
2424
"hoist-non-react-statics": "^3.0.1",
25-
"invariant": "^2.2.4"
25+
"invariant": "^2.2.4",
26+
"jest": "^23.4.2"
2627
},
2728
"devDependencies": {
2829
"@types/react": "^16.4.7",
@@ -37,10 +38,8 @@
3738
"eslint-config-prettier": "^2.9.0",
3839
"eslint-plugin-prettier": "^2.6.2",
3940
"eslint-plugin-react": "^7.10.0",
40-
"expect": "^1.18.0",
4141
"fbjs": "^0.8.17",
4242
"jsdom": "^9.8.3",
43-
"mocha": "^3.3.0",
4443
"prettier": "^1.14.0",
4544
"prop-types": "^15.6.2",
4645
"react": "^16.4.2",
@@ -58,7 +57,7 @@
5857
"lint": "eslint src test",
5958
"lint-check": "eslint --print-config .eslintrc.js | eslint-config-prettier-check",
6059
"prepublish": "rimraf lib && npm run build",
61-
"test": "mocha --compilers js:babel-core/register --recursive --reporter spec --require ./test/setup.js",
60+
"test": "jest",
6261
"test:watch": "npm test -- --watch"
6362
},
6463
"license": "MIT",
@@ -72,9 +71,11 @@
7271
},
7372
"greenkeeper": {
7473
"ignore": [
75-
"expect",
76-
"mocha",
7774
"jsdom"
7875
]
76+
},
77+
"jest": {
78+
"verbose": true,
79+
"testURL": "http://localhost/"
7980
}
8081
}

test/components/ThemeProvider.spec.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import React, { Component } from 'react'
2-
import expect from 'expect'
32
import PropTypes from 'prop-types'
43
import TestUtils from 'react-dom/test-utils'
54
import { ThemeProvider } from '../../src/index'
5+
import { jsdom } from 'jsdom'
66

7-
before(function() {
8-
/* eslint-disable no-console */
9-
console.error = function() {}
7+
const documentDom = jsdom('<!doctype html><html><body></body></html>')
8+
beforeEach(() => {
9+
global.document = documentDom
10+
global.window = document.defaultView
11+
global.navigator = global.window.navigator
12+
13+
jest.spyOn(console, 'error')
14+
global.console.error.mockImplementation(() => {})
1015
})
1116

12-
after(function() {
13-
delete console.error
17+
afterEach(() => {
18+
global.console.error.mockRestore()
1419
})
1520

1621
describe('ThemeProvider', () => {
@@ -24,7 +29,7 @@ describe('ThemeProvider', () => {
2429
themr: PropTypes.object.isRequired
2530
}
2631

27-
it('enforces a single child', () => {
32+
test('enforces a single child', () => {
2833
const theme = {}
2934

3035
// Ignore propTypes warnings
@@ -38,7 +43,7 @@ describe('ThemeProvider', () => {
3843
<div />
3944
</ThemeProvider>
4045
)
41-
).toNotThrow()
46+
).not.toThrow()
4247

4348
expect(() =>
4449
TestUtils.renderIntoDocument(
@@ -57,7 +62,7 @@ describe('ThemeProvider', () => {
5762
}
5863
})
5964

60-
it('should add the theme to the child context', () => {
65+
test('should add the theme to the child context', () => {
6166
const theme = {}
6267

6368
TestUtils.renderIntoDocument(
@@ -66,14 +71,13 @@ describe('ThemeProvider', () => {
6671
</ThemeProvider>
6772
)
6873

69-
const spy = expect.spyOn(console, 'error')
74+
const spy = jest.spyOn(console, 'error')
7075
const tree = TestUtils.renderIntoDocument(
7176
<ThemeProvider theme={theme}>
7277
<Child />
7378
</ThemeProvider>
7479
)
75-
spy.destroy()
76-
expect(spy.calls.length).toBe(0)
80+
expect(spy.mock.calls.length).toBe(0)
7781

7882
const child = TestUtils.findRenderedComponentWithType(tree, Child)
7983
expect(child.context.themr.theme).toBe(theme)

0 commit comments

Comments
 (0)