Skip to content

Fix failBuild() testing #36

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

Merged
merged 1 commit into from
Nov 14, 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
11 changes: 5 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ module.exports = {
const { failBuild } = utils.build

if (Object.keys(packageJson).length === 0) {
failBuild(`Could not find a package.json for this project`)
return
return failBuild(`Could not find a package.json for this project`)
}

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

if (isStaticExportProject({ build, scripts })) {
failBuild(`** Static HTML export next.js projects do not require this plugin **`)
return failBuild(`** Static HTML export next.js projects do not require this plugin **`)
}

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

const isFunctionsDirectoryCorrect =
FUNCTIONS_SRC !== undefined && path.resolve(FUNCTIONS_SRC) === path.resolve('out_functions')
if (!isFunctionsDirectoryCorrect) {
// to do rephrase
failBuild(
return failBuild(
`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`,
)
}
Expand All @@ -60,7 +59,7 @@ module.exports = {
const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.'))
const isValidTarget = acceptableTargets.includes(nextConfig.target)
if (!isValidTarget) {
failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`)
return failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`)
}
} else {
// Create the next config file with target set to serverless by default
Expand Down
97 changes: 44 additions & 53 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ const FIXTURES_DIR = `${__dirname}/fixtures`

const utils = {
run: {
command: jest.fn(),
command() {},
},
build: {
failBuild: jest.fn(),
failBuild(message) {
throw new Error(message)
},
},
}

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

afterEach(async () => {
utils.build.failBuild.mockReset()
utils.run.command.mockReset()
jest.clearAllMocks()
jest.resetAllMocks()

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

describe('preBuild()', () => {
test('fail build if the app has static html export in npm script', async () => {
await plugin.onPreBuild({
netlifyConfig: {},
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
'** Static HTML export next.js projects do not require this plugin **',
)
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **')
})

test('fail build if the app has static html export in toml/ntl config', async () => {
const netlifyConfig = { build: { command: 'next build && next export' } }

await plugin.onPreBuild({
netlifyConfig,
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
'** Static HTML export next.js projects do not require this plugin **',
)
await expect(
plugin.onPreBuild({
netlifyConfig: { build: { command: 'next build && next export' } },
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow('** Static HTML export next.js projects do not require this plugin **')
})

test('fail build if the app has no package.json', async () => {
await plugin.onPreBuild({
netlifyConfig: {},
packageJson: {},
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})

expect(utils.build.failBuild.mock.calls[0][0]).toEqual(`Could not find a package.json for this project`)
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson: {},
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow(`Could not find a package.json for this project`)
})

test('fail build if the app has no functions directory defined', async () => {
await plugin.onPreBuild({
netlifyConfig: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: {},
})

expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: {},
}),
).rejects.toThrow(
`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`,
)
})
Expand All @@ -122,18 +116,15 @@ describe('preBuild()', () => {

test(`fail build if the app's next config has an invalid target`, async () => {
const { restoreCwd } = useFixture('invalid_next_config')
await plugin.onPreBuild({
netlifyConfig: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
await expect(
plugin.onPreBuild({
netlifyConfig: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
}),
).rejects.toThrow(`next.config.js must be one of: serverless, experimental-serverless-trace`)
restoreCwd()

const acceptableTargets = ['serverless', 'experimental-serverless-trace']
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(
`next.config.js must be one of: ${acceptableTargets.join(', ')}`,
)
})
})

Expand Down