-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
test(create-gatsby): add test for handling errors in install plugins #28364
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { installPlugins } from "../install-plugins" | ||
import { reporter } from "../reporter" | ||
import { requireResolve } from "../require-utils" | ||
|
||
jest.mock(`../require-utils`) | ||
jest.mock(`../reporter`) | ||
|
||
jest.mock( | ||
`somewhere-virtually-existing`, | ||
() => { | ||
// Make sure not to resolve `addPlugins` in order to safely handle error | ||
return {} | ||
}, | ||
{ virtual: true } | ||
) | ||
|
||
describe(`install-plugins`, () => { | ||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it(`reports an error explaining that gatsby is not installed`, async () => { | ||
;(requireResolve as any).mockImplementation(() => undefined) | ||
|
||
await installPlugins([], {}, `not-existing-path`, []) | ||
|
||
// Test function behaviour but improves the DX, it probably worth it | ||
expect(reporter.error).toBeCalledWith( | ||
`Could not find "gatsby" in not-existing-path. Perhaps it wasn't installed properly?` | ||
) | ||
}) | ||
|
||
it(`reports an error when the gatsby cli is not installed`, async () => { | ||
;(requireResolve as any).mockImplementation(path => { | ||
if (path === `gatsby-cli/lib/handlers/plugin-add`) { | ||
throw new Error() | ||
} | ||
return `somewhere-i-belong` | ||
}) | ||
|
||
await installPlugins([], {}, `not-existing-path`, []) | ||
|
||
// Test function behaviour but improves the DX, it probably worth it | ||
expect(reporter.error).toBeCalledWith( | ||
`gatsby-cli not installed, or is too old` | ||
) | ||
}) | ||
|
||
it(`reports an error when add plugins fails somehow`, async () => { | ||
;(requireResolve as any).mockImplementation( | ||
() => `somewhere-virtually-existing` | ||
) | ||
|
||
await installPlugins([], {}, `not-existing-path`, []) | ||
|
||
// Test function behaviour but improves the DX, it probably worth it | ||
expect(reporter.error).toBeCalledWith( | ||
`Something went wrong when trying to add the plugins to the project: addPlugins is not a function` | ||
) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,49 +1,81 @@ | ||
import { reporter } from "./reporter" | ||
import path from "path" | ||
import { PluginConfigMap } from "." | ||
export async function installPlugins( | ||
plugins: Array<string>, | ||
pluginOptions: PluginConfigMap = {}, | ||
rootPath: string, | ||
packages: Array<string> | ||
): Promise<void> { | ||
let installPluginCommand | ||
let gatsbyPath | ||
import { requireResolve } from "./require-utils" | ||
|
||
const resolveGatsbyPath = (rootPath: string): string | never => { | ||
try { | ||
gatsbyPath = require.resolve(`gatsby/package.json`, { | ||
const gatsbyPath = requireResolve(`gatsby/package.json`, { | ||
paths: [rootPath], | ||
}) | ||
} catch (e) { | ||
// Not found | ||
console.warn(e) | ||
} | ||
|
||
if (!gatsbyPath) { | ||
reporter.error( | ||
if (!gatsbyPath) throw new Error() | ||
|
||
return gatsbyPath | ||
} catch (e) { | ||
throw new Error( | ||
`Could not find "gatsby" in ${rootPath}. Perhaps it wasn't installed properly?` | ||
) | ||
return | ||
} | ||
} | ||
|
||
const resolveGatsbyCliPath = ( | ||
rootPath: string, | ||
gatsbyPath: string | ||
): string | never => { | ||
try { | ||
installPluginCommand = require.resolve( | ||
const installPluginCommand = requireResolve( | ||
`gatsby-cli/lib/handlers/plugin-add`, | ||
{ | ||
// Try to find gatsby-cli in the site root, or in the site's gatsby dir | ||
paths: [rootPath, path.dirname(gatsbyPath)], | ||
} | ||
) | ||
|
||
if (!installPluginCommand) throw new Error() | ||
|
||
return installPluginCommand | ||
} catch (e) { | ||
// The file is missing | ||
throw new Error(`gatsby-cli not installed, or is too old`) | ||
} | ||
} | ||
|
||
if (!installPluginCommand) { | ||
reporter.error(`gatsby-cli not installed, or is too old`) | ||
return | ||
const addPluginsToProject = async ( | ||
installPluginCommand: string, | ||
plugins: Array<string>, | ||
pluginOptions: PluginConfigMap = {}, | ||
rootPath: string, | ||
packages: Array<string> | ||
): Promise<void> => { | ||
try { | ||
const { addPlugins } = require(installPluginCommand) | ||
await addPlugins(plugins, pluginOptions, rootPath, packages) | ||
} catch (e) { | ||
throw new Error( | ||
`Something went wrong when trying to add the plugins to the project: ${e.message}` | ||
) | ||
} | ||
} | ||
|
||
const { addPlugins } = require(installPluginCommand) | ||
export async function installPlugins( | ||
plugins: Array<string>, | ||
pluginOptions: PluginConfigMap = {}, | ||
rootPath: string, | ||
packages: Array<string> | ||
): Promise<void> { | ||
try { | ||
const gatsbyPath = resolveGatsbyPath(rootPath) | ||
const installPluginCommand = resolveGatsbyCliPath(rootPath, gatsbyPath) | ||
|
||
await addPlugins(plugins, pluginOptions, rootPath, packages) | ||
await addPluginsToProject( | ||
installPluginCommand, | ||
plugins, | ||
pluginOptions, | ||
rootPath, | ||
packages | ||
) | ||
} catch (e) { | ||
reporter.error(e.message) | ||
return | ||
} | ||
} |
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,4 @@ | ||
export const requireResolve = ( | ||
id: string, | ||
options?: { paths?: Array<string> | undefined } | undefined | ||
): string => require.resolve(id, options) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ Aims to mock
require.resolve
that is not yet shipped in jest: jestjs/jest#9543