Skip to content

Commit bdb404a

Browse files
committed
Setup enzyme for JSX tests
Signed-off-by: Marcos Loureiro <mloureiro.dev@gmail.com>
1 parent 2feb6ba commit bdb404a

File tree

5 files changed

+585
-28
lines changed

5 files changed

+585
-28
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@
33

44
# TypeScript
55
/dist
6+
7+
# Jest
8+
.jest/
9+
/coverage/

jest.config.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
{
2+
"preset": "react-native",
23
"transform": {
3-
"^.+\\.(t|j)sx?$": "ts-jest"
4+
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
5+
"^.+\\.tsx?$": "ts-jest"
46
},
57
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
6-
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
8+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
9+
"modulePaths": ["<rootDir>"],
10+
"setupFiles": ["<rootDir>/jest.setup.js"]
711
}

jest.setup.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { configure } from 'enzyme'
2+
import Adapter from 'enzyme-adapter-react-16'
3+
import { JSDOM } from 'jsdom'
4+
import 'react-native-mock-render/mock'
5+
6+
function copyProps(src, target) {
7+
Object.defineProperties(target, {
8+
...Object.getOwnPropertyDescriptors(src),
9+
...Object.getOwnPropertyDescriptors(target),
10+
})
11+
}
12+
13+
const jsdom = new JSDOM()
14+
const { window } = jsdom
15+
16+
global.window = window
17+
global.document = window.document
18+
global.navigator = {
19+
userAgent: 'node.js',
20+
}
21+
copyProps(window, global)
22+
23+
configure({ adapter: new Adapter() })
24+
25+
// react-native-jest-mock throws too many of warnings due to
26+
// unbridged new API methods/signature.
27+
// In order to reduce irrelevant spam, we mock these errors.
28+
console.error = message => message

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@
2929
"test:coverage": "yarn test --coverage --coverageReporters=text-lcov | coveralls"
3030
},
3131
"devDependencies": {
32+
"@types/enzyme": "^3.1.15",
33+
"@types/enzyme-adapter-react-16": "^1.0.3",
3234
"@types/jest": "^23.3.12",
35+
"@types/jsdom": "^12.2.1",
3336
"@types/react": "^16.7.18",
3437
"@types/react-native": "^0.57.27",
3538
"coveralls": "^3.0.2",
39+
"enzyme": "^3.8.0",
40+
"enzyme-adapter-react-16": "^1.7.1",
3641
"husky": "^1.3.1",
3742
"jest": "^23.6.0",
43+
"jest-fetch-mock": "^2.1.0",
44+
"jsdom": "^13.1.0",
3845
"lint-staged": "^8.1.0",
3946
"prettier": "^1.15.3",
4047
"react": "^16.7.0",
48+
"react-dom": "^16.7.0",
4149
"react-native": "^0.57.8",
50+
"react-native-mock-render": "^0.1.2",
4251
"ts-jest": "^23.10.5",
4352
"tslint": "^5.12.1",
4453
"tslint-config-prettier": "^1.17.0",

0 commit comments

Comments
 (0)