Skip to content

Commit a8334d0

Browse files
committed
Fix package.json validation
1 parent 2849dc5 commit a8334d0

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
async onPreBuild({ netlifyConfig, packageJson, utils }) {
1919
const { failBuild } = utils.build
2020

21-
if (!packageJson) {
21+
if (Object.keys(packageJson).length === 0) {
2222
failBuild(`Could not find a package.json for this project`)
2323
return
2424
}

test/index.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ jest.mock('cpx')
3131
// for why this log is required
3232
console.log('Initializing tests')
3333

34+
const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
35+
3436
describe('preBuild()', () => {
3537
test('fail build if the app has static html export in npm script', async () => {
36-
const packageJson = { scripts: { build: 'next export' } }
37-
3838
await plugin.onPreBuild({
3939
netlifyConfig: {},
40-
packageJson,
40+
packageJson: { ...DUMMY_PACKAGE_JSON, scripts: { build: 'next export' } },
4141
utils,
4242
})
4343

@@ -48,11 +48,10 @@ describe('preBuild()', () => {
4848

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

5352
await plugin.onPreBuild({
5453
netlifyConfig,
55-
packageJson,
54+
packageJson: DUMMY_PACKAGE_JSON,
5655
utils,
5756
})
5857

@@ -61,13 +60,22 @@ describe('preBuild()', () => {
6160
)
6261
})
6362

63+
test('fail build if the app has no package.json', async () => {
64+
await plugin.onPreBuild({
65+
netlifyConfig: {},
66+
packageJson: {},
67+
utils,
68+
})
69+
70+
expect(utils.build.failBuild.mock.calls[0][0]).toEqual(`Could not find a package.json for this project`)
71+
})
72+
6473
test('fail build if the app has no functions directory defined', async () => {
6574
const netlifyConfig = { build: {} }
66-
const packageJson = {}
6775

6876
await plugin.onPreBuild({
6977
netlifyConfig,
70-
packageJson,
78+
packageJson: DUMMY_PACKAGE_JSON,
7179
utils,
7280
})
7381

@@ -79,7 +87,7 @@ describe('preBuild()', () => {
7987
test('create next.config.js with correct target if file does not exist', async () => {
8088
await plugin.onPreBuild({
8189
netlifyConfig: {},
82-
packageJson: {},
90+
packageJson: DUMMY_PACKAGE_JSON,
8391
utils,
8492
})
8593

@@ -96,7 +104,7 @@ describe('preBuild()', () => {
96104

97105
await plugin.onPreBuild({
98106
netlifyConfig,
99-
packageJson: {},
107+
packageJson: DUMMY_PACKAGE_JSON,
100108
utils,
101109
})
102110

0 commit comments

Comments
 (0)