11const path = require ( 'path' )
22const process = require ( 'process' )
3-
4- const cpy = require ( 'cpy' )
53const pathExists = require ( 'path-exists' )
64const { dir : getTmpDir } = require ( 'tmp-promise' )
5+ const cpy = require ( 'cpy' )
76
87const plugin = require ( '..' )
9- const getNextConfig = require ( '../helpers/getNextConfig' )
108
119const FIXTURES_DIR = `${ __dirname } /fixtures`
1210const SAMPLE_PROJECT_DIR = `${ __dirname } /sample`
@@ -49,12 +47,6 @@ const useFixture = async function (fixtureName) {
4947// In each test, we change cwd to a temporary directory.
5048// This allows us not to have to mock filesystem operations.
5149beforeEach ( 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 ( )
5850 const { path, cleanup } = await getTmpDir ( { unsafeCleanup : true } )
5951 const restoreCwd = changeCwd ( path )
6052 Object . assign ( this , { cleanup, restoreCwd } )
@@ -74,6 +66,17 @@ const DUMMY_PACKAGE_JSON = { name: 'dummy', version: '1.0.0' }
7466const netlifyConfig = { build : { } }
7567
7668describe ( '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+
7780 test ( 'do nothing if the app has static html export in npm script' , async ( ) => {
7881 await plugin . onPreBuild ( {
7982 netlifyConfig : { build : { command : 'npm run build' } } ,
@@ -92,7 +95,8 @@ describe('preBuild()', () => {
9295 utils,
9396 constants : { } ,
9497 } )
95- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBe ( 'serverless' )
98+
99+ expect ( await pathExists ( 'next.config.js' ) ) . toBeTruthy ( )
96100 } )
97101
98102 test ( 'do nothing if app has static html export in toml/ntl config' , async ( ) => {
@@ -103,7 +107,7 @@ describe('preBuild()', () => {
103107 constants : { FUNCTIONS_SRC : 'out_functions' } ,
104108 } )
105109
106- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
110+ expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
107111 } )
108112
109113 test ( 'do nothing if app has next-on-netlify installed' , async ( ) => {
@@ -116,7 +120,7 @@ describe('preBuild()', () => {
116120 utils,
117121 } )
118122
119- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
123+ expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
120124 } )
121125
122126 test ( 'do nothing if app has next-on-netlify postbuild script' , async ( ) => {
@@ -129,7 +133,7 @@ describe('preBuild()', () => {
129133 utils,
130134 } )
131135
132- expect ( process . env . NEXT_PRIVATE_TARGET ) . toBeUndefined ( )
136+ expect ( await pathExists ( 'next.config.js' ) ) . toBeFalsy ( )
133137 } )
134138
135139 test ( 'fail build if the app has no package.json' , async ( ) => {
@@ -181,7 +185,6 @@ describe('preBuild()', () => {
181185} )
182186
183187describe ( 'onBuild()' , ( ) => {
184- // eslint-disable-next-line max-lines
185188 test ( 'does not run onBuild if using next-on-netlify' , async ( ) => {
186189 const packageJson = {
187190 scripts : { postbuild : 'next-on-netlify' } ,
@@ -199,6 +202,23 @@ describe('onBuild()', () => {
199202 expect ( await pathExists ( `${ PUBLISH_DIR } /index.html` ) ) . toBeFalsy ( )
200203 } )
201204
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+
202222 test ( 'copy files to the publish directory' , async ( ) => {
203223 await useFixture ( 'publish_copy_files' )
204224 await moveNextDist ( )
0 commit comments