Skip to content

Commit 019b6b1

Browse files
chore: delete e2e deploys in afterAll (#2004)
Co-authored-by: Nick Taylor <[email protected]>
1 parent 56c7828 commit 019b6b1

File tree

4 files changed

+67
-11
lines changed

4 files changed

+67
-11
lines changed

.github/workflows/e2e-appdir.yml

+6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ jobs:
3535
test-file: ${{ fromJson(needs.setup.outputs['test-files']) }}
3636
steps:
3737
- uses: actions/checkout@v3
38+
- uses: actions/setup-node@v2
39+
with:
40+
node-version: '16'
41+
cache: 'npm'
3842
- run: npm install
43+
- name: Install Netlify CLI
44+
run: npm install -g netlify-cli
3945
- name: Run tests
4046
run: npx jest --reporters=jest-junit --reporters=default -c test/e2e/jest.config.all.js ${{ matrix.test-file }}
4147
env:

.github/workflows/e2e-next.yml

+6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,13 @@ jobs:
3535
test-file: ${{ fromJson(needs.setup.outputs['test-files']) }}
3636
steps:
3737
- uses: actions/checkout@v3
38+
- uses: actions/setup-node@v2
39+
with:
40+
node-version: '16'
41+
cache: 'npm'
3842
- run: npm install
43+
- name: Install Netlify CLI
44+
run: npm install -g netlify-cli
3945
- name: Run tests
4046
run: npx jest --reporters=jest-junit --reporters=default -c test/e2e/jest.config.all.js ${{ matrix.test-file }}
4147
env:

test/e2e/next-test-lib/next-modes/base.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ export class NextInstance {
230230

231231
public async destroy(): Promise<void> {
232232
if (this.isDestroyed) {
233-
throw new Error(`next instance already destroyed`)
233+
throw new Error(`Next.js base instance already destroyed`)
234234
}
235235
this.isDestroyed = true
236236
this.emit('destroy', [])

test/e2e/next-test-lib/next-modes/next-deploy.ts

+54-10
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,20 @@ import fs from 'fs-extra'
44
import { platform } from 'os'
55
import { NextInstance } from './base'
66

7+
type DeployResponse = {
8+
name: string
9+
site_id: string
10+
site_name: string
11+
deploy_id: string
12+
deploy_url: string
13+
logs: string
14+
}
15+
716
export class NextDeployInstance extends NextInstance {
817
private _cliOutput: string
918
private _buildId: string
19+
private _deployId: string
20+
private _netlifySiteId: string
1021

1122
public get buildId() {
1223
return this._buildId
@@ -28,22 +39,23 @@ export class NextDeployInstance extends NextInstance {
2839
cwd: this.testDir,
2940
stdio: 'inherit',
3041
})
31-
// ensure Netlify CLI is installed
42+
// Netlify CLI should be installed, but just making sure
3243
try {
3344
const res = await execa('ntl', ['--version'])
3445
console.log(`Using Netlify CLI version:`, res.stdout)
3546
} catch (_) {
36-
console.log(`Installing Netlify CLI`)
37-
await execa('npm', ['i', '-g', 'netlify-cli@latest'], {
38-
stdio: 'inherit',
39-
})
47+
throw new Error(`You need to have netlify-cli installed.
48+
49+
You can do this by running: "npm install -g netlify-cli@latest" or "yarn global add netlify-cli@latest"`)
4050
}
4151

42-
const NETLIFY_SITE_ID = process.env.NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
4352
console.log(`Deploys site for test: ${testName}`)
53+
54+
this._netlifySiteId = process.env.NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
55+
4456
try {
45-
const statRes = await execa('ntl', ['status', '--json'], {
46-
env: { NETLIFY_SITE_ID, NODE_ENV: 'production' },
57+
await execa('ntl', ['status', '--json'], {
58+
env: { NETLIFY_SITE_ID: this._netlifySiteId, NODE_ENV: 'production' },
4759
})
4860
} catch (err) {
4961
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 {
5870
cwd: this.testDir,
5971
reject: false,
6072
env: {
61-
NETLIFY_SITE_ID,
73+
NETLIFY_SITE_ID: this._netlifySiteId,
6274
NODE_ENV: 'production',
6375
DISABLE_IPX: platform() === 'linux' ? undefined : '1',
6476
},
@@ -68,8 +80,9 @@ export class NextDeployInstance extends NextInstance {
6880
throw new Error(`Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})`)
6981
}
7082
try {
71-
const data = JSON.parse(deployRes.stdout)
83+
const data: DeployResponse = JSON.parse(deployRes.stdout)
7284
this._url = data.deploy_url
85+
this._deployId = data.deploy_id
7386
console.log(`Deployed to ${this._url}`, data)
7487
this._parsedUrl = new URL(this._url)
7588
} catch (err) {
@@ -89,6 +102,37 @@ export class NextDeployInstance extends NextInstance {
89102
// no-op as the deployment is created during setup()
90103
}
91104

105+
public async destroy(): Promise<void> {
106+
if (this.isDestroyed) {
107+
throw new Error(`Next.js deploy instance already destroyed`)
108+
}
109+
110+
// During setup() the test site is deployed to Netlify
111+
// Once testing is complete, we should delete the deploy again
112+
113+
if (!process.env.NEXT_TEST_SKIP_CLEANUP) {
114+
console.log(`Deleting project with deploy_id ${this._deployId}`)
115+
116+
const deleteResponse = await execa('ntl', ['api', 'deleteDeploy', '--data', `{ "deploy_id": "${this._deployId}" }`])
117+
118+
if (deleteResponse.exitCode !== 0) {
119+
throw new Error(`Failed to delete project ${deleteResponse.stdout} ${deleteResponse.stderr} (${deleteResponse.exitCode})`)
120+
}
121+
122+
console.log(`Successfully deleted project with deploy_id ${this._deployId}`)
123+
}
124+
125+
// Code below is copied from the base NextInstance class
126+
127+
this.isDestroyed = true
128+
this.emit('destroy', [])
129+
130+
if (!process.env.NEXT_TEST_SKIP_CLEANUP) {
131+
await fs.remove(this.testDir)
132+
}
133+
require('console').log(`destroyed next instance`)
134+
}
135+
92136
public async patchFile(filename: string, content: string): Promise<void> {
93137
throw new Error('patchFile is not available in deploy test mode')
94138
}

0 commit comments

Comments
 (0)