Skip to content

Commit ff314f9

Browse files
committed
chore: delete test deploys in afterAll call
1 parent 24eaaab commit ff314f9

File tree

1 file changed

+50
-5
lines changed

1 file changed

+50
-5
lines changed

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

Lines changed: 50 additions & 5 deletions
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 DeployRes = {
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
@@ -39,11 +50,13 @@ export class NextDeployInstance extends NextInstance {
3950
})
4051
}
4152

42-
const NETLIFY_SITE_ID = process.env.NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
4353
console.log(`Deploys site for test: ${testName}`)
54+
55+
this._netlifySiteId = process.env.NETLIFY_SITE_ID || '1d5a5c76-d445-4ae5-b694-b0d3f2e2c395'
56+
4457
try {
45-
const statRes = await execa('ntl', ['status', '--json'], {
46-
env: { NETLIFY_SITE_ID, NODE_ENV: 'production' },
58+
await execa('ntl', ['status', '--json'], {
59+
env: { NETLIFY_SITE_ID: this._netlifySiteId, NODE_ENV: 'production' },
4760
})
4861
} catch (err) {
4962
if (err.message.includes("You don't appear to be in a folder that is linked to a site")) {
@@ -58,7 +71,7 @@ export class NextDeployInstance extends NextInstance {
5871
cwd: this.testDir,
5972
reject: false,
6073
env: {
61-
NETLIFY_SITE_ID,
74+
NETLIFY_SITE_ID: this._netlifySiteId,
6275
NODE_ENV: 'production',
6376
DISABLE_IPX: platform() === 'linux' ? undefined : '1',
6477
},
@@ -68,8 +81,9 @@ export class NextDeployInstance extends NextInstance {
6881
throw new Error(`Failed to deploy project ${deployRes.stdout} ${deployRes.stderr} (${deployRes.exitCode})`)
6982
}
7083
try {
71-
const data = JSON.parse(deployRes.stdout)
84+
const data: DeployRes = JSON.parse(deployRes.stdout)
7285
this._url = data.deploy_url
86+
this._deployId = data.deploy_id
7387
console.log(`Deployed to ${this._url}`, data)
7488
this._parsedUrl = new URL(this._url)
7589
} catch (err) {
@@ -89,6 +103,37 @@ export class NextDeployInstance extends NextInstance {
89103
// no-op as the deployment is created during setup()
90104
}
91105

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

0 commit comments

Comments
 (0)