Skip to content

Commit 4bf84f0

Browse files
gatsbybotpieh
andauthored
test(adapters-e2e): deploy to platform instead of ntl serve (#38643) (#38662)
* test(adapters-e2e): deploy to platform instead of ntl serve * test: clear browser cache for interception tests * test: delete deploy after the test * test: how about not caching things to begin with? * chore: update comment, only disable caching for webpack asset, restore previous assertions (cherry picked from commit 5bc0992) Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 67c8832 commit 4bf84f0

File tree

4 files changed

+85
-22
lines changed

4 files changed

+85
-22
lines changed

e2e-tests/adapters/cypress/configs/netlify.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import { defineConfig } from "cypress"
22

33
export default defineConfig({
44
e2e: {
5-
baseUrl: `http://localhost:8888`,
5+
baseUrl: process.env.DEPLOY_URL || `http://localhost:8888`,
66
// Netlify doesn't handle trailing slash behaviors really, so no use in testing it
7-
excludeSpecPattern: [`cypress/e2e/trailing-slash.cy.ts`,],
7+
excludeSpecPattern: [`cypress/e2e/trailing-slash.cy.ts`],
88
projectId: `4enh4m`,
99
videoUploadOnPasses: false,
1010
experimentalRunAllSpecs: true,
1111
retries: 2,
1212
},
13-
})
13+
})
+22-17
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,49 @@
11
import { title } from "../../constants"
22

