From 8433df5f61470ef74a94fb5f4d619fa840f1da29 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 25 Jul 2024 21:53:12 +0200 Subject: [PATCH] test: pack and install runtime when running Next.js e2e tests (#2554) --- tests/netlify-deploy.ts | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/tests/netlify-deploy.ts b/tests/netlify-deploy.ts index 19d20d59c4..2c3aac9942 100644 --- a/tests/netlify-deploy.ts +++ b/tests/netlify-deploy.ts @@ -2,6 +2,7 @@ import execa from 'execa' import fs from 'fs-extra' import { Span } from 'next/src/trace' +import { tmpdir } from 'node:os' import path from 'path' import { NextInstance } from './base' @@ -14,6 +15,31 @@ type NetlifyDeployResponse = { logs: string } +async function packNextRuntimeImpl() { + const runtimePackDir = await fs.mkdtemp(path.join(tmpdir(), 'next-runtime-pack')) + + const { stdout } = await execa( + 'npm', + ['pack', '--json', '--ignore-scripts', `--pack-destination=${runtimePackDir}`], + { cwd: process.env.RUNTIME_DIR || `${process.cwd()}/../next-runtime` }, + ) + const [{ filename, name }] = JSON.parse(stdout) + + return { + runtimePackageName: name, + runtimePackageTarballPath: path.join(runtimePackDir, filename), + } +} + +let packNextRuntimePromise: ReturnType | null = null +function packNextRuntime() { + if (!packNextRuntimePromise) { + packNextRuntimePromise = packNextRuntimeImpl() + } + + return packNextRuntimePromise +} + export class NextDeployInstance extends NextInstance { private _cliOutput: string private _buildId: string @@ -45,8 +71,10 @@ export class NextDeployInstance extends NextInstance { await fs.rename(nodeModules, nodeModulesBak) } + const { runtimePackageName, runtimePackageTarballPath } = await packNextRuntime() + // install dependencies - await execa('npm', ['i', '--legacy-peer-deps'], { + await execa('npm', ['i', runtimePackageTarballPath, '--legacy-peer-deps'], { cwd: this.testDir, stdio: 'inherit', }) @@ -68,10 +96,7 @@ export class NextDeployInstance extends NextInstance { publish = ".next" [[plugins]] - package = "${path.relative( - this.testDir, - process.env.RUNTIME_DIR || `${process.cwd()}/../next-runtime`, - )}" + package = "${runtimePackageName}" ` await fs.writeFile(path.join(this.testDir, 'netlify.toml'), toml)