-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
51 lines (41 loc) · 1.21 KB
/
index.js
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
41
42
43
44
45
46
47
48
49
50
51
const nock = require('nock')
const http = require('http')
const next = require('next')
// start the Next.js server when Cypress starts
module.exports = async (on, config) => {
const app = next({ dev: true })
const handleNextRequests = app.getRequestHandler()
await app.prepare()
const customServer = new http.Server(async (req, res) => {
return handleNextRequests(req, res)
})
await new Promise((resolve, reject) => {
customServer.listen(3000, (err) => {
if (err) {
return reject(err)
}
console.log('> Ready on http://localhost:3000')
resolve()
})
})
// register handlers for cy.task command
// https://on.cypress.io/task
on('task', {
clearNock() {
nock.restore()
nock.cleanAll()
return null
},
async nock({ hostname, method, path, statusCode, body }) {
nock.activate()
console.log('nock will: %s %s%s respond with %d %o',
method, hostname, path, statusCode, body)
// add one-time network stub like
// nock('https://icanhazdadjoke.com').get('/').reply(200, ...)
method = method.toLowerCase()
nock(hostname)[method](path).reply(statusCode, body)
return null
},
})
return config
}