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,11 @@ 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
+ delete process . env . NEXT_PRIVATE_TARGET
53
+ delete require . cache [ require . resolve ( 'next/dist/telemetry/ci-info' ) ]
54
+ delete require . cache [ require . resolve ( 'next/dist/next-server/server/config' ) ]
55
+
56
+ getNextConfig . clear ( )
50
57
const { path, cleanup } = await getTmpDir ( { unsafeCleanup : true } )
51
58
const restoreCwd = changeCwd ( path )
52
59
Object . assign ( this , { cleanup, restoreCwd } )
@@ -66,17 +73,6 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
66
73
const netlifyConfig = { build : { } }
67
74
68
75
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
76
test ( 'do nothing if the app has static html export in npm script' , async ( ) => {
81
77
await plugin . onPreBuild ( {
82
78
netlifyConfig : { build : { command : 'npm run build' } } ,
@@ -89,14 +85,15 @@ describe('preBuild()', () => {
89
85
} )
90
86
91
87
test ( 'run plugin if the app has next export in an unused script' , async ( ) => {
88
+ process . env . hi = 'ok'
92
89
await plugin . onPreBuild ( {
93
90
netlifyConfig,
94
91
packageJson : { ...DUMMY_PACKAGE_JSON , scripts : { export : 'next export' } } ,
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' )
96
+ // expect(await pathExists('next.config.js')).toBeTruthy()
100
97
} )
101
98
102
99
test ( 'do nothing if app has static html export in toml/ntl config' , async ( ) => {
@@ -107,7 +104,7 @@ describe('preBuild()', () => {
107
104
constants : { FUNCTIONS_SRC : 'out_functions' } ,
108
105
} )
109
106
110
- expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
107
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
111
108
} )
112
109
113
110
test ( 'do nothing if app has next-on-netlify installed' , async ( ) => {
@@ -120,7 +117,7 @@ describe('preBuild()', () => {
120
117
utils,
121
118
} )
122
119
123
- expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
120
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
124
121
} )
125
122
126
123
test ( 'do nothing if app has next-on-netlify postbuild script' , async ( ) => {
@@ -133,7 +130,7 @@ describe('preBuild()', () => {
133
130
utils,
134
131
} )
135
132
136
- expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
133
+ expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
137
134
} )
138
135
139
136
test ( 'fail build if the app has no package.json' , async ( ) => {
@@ -185,6 +182,7 @@ describe('preBuild()', () => {
185
182
} )
186
183
187
184
describe ( 'onBuild()' , ( ) => {
185
+ // eslint-disable-next-line max-lines
188
186
test ( 'does not run onBuild if using next-on-netlify' , async ( ) => {
189
187
const packageJson = {
190
188
scripts : { postbuild : 'next-on-netlify' } ,
@@ -202,23 +200,6 @@ describe('onBuild()', () => {
202
200
expect ( await pathExists ( `${ PUBLISH_DIR } /index.html` ) ) . toBeFalsy ( )
203
201
} )
204
202
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
203
test ( 'copy files to the publish directory' , async ( ) => {
223
204
await useFixture ( 'publish_copy_files' )
224
205
await moveNextDist ( )
0 commit comments