Skip to content
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

fix(gatsby): more robust adapter zero-conf handling (#38778) #38800

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions e2e-tests/adapters/gatsby-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ if (shouldUseDebugAdapter) {
configOverrides = {
adapter: debugAdapter(),
}
} else {
process.env.GATSBY_ADAPTERS_MANIFEST = /* javascript */ `
module.exports = [
{
name: 'Netlify',
module: 'gatsby-adapter-netlify',
test: () => !!process.env.NETLIFY || !!process.env.NETLIFY_LOCAL,
versions: [
{
gatsbyVersion: '*',
moduleVersion: '*',
}
],
}
]
`
}

const config: GatsbyConfig = {
Expand Down
63 changes: 62 additions & 1 deletion packages/gatsby/src/utils/__tests__/get-latest-gatsby-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,42 @@ describe(`default behavior: has network connectivity`, () => {

describe(`getLatestAdapters`, () => {
beforeEach(() => {
delete process.env.GATSBY_ADAPTERS_MANIFEST
})
it(`loads .js modules (prefers github)`, async () => {
axios.get.mockResolvedValueOnce({ data: latestAdaptersMarker })
const data = await getLatestAdapters()

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining(`raw.githubusercontent.com`),
expect.any(Object)
)

expect(axios.get).not.toHaveBeenCalledWith(
expect.stringContaining(`unpkg.com`),
expect.any(Object)
)

expect(fs.writeFile).toHaveBeenCalledWith(
expect.stringContaining(`latest-adapters.js`),
latestAdaptersMarker,
expect.any(String)
)

expect(data).toEqual(mockAdaptersManifest)
})

it(`loads .js modules`, async () => {
it(`loads .js modules (fallbacks to unkpg of github fails)`, async () => {
axios.get.mockRejectedValueOnce(new Error(`does not matter`))
axios.get.mockResolvedValueOnce({ data: latestAdaptersMarker })

const data = await getLatestAdapters()

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining(`raw.githubusercontent.com`),
expect.any(Object)
)

expect(axios.get).toHaveBeenCalledWith(
expect.stringContaining(`unpkg.com`),
expect.any(Object)
Expand All @@ -128,6 +158,37 @@ describe(`default behavior: has network connectivity`, () => {

expect(data).toEqual(mockAdaptersManifest)
})

it(`uses GATSBY_ADAPTERS_MANIFEST env var if set`, async () => {
process.env.GATSBY_ADAPTERS_MANIFEST = `custom_manifest`

axios.get.mockRejectedValueOnce(
new Error(`does not matter and should't be called`)
)
axios.get.mockRejectedValueOnce(
new Error(`does not matter and should't be called`)
)

const data = await getLatestAdapters()

expect(axios.get).not.toHaveBeenCalledWith(
expect.stringContaining(`raw.githubusercontent.com`),
expect.any(Object)
)

expect(axios.get).not.toHaveBeenCalledWith(
expect.stringContaining(`unpkg.com`),
expect.any(Object)
)

expect(fs.writeFile).toHaveBeenCalledWith(
expect.stringContaining(`latest-adapters.js`),
process.env.GATSBY_ADAPTERS_MANIFEST,
expect.any(String)
)

expect(data).toEqual(mockAdaptersManifest)
})
})
})

Expand Down
Loading