Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby): Improve get-config-file error handling (#35776)
- Loading branch information
Showing
9 changed files
with
266 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/gatsby/src/bootstrap/__mocks__/get-config/compiled-dir/compiled/gatsby-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
siteMetadata: { | ||
title: `compiled`, | ||
siteUrl: `https://www.yourdomain.tld`, | ||
}, | ||
plugins: [], | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/gatsby/src/bootstrap/__mocks__/get-config/gatsby-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
siteMetadata: { | ||
title: `uncompiled`, | ||
siteUrl: `https://www.yourdomain.tld`, | ||
}, | ||
plugins: [], | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/gatsby/src/bootstrap/__mocks__/get-config/near-match-dir/gatsby-confi.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
siteMetadata: { | ||
title: `near-match`, | ||
siteUrl: `https://www.yourdomain.tld`, | ||
}, | ||
plugins: [], | ||
} |
7 changes: 7 additions & 0 deletions
7
packages/gatsby/src/bootstrap/__mocks__/get-config/src-dir/src/gatsby-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
siteMetadata: { | ||
title: `in-src`, | ||
siteUrl: `https://www.yourdomain.tld`, | ||
}, | ||
plugins: [], | ||
} |
11 changes: 11 additions & 0 deletions
11
packages/gatsby/src/bootstrap/__mocks__/get-config/ts-dir/gatsby-config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { GatsbyConfig } from "gatsby" | ||
|
||
const config: GatsbyConfig = { | ||
siteMetadata: { | ||
title: `ts`, | ||
siteUrl: `https://www.yourdomain.tld`, | ||
}, | ||
plugins: [], | ||
} | ||
|
||
export default config |
9 changes: 9 additions & 0 deletions
9
...ages/gatsby/src/bootstrap/__mocks__/get-config/user-require-dir/compiled/gatsby-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const something = require(`some-place-that-does-not-exist`) | ||
|
||
module.exports = { | ||
siteMetadata: { | ||
title: `user-require-error`, | ||
siteUrl: `https://www.yourdomain.tld`, | ||
}, | ||
plugins: [], | ||
} |
164 changes: 164 additions & 0 deletions
164
packages/gatsby/src/bootstrap/__tests__/get-config-file.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import path from "path" | ||
import { isNearMatch, getConfigFile } from "../get-config-file" | ||
import { testRequireError } from "../../utils/test-require-error" | ||
import reporter from "gatsby-cli/lib/reporter" | ||
|
||
jest.mock(`path`, () => { | ||
const actual = jest.requireActual(`path`) | ||
return { | ||
...actual, | ||
join: jest.fn((...arg) => actual.join(...arg)), | ||
} | ||
}) | ||
|
||
jest.mock(`../../utils/test-require-error`, () => { | ||
return { | ||
testRequireError: jest.fn(), | ||
} | ||
}) | ||
|
||
jest.mock(`../../utils/parcel/compile-gatsby-files`, () => { | ||
const actual = jest.requireActual(`../../utils/parcel/compile-gatsby-files`) | ||
return { | ||
...actual, | ||
COMPILED_CACHE_DIR: `compiled`, // .cache is git ignored | ||
} | ||
}) | ||
|
||
jest.mock(`gatsby-cli/lib/reporter`, () => { | ||
return { | ||
panic: jest.fn(), | ||
} | ||
}) | ||
|
||
const pathJoinMock = path.join as jest.MockedFunction<typeof path.join> | ||
|
||
const testRequireErrorMock = testRequireError as jest.MockedFunction< | ||
typeof testRequireError | ||
> | ||
|
||
const reporterPanicMock = reporter.panic as jest.MockedFunction< | ||
typeof reporter.panic | ||
> | ||
|
||
describe(`isNearMatch`, () => { | ||
it(`should NOT find a near match if file name is undefined`, () => { | ||
const nearMatchA = isNearMatch(undefined, `gatsby-config`, 1) | ||
expect(nearMatchA).toBeFalse() | ||
}) | ||
|
||
it(`should calculate near matches based on distance`, () => { | ||
const nearMatchA = isNearMatch(`gatsby-config`, `gatsby-conf`, 2) | ||
const nearMatchB = isNearMatch(`gatsby-config`, `gatsby-configur`, 2) | ||
expect(nearMatchA).toBeTrue() | ||
expect(nearMatchB).toBeTrue() | ||
}) | ||
}) | ||
|
||
// Separate config directories so cases can be tested separately | ||
const dir = path.resolve(__dirname, `../__mocks__/get-config`) | ||
const compiledDir = `${dir}/compiled-dir` | ||
const userRequireDir = `${dir}/user-require-dir` | ||
const tsDir = `${dir}/ts-dir` | ||
const nearMatchDir = `${dir}/near-match-dir` | ||
const srcDir = `${dir}/src-dir` | ||
|
||
describe(`getConfigFile`, () => { | ||
beforeEach(() => { | ||
reporterPanicMock.mockClear() | ||
}) | ||
|
||
it(`should get an uncompiled gatsby-config.js`, async () => { | ||
const { configModule, configFilePath } = await getConfigFile( | ||
dir, | ||
`gatsby-config` | ||
) | ||
expect(configFilePath).toBe(path.join(dir, `gatsby-config.js`)) | ||
expect(configModule.siteMetadata.title).toBe(`uncompiled`) | ||
}) | ||
|
||
it(`should get a compiled gatsby-config.js`, async () => { | ||
const { configModule, configFilePath } = await getConfigFile( | ||
compiledDir, | ||
`gatsby-config` | ||
) | ||
expect(configFilePath).toBe( | ||
path.join(compiledDir, `compiled`, `gatsby-config.js`) | ||
) | ||
expect(configModule.siteMetadata.title).toBe(`compiled`) | ||
}) | ||
|
||
it(`should handle user require errors found in compiled gatsby-config.js`, async () => { | ||
await getConfigFile(userRequireDir, `gatsby-config`) | ||
|
||
expect(reporterPanicMock).toBeCalledWith({ | ||
id: `11902`, | ||
error: expect.toBeObject(), | ||
context: { | ||
configName: `gatsby-config`, | ||
message: expect.toBeString(), | ||
}, | ||
}) | ||
}) | ||
|
||
it(`should handle non-require errors`, async () => { | ||
testRequireErrorMock.mockImplementationOnce(() => false) | ||
|
||
await getConfigFile(nearMatchDir, `gatsby-config`) | ||
|
||
expect(reporterPanicMock).toBeCalledWith({ | ||
id: `10123`, | ||
error: expect.toBeObject(), | ||
context: { | ||
configName: `gatsby-config`, | ||
message: expect.toBeString(), | ||
}, | ||
}) | ||
}) | ||
|
||
it(`should handle case where gatsby-config.ts exists but no compiled gatsby-config.js exists`, async () => { | ||
// Force outer and inner errors so we can hit the code path that checks if gatsby-config.ts exists | ||
pathJoinMock | ||
.mockImplementationOnce(() => `force-outer-error`) | ||
.mockImplementationOnce(() => `force-inner-error`) | ||
testRequireErrorMock.mockImplementationOnce(() => true) | ||
|
||
await getConfigFile(tsDir, `gatsby-config`) | ||
|
||
expect(reporterPanicMock).toBeCalledWith({ | ||
id: `10127`, | ||
error: expect.toBeObject(), | ||
context: { | ||
configName: `gatsby-config`, | ||
}, | ||
}) | ||
}) | ||
|
||
it(`should handle near matches`, async () => { | ||
testRequireErrorMock.mockImplementationOnce(() => true) | ||
|
||
await getConfigFile(nearMatchDir, `gatsby-config`) | ||
|
||
expect(reporterPanicMock).toBeCalledWith({ | ||
id: `10124`, | ||
error: expect.toBeObject(), | ||
context: { | ||
configName: `gatsby-config`, | ||
nearMatch: `gatsby-confi.js`, | ||
}, | ||
}) | ||
}) | ||
|
||
it(`should handle gatsby config incorrectly located in src dir`, async () => { | ||
testRequireErrorMock.mockImplementationOnce(() => true) | ||
|
||
await getConfigFile(srcDir, `gatsby-config`) | ||
|
||
expect(reporterPanicMock).toBeCalledWith({ | ||
id: `10125`, | ||
context: { | ||
configName: `gatsby-config`, | ||
}, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters