-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
40 lines (32 loc) · 960 Bytes
/
jest.setup.js
File metadata and controls
40 lines (32 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import React from 'react'
const Enzyme = require('enzyme')
const EnzymeAdapter = require('enzyme-adapter-react-16')
React.useLayoutEffect = React.useEffect
Enzyme.configure({ adapter: new EnzymeAdapter() })
/*
Util methods to start/close an express server during tests
*/
const serverMap = {}
global.startServer = (server) =>
new Promise((resolve, reject) => {
const listener = server.listen(0, 'localhost', (err) => {
if (err) {
return reject(err)
}
const address = listener.address()
const url = `http://${address.address}:${address.port}`
if (url in serverMap) {
return reject(new Error(`${url} is already mapped!`))
} else {
serverMap[url] = listener
}
return resolve(url)
})
})
global.closeServers = (urls) => {
Object.entries(serverMap).forEach(([url, listener]) => {
if ((!urls || urls.includes(url)) && listener) {
listener.close()
}
})
}