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

test(create-gatsby): add test for handling errors in install plugins #28364

Merged
merged 3 commits into from
Dec 3, 2020
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
61 changes: 61 additions & 0 deletions packages/create-gatsby/src/__tests__/install-plugins.ts
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`
)
})
})
78 changes: 55 additions & 23 deletions packages/create-gatsby/src/install-plugins.ts
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
}
}
4 changes: 4 additions & 0 deletions packages/create-gatsby/src/require-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const requireResolve = (
Copy link
Contributor Author

@mfrachet mfrachet Nov 30, 2020

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

id: string,
options?: { paths?: Array<string> | undefined } | undefined
): string => require.resolve(id, options)