Skip to content

Commit 4ceb71f

Browse files
authored
Merge pull request #29 from netlify/bug/package-json
Fix `package.json` validation
2 parents 5916d2f + a0dd873 commit 4ceb71f

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

index.js

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

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

test/index.js

Lines changed: 18 additions & 10 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
constants: { FUNCTIONS_SRC: 'out_functions' },
4343
})
@@ -49,11 +49,10 @@ describe('preBuild()', () => {
4949

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

5453
await plugin.onPreBuild({
5554
netlifyConfig,
56-
packageJson,
55+
packageJson: DUMMY_PACKAGE_JSON,
5756
utils,
5857
constants: { FUNCTIONS_SRC: 'out_functions' },
5958
})
@@ -63,12 +62,21 @@ describe('preBuild()', () => {
6362
)
6463
})
6564

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

76+
test('fail build if the app has no functions directory defined', async () => {
6977
await plugin.onPreBuild({
7078
netlifyConfig: {},
71-
packageJson,
79+
packageJson: DUMMY_PACKAGE_JSON,
7280
utils,
7381
constants: {},
7482
})
@@ -81,7 +89,7 @@ describe('preBuild()', () => {
8189
test('create next.config.js with correct target if file does not exist', async () => {
8290
await plugin.onPreBuild({
8391
netlifyConfig: {},
84-
packageJson: {},
92+
packageJson: DUMMY_PACKAGE_JSON,
8593
utils,
8694
constants: { FUNCTIONS_SRC: 'out_functions' },
8795
})
@@ -98,7 +106,7 @@ describe('preBuild()', () => {
98106

99107
await plugin.onPreBuild({
100108
netlifyConfig: {},
101-
packageJson: {},
109+
packageJson: DUMMY_PACKAGE_JSON,
102110
utils,
103111
constants: { FUNCTIONS_SRC: 'out_functions' },
104112
})

0 commit comments

Comments
 (0)