3-
describe('Basics', () => {
3+
describe("Basics", () => {
44
beforeEach(() => {
55
cy.intercept("/gatsby-icon.png").as("static-folder-image")
6-
cy.intercept("/static/astro-**.png").as("img-import")
6+
cy.intercept("/static/astro-**.png", req => {
7+
req.on("before:response", res => {
8+
// this generally should be permamently cached, but that cause problems with intercepting
9+
// see https://docs.cypress.io/api/commands/intercept#cyintercept-and-request-caching
10+
// so we disable caching for this response
11+
// tests for cache-control headers should be done elsewhere
712

8-
cy.visit('/').waitForRouteChange()
13+
res.headers["cache-control"] = "no-store"
14+
})
15+
}).as("img-import")
16+
17+
cy.visit("/").waitForRouteChange()
918
})
1019

11-
it('should display index page', () => {
12-
cy.get('h1').should('have.text', title)
13-
cy.title().should('eq', 'Adapters E2E')
20+
it("should display index page", () => {
21+
cy.get("h1").should("have.text", title)
22+
cy.title().should("eq", "Adapters E2E")
1423
})
1524
// If this test fails, run "gatsby build" and retry
1625
it('should serve assets from "static" folder', () => {
1726
cy.wait("@static-folder-image").should(req => {
1827
expect(req.response.statusCode).to.be.gte(200).and.lt(400)
1928
})
2029

21-
cy.get('[alt="Gatsby Monogram Logo"]').should('be.visible')
30+
cy.get('[alt="Gatsby Monogram Logo"]').should("be.visible")
2231
})
23-
it('should serve assets imported through webpack', () => {
32+
it("should serve assets imported through webpack", () => {
2433
cy.wait("@img-import").should(req => {
2534
expect(req.response.statusCode).to.be.gte(200).and.lt(400)
2635
})
2736

28-
cy.get('[alt="Gatsby Astronaut"]').should('be.visible')
37+
cy.get('[alt="Gatsby Astronaut"]').should("be.visible")
2938
})
3039
it(`should show custom 404 page on invalid URL`, () => {
3140
cy.visit(`/non-existent-page`, {
3241
failOnStatusCode: false,
3342
})
3443

35-
cy.get('h1').should('have.text', 'Page not found')
44+
cy.get("h1").should("have.text", "Page not found")
3645
})
37-
it('should apply CSS', () => {
38-
cy.get(`h1`).should(
39-
`have.css`,
40-
`color`,
41-
`rgb(21, 21, 22)`
42-
)
46+
it("should apply CSS", () => {
47+
cy.get(`h1`).should(`have.css`, `color`, `rgb(21, 21, 22)`)
4348
})
44-
})
49+
})

e2e-tests/adapters/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
"test:template": "cross-env-shell CYPRESS_GROUP_NAME=$ADAPTER TRAILING_SLASH=$TRAILING_SLASH node ../../scripts/cypress-run-with-conditional-record-flag.js --browser chrome --e2e --config-file \"cypress/configs/$ADAPTER.ts\" --env TRAILING_SLASH=$TRAILING_SLASH",
1717
"test:template:debug": "cross-env-shell CYPRESS_GROUP_NAME=$ADAPTER TRAILING_SLASH=$TRAILING_SLASH npm run cy:open -- --config-file \"cypress/configs/$ADAPTER.ts\" --env TRAILING_SLASH=$TRAILING_SLASH",
1818
"test:debug": "npm-run-all -s build:debug ssat:debug",
19-
"test:netlify": "start-server-and-test 'cross-env TRAILING_SLASH=always BROWSER=none ntl serve --port 8888' http://localhost:8888 'cross-env ADAPTER=netlify TRAILING_SLASH=always npm run test:template'",
20-
"test:netlify:debug": "start-server-and-test 'cross-env TRAILING_SLASH=always BROWSER=none ntl serve --port 8888' http://localhost:8888 'cross-env ADAPTER=netlify TRAILING_SLASH=always npm run test:template:debug'",
19+
"test:netlify": "cross-env TRAILING_SLASH=always node scripts/deploy-and-run/netlify.mjs test:template",
20+
"test:netlify:debug": "cross-env TRAILING_SLASH=always node scripts/deploy-and-run/netlify.mjs test:template:debug",
2121
"test": "npm-run-all -c -s test:netlify"
2222
},
2323
"dependencies": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// @ts-check
2+
3+
import { execa } from "execa"
4+
5+
process.env.NETLIFY_SITE_ID = process.env.E2E_ADAPTERS_NETLIFY_SITE_ID
6+
process.env.ADAPTER = "netlify"
7+
8+
const deployTitle = process.env.CIRCLE_SHA1 || "N/A"
9+
10+
const npmScriptToRun = process.argv[2] || "test:netlify"
11+
12+
const deployResults = await execa(
13+
"ntl",
14+
["deploy", "--build", "--json", "--message", deployTitle],
15+
{
16+
reject: false,
17+
}
18+
)
19+
20+
if (deployResults.exitCode !== 0) {
21+
if (deployResults.stdout) {
22+
console.log(deployResults.stdout)
23+
}
24+
if (deployResults.stderr) {
25+
console.error(deployResults.stderr)
26+
}
27+
28+
process.exit(deployResults.exitCode)
29+
}
30+
31+
const deployInfo = JSON.parse(deployResults.stdout)
32+
33+
process.env.DEPLOY_URL = deployInfo.deploy_url
34+
35+
try {
36+
await execa(`npm`, [`run`, npmScriptToRun], { stdio: `inherit` })
37+
} finally {
38+
if (!process.env.GATSBY_TEST_SKIP_CLEANUP) {
39+
console.log(`Deleting project with deploy_id ${deployInfo.deploy_id}`)
40+
41+
const deleteResponse = await execa("ntl", [
42+
"api",
43+
"deleteDeploy",
44+
"--data",
45+
`{ "deploy_id": "${deployInfo.deploy_id}" }`,
46+
])
47+
48+
if (deleteResponse.exitCode !== 0) {
49+
throw new Error(
50+
`Failed to delete project ${deleteResponse.stdout} ${deleteResponse.stderr} (${deleteResponse.exitCode})`
51+
)
52+
}
53+
54+
console.log(
55+
`Successfully deleted project with deploy_id ${deployInfo.deploy_id}`
56+
)
57+
}
58+
}

0 commit comments

Comments
 (0)