-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcypress.config.mjs
More file actions
115 lines (105 loc) · 3.61 KB
/
cypress.config.mjs
File metadata and controls
115 lines (105 loc) · 3.61 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { defineConfig } from "cypress";
import dotenv from "dotenv";
import fs from "fs";
import path from "path";
import JSZip from "jszip";
dotenv.config();
export default defineConfig({
e2e: {
chromeWebSecurity: false,
defaultCommandTimeout: 10000,
video: false,
defaultBrowser: "chrome",
testIsolation: true,
downloadsFolder: "cypress/downloads",
setupNodeEvents(on, config) {
const projectRoot = config.projectRoot ?? process.cwd();
const downloadsFolder = path.resolve(
projectRoot,
config.downloadsFolder || "cypress/downloads",
);
on("task", {
log(message) {
console.log(message);
return null;
},
resetDownloads() {
if (fs.existsSync(downloadsFolder)) {
const files = fs.readdirSync(downloadsFolder);
for (const file of files) {
fs.unlinkSync(path.join(downloadsFolder, file));
}
} else {
fs.mkdirSync(downloadsFolder, { recursive: true });
}
return null;
},
async getNewestDownload(extension) {
if (!extension || !extension.startsWith(".")) {
throw new Error(
"getNewestDownload requires a file extension starting with . (e.g. .sb3)",
);
}
const ext = extension;
const pollMs = 100;
const timeoutMs = 1500;
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (!fs.existsSync(downloadsFolder)) {
await new Promise((r) => setTimeout(r, pollMs));
continue;
}
const files = fs.readdirSync(downloadsFolder);
const matching = files
.filter((f) => f.endsWith(ext))
.map((f) => ({
name: f,
path: path.join(downloadsFolder, f),
mtime: fs.statSync(path.join(downloadsFolder, f)).mtimeMs,
}))
.sort((a, b) => b.mtime - a.mtime);
if (matching.length > 0) {
return matching[0].path;
}
await new Promise((r) => setTimeout(r, pollMs));
}
if (!fs.existsSync(downloadsFolder)) {
throw new Error("Downloads folder not found");
}
throw new Error(`No ${ext} file found in downloads folder`);
},
async readSb3(filePath) {
const buf = fs.readFileSync(filePath);
const zip = await JSZip.loadAsync(buf);
const fileNames = Object.keys(zip.files);
const projectJsonFile = zip.file("project.json");
if (!projectJsonFile) {
throw new Error("Invalid .sb3 file: missing project.json");
}
const projectJson = JSON.parse(await projectJsonFile.async("string"));
return { fileNames, projectJson };
},
});
on("before:browser:launch", (browser = {}, launchOptions) => {
if (browser.name === "chrome") {
console.log("Applying Chrome launch options");
launchOptions.args.push("--enable-features=SharedArrayBuffer");
launchOptions.args.push("--disable-site-isolation-trials");
launchOptions.args.push("--enable-unsafe-swiftshader");
launchOptions.preferences = {
...launchOptions.preferences,
"download.default_directory": downloadsFolder,
};
}
return launchOptions;
});
},
retries: {
runMode: 3,
openMode: 0,
},
},
env: {
REACT_APP_API_ENDPOINT: process.env.REACT_APP_API_ENDPOINT,
},
});