1
1
const path = require ( 'path' )
2
2
const process = require ( 'process' )
3
+
4
+ const cpy = require ( 'cpy' )
3
5
const pathExists = require ( 'path-exists' )
4
6
const { dir : getTmpDir } = require ( 'tmp-promise' )
5
- const cpy = require ( 'cpy' )
6
7
7
8
const plugin = require ( '..' )
9
+ const getNextConfig = require ( '../helpers/getNextConfig' )
8
10
9
11
const FIXTURES_DIR = `${ __dirname } /fixtures`
10
12
const SAMPLE_PROJECT_DIR = `${ __dirname } /sample`
@@ -47,6 +49,12 @@ const useFixture = async function (fixtureName) {
47
49
// In each test, we change cwd to a temporary directory.
48
50
// This allows us not to have to mock filesystem operations.
49
51
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 ( )
50
58
const { path, cleanup } = await getTmpDir ( { unsafeCleanup : true } )
51
59
const restoreCwd = changeCwd ( path )
52
60
Object . assign ( this , { cleanup, restoreCwd } )
@@ -66,17 +74,6 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
66
74
const netlifyConfig = { build : { } }
67
75
68
76
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
-
80
77
test ( 'do nothing if the app has static html export in npm script' , async ( ) => {
81
78
await plugin . onPreBuild ( {
82
79
netlifyConfig : { build : { command : 'npm run build' } } ,
@@ -95,8 +92,7 @@ describe('preBuild()', () => {
95
92
utils,
96
93
constants : { } ,
97
94
} )
98
-
99
- expect ( await pathExists ( 'next.config.js' ) ) . toBeTruthy ( )
95
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBe ( 'serverless' )
100
96
} )
101
97
102
98
test ( 'do nothing if app has static html export in toml/ntl config' , async ( ) => {
@@ -107,7 +103,7 @@ describe('preBuild()', () => {
107
103
constants : { FUNCTIONS_SRC : 'out_functions' } ,
108
104
} )
109
105
110
- expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
106
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
111
107
} )
112
108
113
109
test ( 'do nothing if app has next-on-netlify installed' , async ( ) => {
@@ -120,7 +116,7 @@ describe('preBuild()', () => {
120
116
utils,
121
117
} )
122
118
123
- expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
119
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
124
120
} )
125
121
126
122
test ( 'do nothing if app has next-on-netlify postbuild script' , async ( ) => {
@@ -133,7 +129,7 @@ describe('preBuild()', () => {
133
129
utils,
134
130
} )
135
131
136
- expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
132
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
137
133
} )
138
134
139
135
test ( 'fail build if the app has no package.json' , async ( ) => {
@@ -185,6 +181,7 @@ describe('preBuild()', () => {
185
181
} )
186
182
187
183
describe ( 'onBuild()' , ( ) => {
184
+ // eslint-disable-next-line max-lines
188
185
test ( 'does not run onBuild if using next-on-netlify' , async ( ) => {
189
186
const packageJson = {
190
187
scripts : { postbuild : 'next-on-netlify' } ,
@@ -202,23 +199,6 @@ describe('onBuild()', () => {
202
199
expect ( await pathExists ( `${ PUBLISH_DIR } /index.html` ) ) . toBeFalsy ( )
203
200
} )
204
201
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
-
222
202
test ( 'copy files to the publish directory' , async ( ) => {
223
203
await useFixture ( 'publish_copy_files' )
224
204
await moveNextDist ( )
0 commit comments