Skip to content

Commit e7b329c

Browse files
author
Marvin Frachet
authored
test(create-gatsby): add test for handling errors in install plugins (#28364)
1 parent 3f2a49e commit e7b329c

File tree

3 files changed

+120
-23
lines changed

3 files changed

+120
-23
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { installPlugins } from "../install-plugins"
2+
import { reporter } from "../reporter"
3+
import { requireResolve } from "../require-utils"
4+
5+
jest.mock(`../require-utils`)
6+
jest.mock(`../reporter`)
7+
8+
jest.mock(
9+
`somewhere-virtually-existing`,
10+
() => {
11+
// Make sure not to resolve `addPlugins` in order to safely handle error
12+
return {}
13+
},
14+
{ virtual: true }
15+
)
16+
17+
describe(`install-plugins`, () => {
18+
afterEach(() => {
19+
jest.resetAllMocks()
20+
})
21+
22+
it(`reports an error explaining that gatsby is not installed`, async () => {
23+
;(requireResolve as any).mockImplementation(() => undefined)
24+
25+
await installPlugins([], {}, `not-existing-path`, [])
26+
27+
// Test function behaviour but improves the DX, it probably worth it
28+
expect(reporter.error).toBeCalledWith(
29+
`Could not find "gatsby" in not-existing-path. Perhaps it wasn't installed properly?`
30+
)
31+
})
32+
33+
it(`reports an error when the gatsby cli is not installed`, async () => {
34+
;(requireResolve as any).mockImplementation(path => {
35+
if (path === `gatsby-cli/lib/handlers/plugin-add`) {
36+
throw new Error()
37+
}
38+
return `somewhere-i-belong`
39+
})
40+
41+
await installPlugins([], {}, `not-existing-path`, [])
42+
43+
// Test function behaviour but improves the DX, it probably worth it
44+
expect(reporter.error).toBeCalledWith(
45+
`gatsby-cli not installed, or is too old`
46+
)
47+
})
48+
49+
it(`reports an error when add plugins fails somehow`, async () => {
50+
;(requireResolve as any).mockImplementation(
51+
() => `somewhere-virtually-existing`
52+
)
53+
54+
await installPlugins([], {}, `not-existing-path`, [])
55+
56+
// Test function behaviour but improves the DX, it probably worth it
57+
expect(reporter.error).toBeCalledWith(
58+
`Something went wrong when trying to add the plugins to the project: addPlugins is not a function`
59+
)
60+
})
61+
})
Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,81 @@
11
import { reporter } from "./reporter"
22
import path from "path"
33
import { PluginConfigMap } from "."
4-
export async function installPlugins(
5-
plugins: Array<string>,
6-
pluginOptions: PluginConfigMap = {},
7-
rootPath: string,
8-
packages: Array<string>
9-
): Promise<void> {
10-
let installPluginCommand
11-
let gatsbyPath
4+
import { requireResolve } from "./require-utils"
125

6+
const resolveGatsbyPath = (rootPath: string): string | never => {
137
try {
14-
gatsbyPath = require.resolve(`gatsby/package.json`, {
8+
const gatsbyPath = requireResolve(`gatsby/package.json`, {
159
paths: [rootPath],
1610
})
17-
} catch (e) {
18-
// Not found
19-
console.warn(e)
20-
}
2111

22-
if (!gatsbyPath) {
23-
reporter.error(
12+
if (!gatsbyPath) throw new Error()
13+
14+
return gatsbyPath
15+
} catch (e) {
16+
throw new Error(
2417
`Could not find "gatsby" in ${rootPath}. Perhaps it wasn't installed properly?`
2518
)
26-
return
2719
}
20+
}
2821

22+
const resolveGatsbyCliPath = (
23+
rootPath: string,
24+
gatsbyPath: string
25+
): string | never => {
2926
try {
30-
installPluginCommand = require.resolve(
27+
const installPluginCommand = requireResolve(
3128
`gatsby-cli/lib/handlers/plugin-add`,
3229
{
3330
// Try to find gatsby-cli in the site root, or in the site's gatsby dir
3431
paths: [rootPath, path.dirname(gatsbyPath)],
3532
}
3633
)
34+
35+
if (!installPluginCommand) throw new Error()
36+
37+
return installPluginCommand
3738
} catch (e) {
38-
// The file is missing
39+
throw new Error(`gatsby-cli not installed, or is too old`)
3940
}
41+
}
4042

41-
if (!installPluginCommand) {
42-
reporter.error(`gatsby-cli not installed, or is too old`)
43-
return
43+
const addPluginsToProject = async (
44+
installPluginCommand: string,
45+
plugins: Array<string>,
46+
pluginOptions: PluginConfigMap = {},
47+
rootPath: string,
48+
packages: Array<string>
49+
): Promise<void> => {
50+
try {
51+
const { addPlugins } = require(installPluginCommand)
52+
await addPlugins(plugins, pluginOptions, rootPath, packages)
53+
} catch (e) {
54+
throw new Error(
55+
`Something went wrong when trying to add the plugins to the project: ${e.message}`
56+
)
4457
}
58+
}
4559

46-
const { addPlugins } = require(installPluginCommand)
60+
export async function installPlugins(
61+
plugins: Array<string>,
62+
pluginOptions: PluginConfigMap = {},
63+
rootPath: string,
64+
packages: Array<string>
65+
): Promise<void> {
66+
try {
67+
const gatsbyPath = resolveGatsbyPath(rootPath)
68+
const installPluginCommand = resolveGatsbyCliPath(rootPath, gatsbyPath)
4769

48-
await addPlugins(plugins, pluginOptions, rootPath, packages)
70+
await addPluginsToProject(
71+
installPluginCommand,
72+
plugins,
73+
pluginOptions,
74+
rootPath,
75+
packages
76+
)
77+
} catch (e) {
78+
reporter.error(e.message)
79+
return
80+
}
4981
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export const requireResolve = (
2+
id: string,
3+
options?: { paths?: Array<string> | undefined } | undefined
4+
): string => require.resolve(id, options)

0 commit comments

Comments
 (0)