Skip to content

Fix package.json validation #29

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 13, 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
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = {
async onPreBuild({ netlifyConfig, packageJson, utils, constants: { FUNCTIONS_SRC } }) {
const { failBuild } = utils.build

if (!packageJson) {
if (Object.keys(packageJson).length === 0) {
failBuild(`Could not find a package.json for this project`)
return
}
Expand Down
28 changes: 18 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ jest.mock('cpx')
// for why this log is required
console.log('Initializing tests')

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 () => {
const packageJson = { scripts: { build: 'next export' } }

await plugin.onPreBuild({
netlifyConfig: {},
packageJson,
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
Expand All @@ -49,11 +49,10 @@ describe('preBuild()', () => {

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

await plugin.onPreBuild({
netlifyConfig,
packageJson,
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
Expand All @@ -63,12 +62,21 @@ describe('preBuild()', () => {
)
})

test('fail build if the app has no functions directory defined', async () => {
const packageJson = {}
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`)
})

test('fail build if the app has no functions directory defined', async () => {
await plugin.onPreBuild({
netlifyConfig: {},
packageJson,
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: {},
})
Expand All @@ -81,7 +89,7 @@ describe('preBuild()', () => {
test('create next.config.js with correct target if file does not exist', async () => {
await plugin.onPreBuild({
netlifyConfig: {},
packageJson: {},
packageJson: DUMMY_PACKAGE_JSON,
utils,
constants: { FUNCTIONS_SRC: 'out_functions' },
})
Expand All @@ -98,7 +106,7 @@ describe('preBuild()', () => {

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