1
1
const path = require ( 'path' )
2
2
const process = require ( 'process' )
3
-
4
- const cpy = require ( 'cpy' )
5
3
const pathExists = require ( 'path-exists' )
6
4
const { dir : getTmpDir } = require ( 'tmp-promise' )
5
+ const cpy = require ( 'cpy' )
7
6
8
7
const plugin = require ( '..' )
9
- const getNextConfig = require ( '../helpers/getNextConfig' )
10
8
11
9
const FIXTURES_DIR = `${ __dirname } /fixtures`
12
10
const SAMPLE_PROJECT_DIR = `${ __dirname } /sample`
@@ -49,12 +47,6 @@ const useFixture = async function (fixtureName) {
49
47
// In each test, we change cwd to a temporary directory.
50
48
// This allows us not to have to mock filesystem operations.
51
49
beforeEach ( async ( ) => {
52
- // This is so we can test the target setting code
53
- delete process . env . NEXT_PRIVATE_TARGET
54
- delete require . cache [ require . resolve ( 'next/dist/telemetry/ci-info' ) ]
55
- delete require . cache [ require . resolve ( 'next/dist/next-server/server/config' ) ]
56
-
57
- getNextConfig . clear ( )
58
50
const { path, cleanup } = await getTmpDir ( { unsafeCleanup : true } )
59
51
const restoreCwd = changeCwd ( path )
60
52
Object . assign ( this , { cleanup, restoreCwd } )
@@ -74,6 +66,17 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
74
66
const netlifyConfig = { build : { } }
75
67
76
68
describe ( 'preBuild()' , ( ) => {
69
+ test ( 'create next.config.js with correct target if file does not exist' , async ( ) => {
70
+ await plugin . onPreBuild ( {
71
+ netlifyConfig,
72
+ packageJson : DUMMY_PACKAGE_JSON ,
73
+ utils,
74
+ constants : { FUNCTIONS_SRC : 'out_functions' } ,
75
+ } )
76
+
77
+ expect ( await pathExists ( 'next.config.js' ) ) . toBeTruthy ( )
78
+ } )
79
+
77
80
test ( 'do nothing if the app has static html export in npm script' , async ( ) => {
78
81
await plugin . onPreBuild ( {
79
82
netlifyConfig : { build : { command : 'npm run build' } } ,
@@ -92,7 +95,8 @@ describe('preBuild()', () => {
92
95
utils,
93
96
constants : { } ,
94
97
} )
95
- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBe ( 'serverless' )
98
+
99
+ expect ( await pathExists ( 'next.config.js' ) ) . toBeTruthy ( )
96
100
} )
97
101
98
102
test ( 'do nothing if app has static html export in toml/ntl config' , async ( ) => {
@@ -103,7 +107,7 @@ describe('preBuild()', () => {
103
107
constants : { FUNCTIONS_SRC : 'out_functions' } ,
104
108
} )
105
109
106
- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
110
+ expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
107
111
} )
108
112
109
113
test ( 'do nothing if app has next-on-netlify installed' , async ( ) => {
@@ -116,7 +120,7 @@ describe('preBuild()', () => {
116
120
utils,
117
121
} )
118
122
119
- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
123
+ expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
120
124
} )
121
125
122
126
test ( 'do nothing if app has next-on-netlify postbuild script' , async ( ) => {
@@ -129,7 +133,7 @@ describe('preBuild()', () => {
129
133
utils,
130
134
} )
131
135
132
- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
136
+ expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
133
137
} )
134
138
135
139
test ( 'fail build if the app has no package.json' , async ( ) => {
@@ -181,7 +185,6 @@ describe('preBuild()', () => {
181
185
} )
182
186
183
187
describe ( 'onBuild()' , ( ) => {
184
- // eslint-disable-next-line max-lines
185
188
test ( 'does not run onBuild if using next-on-netlify' , async ( ) => {
186
189
const packageJson = {
187
190
scripts : { postbuild : 'next-on-netlify' } ,
@@ -199,6 +202,23 @@ describe('onBuild()', () => {
199
202
expect ( await pathExists ( `${ PUBLISH_DIR } /index.html` ) ) . toBeFalsy ( )
200
203
} )
201
204
205
+ test . each ( [ 'invalid_next_config' , 'deep_invalid_next_config' ] ) (
206
+ `do nothing if the app's next config has an invalid target` ,
207
+ async ( fixtureName ) => {
208
+ await useFixture ( fixtureName )
209
+ const PUBLISH_DIR = 'publish'
210
+ await plugin . onBuild ( {
211
+ netlifyConfig,
212
+ packageJson : DUMMY_PACKAGE_JSON ,
213
+ utils,
214
+ constants : { FUNCTIONS_SRC : 'out_functions' } ,
215
+ utils,
216
+ } )
217
+
218
+ expect ( await pathExists ( `${ PUBLISH_DIR } /index.html` ) ) . toBeFalsy ( )
219
+ } ,
220
+ )
221
+
202
222
test ( 'copy files to the publish directory' , async ( ) => {
203
223
await useFixture ( 'publish_copy_files' )
204
224
await moveNextDist ( )
0 commit comments