-
Notifications
You must be signed in to change notification settings - Fork 0
/
cypress.config.js
74 lines (59 loc) · 1.95 KB
/
cypress.config.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const http = require('node:http');
const { defineConfig } = require('cypress');
const next = require('next');
const { loadEnvConfig } = require('@next/env');
// Load environment variables the same way Next.js does.
loadEnvConfig(__dirname);
const testType = process.env.TEST_TYPE ?? 'integration';
const startNextServer = async () => {
const app = next({ dev: true });
const handleNextRequests = app.getRequestHandler();
await app.prepare();
console.log('> Next.js server started');
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('> Listening on http://localhost:3000');
resolve();
});
});
};
const integrationConfig = {
baseUrl: 'http://localhost:3000',
specPattern: 'cypress/integration/**/*.cy.{js,jsx,ts,tsx}',
setupNodeEvents: async (on) => {
const { rest } = require('msw');
const { setupServer } = require('msw/node');
const mockServer = setupServer();
await startNextServer();
on('task', {
mswRequest({ method, path, statusCode, body }) {
console.log(`mswRequest - Mocking route - [${method}] ${path}`);
const mswMethodName = method.toLowerCase();
mockServer.listen();
mockServer.use(
rest[mswMethodName](path, (req, res, ctx) => {
return res(ctx.status(statusCode), ctx.json(body));
})
);
return null;
},
mswClear() {
console.log('mswClear - Restoring handlers.');
mockServer.restoreHandlers();
return null;
},
});
},
};
const endToEndConfig = {
baseUrl: 'https://nextjs-pokedex-lemon.vercel.app/',
specPattern: 'cypress/e2e/**/*.cy.{js,jsx,ts,tsx}',
trashAssetsBeforeRuns: true,
};
module.exports = defineConfig({
e2e: testType === 'integration' ? integrationConfig : endToEndConfig,
});