2023-10-23 23:33:39 +02:00
|
|
|
import path from "node:path"
|
2022-08-23 21:51:20 +02:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { defineConfig } from "cypress"
|
|
|
|
import { getLocal } from "mockttp"
|
|
|
|
import type { Mockttp } from "mockttp"
|
2022-08-23 21:51:20 +02:00
|
|
|
|
2023-10-23 23:33:39 +02:00
|
|
|
import { API_DEFAULT_PORT } from "./tools/api"
|
|
|
|
import type { Handlers } from "./cypress/fixtures/handler"
|
2022-08-23 21:51:20 +02:00
|
|
|
|
|
|
|
const UPLOADS_FIXTURES_DIRECTORY = path.join(
|
|
|
|
process.cwd(),
|
2023-10-23 23:33:39 +02:00
|
|
|
"cypress",
|
|
|
|
"fixtures",
|
|
|
|
"uploads",
|
2022-08-23 21:51:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
let server: Mockttp | null = null
|
|
|
|
|
|
|
|
export default defineConfig({
|
|
|
|
fixturesFolder: false,
|
|
|
|
video: false,
|
|
|
|
screenshotOnRunFailure: false,
|
|
|
|
e2e: {
|
2023-10-23 23:33:39 +02:00
|
|
|
baseUrl: "http://127.0.0.1:3000",
|
2022-08-23 21:51:20 +02:00
|
|
|
supportFile: false,
|
|
|
|
setupNodeEvents(on, config) {
|
2023-10-23 23:33:39 +02:00
|
|
|
on("task", {
|
2022-08-23 21:51:20 +02:00
|
|
|
async startMockServer(handlers: Handlers): Promise<null> {
|
2023-04-03 00:11:19 +02:00
|
|
|
server = getLocal({ cors: true })
|
2022-08-23 21:51:20 +02:00
|
|
|
await server.start(API_DEFAULT_PORT)
|
|
|
|
for (const handler of handlers) {
|
2023-07-22 16:34:23 +02:00
|
|
|
const { isFile = false, statusCode, body } = handler.response
|
2023-04-03 00:11:19 +02:00
|
|
|
let requestBuilder = server.forGet(handler.url)
|
|
|
|
switch (handler.method) {
|
2023-10-23 23:33:39 +02:00
|
|
|
case "GET":
|
2023-04-03 00:11:19 +02:00
|
|
|
requestBuilder = server.forGet(handler.url)
|
|
|
|
break
|
2023-10-23 23:33:39 +02:00
|
|
|
case "POST":
|
2023-04-03 00:11:19 +02:00
|
|
|
requestBuilder = server.forPost(handler.url)
|
|
|
|
break
|
2023-10-23 23:33:39 +02:00
|
|
|
case "PUT":
|
2023-04-03 00:11:19 +02:00
|
|
|
requestBuilder = server.forPut(handler.url)
|
|
|
|
break
|
2023-10-23 23:33:39 +02:00
|
|
|
case "DELETE":
|
2023-04-03 00:11:19 +02:00
|
|
|
requestBuilder = server.forDelete(handler.url)
|
|
|
|
break
|
|
|
|
}
|
2022-08-23 21:51:20 +02:00
|
|
|
if (isFile) {
|
2023-04-03 00:11:19 +02:00
|
|
|
await requestBuilder.thenFromFile(
|
2023-07-22 16:34:23 +02:00
|
|
|
statusCode,
|
2023-10-23 23:33:39 +02:00
|
|
|
path.join(UPLOADS_FIXTURES_DIRECTORY, ...body),
|
2022-08-23 21:51:20 +02:00
|
|
|
)
|
|
|
|
} else {
|
2023-07-22 16:34:23 +02:00
|
|
|
await requestBuilder.thenJson(statusCode, body)
|
2022-08-23 21:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-03 00:11:19 +02:00
|
|
|
|
2022-08-23 21:51:20 +02:00
|
|
|
return null
|
|
|
|
},
|
|
|
|
|
|
|
|
async stopMockServer(): Promise<null> {
|
|
|
|
if (server != null) {
|
|
|
|
await server.stop()
|
|
|
|
server = null
|
|
|
|
}
|
|
|
|
return null
|
2023-10-23 23:33:39 +02:00
|
|
|
},
|
2022-08-23 21:51:20 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return config
|
2023-10-23 23:33:39 +02:00
|
|
|
},
|
2022-08-23 21:51:20 +02:00
|
|
|
},
|
|
|
|
component: {
|
|
|
|
devServer: {
|
2023-10-23 23:33:39 +02:00
|
|
|
framework: "next",
|
|
|
|
bundler: "webpack",
|
|
|
|
},
|
|
|
|
},
|
2022-08-23 21:51:20 +02:00
|
|
|
})
|