Skip to content

Commit e456a79

Browse files
authored
Merge pull request #36 from netlify/bug/fix-fail-build
Fix `failBuild()` testing
2 parents ea07d97 + 6747377 commit e456a79

File tree

2 files changed

+49
-59
lines changed

2 files changed

+49
-59
lines changed

index.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ module.exports = {
2222
const { failBuild } = utils.build
2323

2424
if (Object.keys(packageJson).length === 0) {
25-
failBuild(`Could not find a package.json for this project`)
26-
return
25+
return failBuild(`Could not find a package.json for this project`)
2726
}
2827

2928
const { build } = netlifyConfig
@@ -34,21 +33,21 @@ module.exports = {
3433
await utils.run.command('npm install next-on-netlify@latest')
3534

3635
if (isStaticExportProject({ build, scripts })) {
37-
failBuild(`** Static HTML export next.js projects do not require this plugin **`)
36+
return failBuild(`** Static HTML export next.js projects do not require this plugin **`)
3837
}
3938

4039
// TO-DO: check scripts to make sure the app isn't manually running NoN
4140
// For now, we'll make it clear in the README
4241
// const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
4342
// if (isAlreadyUsingNextOnNetlify) {
44-
// failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
43+
// return failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
4544
// }
4645

4746
const isFunctionsDirectoryCorrect =
4847
FUNCTIONS_SRC !== undefined && path.resolve(FUNCTIONS_SRC) === path.resolve('out_functions')
4948
if (!isFunctionsDirectoryCorrect) {
5049
// to do rephrase
51-
failBuild(
50+
return failBuild(
5251
`You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder`,
5352
)
5453
}
@@ -60,7 +59,7 @@ module.exports = {
6059
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
6160
const isValidTarget = acceptableTargets.includes(nextConfig.target)
6261
if (!isValidTarget) {
63-
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`)
62+
return failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`)
6463
}
6564
} else {
6665
// Create the next config file with target set to serverless by default

test/index.js

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ const FIXTURES_DIR = `${__dirname}/fixtures`
1111

1212
const utils = {
1313
run: {
14-
command: jest.fn(),
14+
command() {},
1515
},
1616
build: {
17-
failBuild: jest.fn(),
17+
failBuild(message) {
18+
throw new Error(message)
19+
},
1820
},
1921
}
2022

@@ -41,8 +43,6 @@ beforeEach(async () => {
4143
})
4244

4345
afterEach(async () => {
44-
utils.build.failBuild.mockReset()
45-
utils.run.command.mockReset()
4646
jest.clearAllMocks()
4747
jest.resetAllMocks()
4848

@@ -58,53 +58,47 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
5858

5959
describe('preBuild()', () => {
6060
test('fail build if the app has static html export in npm script', async () => {
61-
await plugin.onPreBuild({
62-
netlifyConfig: {},
63-
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
64-
utils,
65-
constants: { FUNCTIONS_SRC: 'out_functions' },
66-
})
67-
68-
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
69-
'** Static HTML export next.js projects do not require this plugin **',
70-
)
61+
await expect(
62+
plugin.onPreBuild({
63+
netlifyConfig: {},
64+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
65+
utils,
66+
constants: { FUNCTIONS_SRC: 'out_functions' },
67+
}),
68+
).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **')
7169
})
7270

7371
test('fail build if the app has static html export in toml/ntl config', async () => {
74-
const netlifyConfig = { build: { command: 'next build && next export' } }
75-
76-
await plugin.onPreBuild({
77-
netlifyConfig,
78-
packageJson: DUMMY_PACKAGE_JSON,
79-
utils,
80-
constants: { FUNCTIONS_SRC: 'out_functions' },
81-
})
82-
83-
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
84-
'** Static HTML export next.js projects do not require this plugin **',
85-
)
72+
await expect(
73+
plugin.onPreBuild({
74+
netlifyConfig: { build: { command: 'next build && next export' } },
75+
packageJson: DUMMY_PACKAGE_JSON,
76+
utils,
77+
constants: { FUNCTIONS_SRC: 'out_functions' },
78+
}),
79+
).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **')
8680
})
8781

8882
test('fail build if the app has no package.json', async () => {
89-
await plugin.onPreBuild({
90-
netlifyConfig: {},
91-
packageJson: {},
92-
utils,
93-
constants: { FUNCTIONS_SRC: 'out_functions' },
94-
})
95-
96-
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(`Could not find a package.json for this project`)
83+
await expect(
84+
plugin.onPreBuild({
85+
netlifyConfig: {},
86+
packageJson: {},
87+
utils,
88+
constants: { FUNCTIONS_SRC: 'out_functions' },
89+
}),
90+
).rejects.toThrow(`Could not find a package.json for this project`)
9791
})
9892

9993
test('fail build if the app has no functions directory defined', async () => {
100-
await plugin.onPreBuild({
101-
netlifyConfig: {},
102-
packageJson: DUMMY_PACKAGE_JSON,
103-
utils,
104-
constants: {},
105-
})
106-
107-
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
94+
await expect(
95+
plugin.onPreBuild({
96+
netlifyConfig: {},
97+
packageJson: DUMMY_PACKAGE_JSON,
98+
utils,
99+
constants: {},
100+
}),
101+
).rejects.toThrow(
108102
`You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder`,
109103
)
110104
})
@@ -122,18 +116,15 @@ describe('preBuild()', () => {
122116

123117
test(`fail build if the app's next config has an invalid target`, async () => {
124118
const { restoreCwd } = useFixture('invalid_next_config')
125-
await plugin.onPreBuild({
126-
netlifyConfig: {},
127-
packageJson: DUMMY_PACKAGE_JSON,
128-
utils,
129-
constants: { FUNCTIONS_SRC: 'out_functions' },
130-
})
119+
await expect(
120+
plugin.onPreBuild({
121+
netlifyConfig: {},
122+
packageJson: DUMMY_PACKAGE_JSON,
123+
utils,
124+
constants: { FUNCTIONS_SRC: 'out_functions' },
125+
}),
126+
).rejects.toThrow(`next.config.js must be one of: serverless, experimental-serverless-trace`)
131127
restoreCwd()
132-
133-
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
134-
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
135-
`next.config.js must be one of: ${acceptableTargets.join(', ')}`,
136-
)
137128
})
138129
})
139130

0 commit comments

Comments
 (0)