From 298f3ec1b67beeb05fc4a9e276292ab8395d315c Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Tue, 12 Jul 2022 11:46:48 +0200 Subject: [PATCH] Preprocess Cypress tests with Vite (#2956) --- cypress.config.ts | 66 ++++++++++++++++++++++ package-lock.json | 136 ++++++++++++++++++++++++++++++++++++++++++++-- package.json | 1 + 3 files changed, 197 insertions(+), 6 deletions(-) diff --git a/cypress.config.ts b/cypress.config.ts index ef36a173bc..562b4a4c7b 100644 --- a/cypress.config.ts +++ b/cypress.config.ts @@ -1,5 +1,7 @@ import { defineConfig } from "cypress"; import del from "del"; +import path from "node:path"; +import { build, InlineConfig } from "vite"; export default defineConfig({ projectId: "j4yhox", @@ -17,6 +19,7 @@ export default defineConfig({ }, e2e: { setupNodeEvents(on) { + on("file:preprocessor", vitePreprocessor); on("after:spec", async (spec, results) => { if (!results.video) { return; @@ -38,3 +41,66 @@ export default defineConfig({ specPattern: "cypress/e2e/**/*.{js,jsx,ts,tsx}", }, }); + +const cache: Record = {}; + +// See https://adamlynch.com/preprocess-cypress-tests-with-vite/ +async function vitePreprocessor(file: Cypress.FileObject) { + const { filePath, outputPath, shouldWatch } = file; + + if (cache[filePath]) { + return cache[filePath]; + } + + const filename = path.basename(outputPath); + const filenameWithoutExtension = path.basename( + outputPath, + path.extname(outputPath) + ); + + const viteConfig: InlineConfig = { + build: { + emptyOutDir: false, + minify: false, + outDir: path.dirname(outputPath), + sourcemap: true, + write: true, + }, + }; + + if (filename.endsWith(".html")) { + viteConfig.build!.rollupOptions = { + input: { + [filenameWithoutExtension]: filePath, + }, + }; + } else { + viteConfig.build!.lib = { + entry: filePath, + fileName: () => filename, + formats: ["es"], + name: filenameWithoutExtension, + }; + } + + if (shouldWatch) { + // @ts-ignore + viteConfig.build.watch = true; + } + + const watcher = await build(viteConfig); + + if ("on" in watcher) { + watcher.on("event", (event) => { + if (event.code === "END") { + file.emit("rerun"); + } + }); + file.on("close", () => { + delete cache[filePath]; + watcher.close(); + }); + } + + return (cache[filePath] = outputPath); +} diff --git a/package-lock.json b/package-lock.json index 566408b125..38bce82ce3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -77,6 +77,7 @@ "tar-fs": "^2.1.1", "ts-node": "^10.8.2", "typescript": "^4.7.4", + "vite": "^2.9.14", "vitest": "^0.18.0" }, "engines": { @@ -5865,6 +5866,16 @@ "node": ">=8" } }, + "node_modules/bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "optional": true, + "peer": true, + "dependencies": { + "file-uri-to-path": "1.0.0" + } + }, "node_modules/bl": { "version": "1.2.3", "dev": true, @@ -9554,6 +9565,13 @@ "node": ">=4" } }, + "node_modules/file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "optional": true, + "peer": true + }, "node_modules/fill-range": { "version": "7.0.1", "devOptional": true, @@ -9770,6 +9788,20 @@ "version": "1.0.0", "license": "ISC" }, + "node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/function-bind": { "version": "1.1.1", "dev": true, @@ -12976,6 +13008,13 @@ "dev": true, "license": "MIT" }, + "node_modules/nan": { + "version": "2.16.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.16.0.tgz", + "integrity": "sha512-UdAqHyFngu7TfQKsCBgAA6pWDkT8MAO7d0jyOecVhN5354xbLqdn8mV9Tat9gepAupm0bt2DbeaSC8vS52MuFA==", + "optional": true, + "peer": true + }, "node_modules/nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", @@ -15746,6 +15785,21 @@ "@rollup/plugin-inject": "^4.0.0" } }, + "node_modules/rollup/node_modules/fsevents": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz", + "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==", + "deprecated": "\"Please update to latest v2.3 or v2.2\"", + "dev": true, + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/rsvp": { "version": "4.8.5", "dev": true, @@ -18376,9 +18430,9 @@ "license": "MIT" }, "node_modules/vite": { - "version": "2.9.13", - "resolved": "https://registry.npmjs.org/vite/-/vite-2.9.13.tgz", - "integrity": "sha512-AsOBAaT0AD7Mhe8DuK+/kE4aWYFMx/i0ZNi98hJclxb4e0OhQcZYUrvLjIaQ8e59Ui7txcvKMiJC1yftqpQoDw==", + "version": "2.9.14", + "resolved": "https://registry.npmjs.org/vite/-/vite-2.9.14.tgz", + "integrity": "sha512-P/UCjSpSMcE54r4mPak55hWAZPlyfS369svib/gpmz8/01L822lMPOJ/RYW6tLCe1RPvMvOsJ17erf55bKp4Hw==", "dev": true, "dependencies": { "esbuild": "^0.14.27", @@ -18689,6 +18743,25 @@ "node": ">=0.10.0" } }, + "node_modules/watchpack-chokidar2/node_modules/fsevents": { + "version": "1.2.13", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", + "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", + "deprecated": "fsevents 1 will break on node v14+ and could be using insecure binaries. Upgrade to fsevents 2.", + "hasInstallScript": true, + "optional": true, + "os": [ + "darwin" + ], + "peer": true, + "dependencies": { + "bindings": "^1.5.0", + "nan": "^2.12.1" + }, + "engines": { + "node": ">= 4.0" + } + }, "node_modules/watchpack-chokidar2/node_modules/glob-parent": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-3.1.0.tgz", @@ -23617,6 +23690,16 @@ "version": "2.2.0", "devOptional": true }, + "bindings": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", + "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", + "optional": true, + "peer": true, + "requires": { + "file-uri-to-path": "1.0.0" + } + }, "bl": { "version": "1.2.3", "dev": true, @@ -26169,6 +26252,13 @@ "version": "5.2.0", "dev": true }, + "file-uri-to-path": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", + "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", + "optional": true, + "peer": true + }, "fill-range": { "version": "7.0.1", "devOptional": true, @@ -26311,6 +26401,13 @@ "fs.realpath": { "version": "1.0.0" }, + "fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "optional": true + }, "function-bind": { "version": "1.1.1", "dev": true @@ -28429,6 +28526,13 @@ "version": "2.1.2", "dev": true }, + "nan": { + "version": "2.16.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.16.0.tgz", + "integrity": "sha512-UdAqHyFngu7TfQKsCBgAA6pWDkT8MAO7d0jyOecVhN5354xbLqdn8mV9Tat9gepAupm0bt2DbeaSC8vS52MuFA==", + "optional": true, + "peer": true + }, "nanoid": { "version": "3.3.4", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.4.tgz", @@ -30270,6 +30374,15 @@ "dev": true, "requires": { "fsevents": "~2.1.2" + }, + "dependencies": { + "fsevents": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.3.tgz", + "integrity": "sha512-Auw9a4AxqWpa9GUfj370BMPzzyncfBABW8Mab7BGWBYDj4Isgq+cDKtx0i6u9jcX9pQDnswsaaOTgTmA5pEjuQ==", + "dev": true, + "optional": true + } } }, "rollup-plugin-polyfill-node": { @@ -32157,9 +32270,9 @@ } }, "vite": { - "version": "2.9.13", - "resolved": "https://registry.npmjs.org/vite/-/vite-2.9.13.tgz", - "integrity": "sha512-AsOBAaT0AD7Mhe8DuK+/kE4aWYFMx/i0ZNi98hJclxb4e0OhQcZYUrvLjIaQ8e59Ui7txcvKMiJC1yftqpQoDw==", + "version": "2.9.14", + "resolved": "https://registry.npmjs.org/vite/-/vite-2.9.14.tgz", + "integrity": "sha512-P/UCjSpSMcE54r4mPak55hWAZPlyfS369svib/gpmz8/01L822lMPOJ/RYW6tLCe1RPvMvOsJ17erf55bKp4Hw==", "dev": true, "requires": { "esbuild": "^0.14.27", @@ -32374,6 +32487,17 @@ } } }, + "fsevents": { + "version": "1.2.13", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", + "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", + "optional": true, + "peer": true, + "requires": { + "bindings": "^1.5.0", + "nan": "^2.12.1" + } + }, "glob-parent": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-3.1.0.tgz", diff --git a/package.json b/package.json index c370e1abb3..182a45d0ee 100644 --- a/package.json +++ b/package.json @@ -96,6 +96,7 @@ "tar-fs": "^2.1.1", "ts-node": "^10.8.2", "typescript": "^4.7.4", + "vite": "^2.9.14", "vitest": "^0.18.0" }, "lint-staged": {