diff --git a/.github/workflows/e2e-appdir.yml b/.github/workflows/e2e-appdir.yml index 9e858f7c52..5f9f1d2ca1 100644 --- a/.github/workflows/e2e-appdir.yml +++ b/.github/workflows/e2e-appdir.yml @@ -35,7 +35,13 @@ jobs: test-file: ${{ fromJson(needs.setup.outputs['test-files']) }} steps: - uses: actions/checkout@v3 + - uses: actions/setup-node@v2 + with: + node-version: '16' + cache: 'npm' - run: npm install + - name: Install Netlify CLI + run: npm install -g netlify-cli - name: Run tests run: npx jest --reporters=jest-junit --reporters=default -c test/e2e/jest.config.all.js ${{ matrix.test-file }} env: diff --git a/.github/workflows/e2e-next.yml b/.github/workflows/e2e-next.yml index 0fdb8113aa..f664334bdf 100644 --- a/.github/workflows/e2e-next.yml +++ b/.github/workflows/e2e-next.yml @@ -35,7 +35,13 @@ jobs: test-file: ${{ fromJson(needs.setup.outputs['test-files']) }} steps: - uses: actions/checkout@v3 + - uses: actions/setup-node@v2 + with: + node-version: '16' + cache: 'npm' - run: npm install + - name: Install Netlify CLI + run: npm install -g netlify-cli - name: Run tests run: npx jest --reporters=jest-junit --reporters=default -c test/e2e/jest.config.all.js ${{ matrix.test-file }} env: diff --git a/test/e2e/next-test-lib/next-modes/base.ts b/test/e2e/next-test-lib/next-modes/base.ts index e9a64d2730..7fe5a77118 100644 --- a/test/e2e/next-test-lib/next-modes/base.ts +++ b/test/e2e/next-test-lib/next-modes/base.ts @@ -230,7 +230,7 @@ export class NextInstance { public async destroy(): Promise { if (this.isDestroyed) { - throw new Error(`next instance already destroyed`) + throw new Error(`Next.js base instance already destroyed`) } this.isDestroyed = true this.emit('destroy', []) diff --git a/test/e2e/next-test-lib/next-modes/next-deploy.ts b/test/e2e/next-test-lib/next-modes/next-deploy.ts index 7d4be7ad64..611737f443 100644 --- a/test/e2e/next-test-lib/next-modes/next-deploy.ts +++ b/test/e2e/next-test-lib/next-modes/next-deploy.ts @@ -4,9 +4,20 @@ import fs from 'fs-extra' import { platform } from 'os' import { NextInstance } from './base' +type DeployResponse = { + name: string + site_id: string + site_name: string + deploy_id: string + deploy_url: string + logs: string +} + export class NextDeployInstance extends NextInstance { private _cliOutput: string private _buildId: string + private _deployId: string + private _netlifySiteId: string public get buildId() { return this._buildId @@ -28,22 +39,23 @@ export class NextDeployInstance extends NextInstance { cwd: this.testDir, stdio: 'inherit', }) - // ensure Netlify CLI is installed + // Netlify CLI should be installed, but just making sure try { const res = await execa('ntl', ['--version']) console.log(`Using Netlify CLI version:`, res.stdout) } catch (_) { - console.log(`Installing Netlify CLI`) - await execa('npm', ['i', '-g', 'netlify-cli@latest'], { - stdio: 'inherit', - }) + throw new Error(`You need to have netlify-cli installed. + + You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"`) } - const NETLIFY_SITE_ID = process.env.NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395' console.log(`Deploys site for test: ${testName}`) + + this._netlifySiteId = process.env.NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395' + try { - const statRes = await execa('ntl', ['status', '--json'], { - env: { NETLIFY_SITE_ID, NODE_ENV: 'production' }, + await execa('ntl', ['status', '--json'], { + env: { NETLIFY_SITE_ID: this._netlifySiteId, NODE_ENV: 'production' }, }) } catch (err) { if (err.message.includes("You don't appear to be in a folder that is linked to a site")) { @@ -58,7 +70,7 @@ export class NextDeployInstance extends NextInstance { cwd: this.testDir, reject: false, env: { - NETLIFY_SITE_ID, + NETLIFY_SITE_ID: this._netlifySiteId, NODE_ENV: 'production', DISABLE_IPX: platform() === 'linux' ? undefined : '1', }, @@ -68,8 +80,9 @@ export class NextDeployInstance extends NextInstance { throw new Error(`Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})`) } try { - const data = JSON.parse(deployRes.stdout) + const data: DeployResponse = JSON.parse(deployRes.stdout) this._url = data.deploy_url + this._deployId = data.deploy_id console.log(`Deployed to ${this._url}`, data) this._parsedUrl = new URL(this._url) } catch (err) { @@ -89,6 +102,37 @@ export class NextDeployInstance extends NextInstance { // no-op as the deployment is created during setup() } + public async destroy(): Promise { + if (this.isDestroyed) { + throw new Error(`Next.js deploy instance already destroyed`) + } + + // During setup() the test site is deployed to Netlify + // Once testing is complete, we should delete the deploy again + + if (!process.env.NEXT_TEST_SKIP_CLEANUP) { + console.log(`Deleting project with deploy_id ${this._deployId}`) + + const deleteResponse = await execa('ntl', ['api', 'deleteDeploy', '--data', `{ "deploy_id": "${this._deployId}" }`]) + + if (deleteResponse.exitCode !== 0) { + throw new Error(`Failed to delete project ${deleteResponse.stdout} ${deleteResponse.stderr} (${deleteResponse.exitCode})`) + } + + console.log(`Successfully deleted project with deploy_id ${this._deployId}`) + } + + // Code below is copied from the base NextInstance class + + this.isDestroyed = true + this.emit('destroy', []) + + if (!process.env.NEXT_TEST_SKIP_CLEANUP) { + await fs.remove(this.testDir) + } + require('console').log(`destroyed next instance`) + } + public async patchFile(filename: string, content: string): Promise { throw new Error('patchFile is not available in deploy test mode') }