Skip to content

chore: delete e2e deploys in afterAll #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 6, 2023
Merged
6 changes: 6 additions & 0 deletions .github/workflows/e2e-appdir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/e2e-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/next-test-lib/next-modes/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ export class NextInstance {

public async destroy(): Promise<void> {
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', [])
Expand Down
64 changes: 54 additions & 10 deletions test/e2e/next-test-lib/next-modes/next-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the default site ID '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Who knows... 😆 That code was already there, I only moved it


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")) {
Expand All @@ -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',
},
Expand All @@ -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) {
Expand All @@ -89,6 +102,37 @@ export class NextDeployInstance extends NextInstance {
// no-op as the deployment is created during setup()
}

public async destroy(): Promise<void> {
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<void> {
throw new Error('patchFile is not available in deploy test mode')
}
Expand Down