@@ -5,6 +5,7 @@ jest.mock('../plugin/src/helpers/utils', () => {
55 }
66} )
77
8+ const Chance = require ( 'chance' )
89const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir, readJson } = require ( 'fs-extra' )
910const path = require ( 'path' )
1011const process = require ( 'process' )
@@ -29,6 +30,7 @@ const { getRequiredServerFiles, updateRequiredServerFiles } = require('../plugin
2930const { dirname } = require ( 'path' )
3031const { getProblematicUserRewrites } = require ( '../plugin/src/helpers/verification' )
3132
33+ const chance = new Chance ( )
3234const FIXTURES_DIR = `${ __dirname } /fixtures`
3335const SAMPLE_PROJECT_DIR = `${ __dirname } /../demos/default`
3436const constants = {
@@ -221,8 +223,31 @@ describe('onBuild()', () => {
221223 } )
222224 } )
223225
226+ afterEach ( ( ) => {
227+ delete process . env . URL
228+ } )
229+
230+ test ( 'does not set NEXTAUTH_URL if value is already set' , async ( ) => {
231+ const mockUserDefinedSiteUrl = chance . url ( )
232+ process . env . URL = chance . url ( )
233+
234+ await moveNextDist ( )
235+
236+ const initialConfig = await getRequiredServerFiles ( netlifyConfig . build . publish )
237+
238+ initialConfig . config . env . NEXTAUTH_URL = mockUserDefinedSiteUrl
239+ await updateRequiredServerFiles ( netlifyConfig . build . publish , initialConfig )
240+
241+ await plugin . onBuild ( defaultArgs )
242+
243+ expect ( onBuildHasRun ( netlifyConfig ) ) . toBe ( true )
244+ const config = await getRequiredServerFiles ( netlifyConfig . build . publish )
245+
246+ expect ( config . config . env . NEXTAUTH_URL ) . toEqual ( mockUserDefinedSiteUrl )
247+ } )
248+
224249 test ( 'sets NEXTAUTH_URL when next-auth package is detected' , async ( ) => {
225- const mockSiteUrl = 'https://my-netlify-site.app'
250+ const mockSiteUrl = chance . url ( )
226251
227252 // Value represents the main address to the site and is either
228253 // a Netlify subdomain or custom domain set by the user.
@@ -237,12 +262,10 @@ describe('onBuild()', () => {
237262 const config = await getRequiredServerFiles ( netlifyConfig . build . publish )
238263
239264 expect ( config . config . env . NEXTAUTH_URL ) . toEqual ( mockSiteUrl )
240-
241- delete process . env . URL
242265 } )
243266
244267 test ( 'includes the basePath on NEXTAUTH_URL when present' , async ( ) => {
245- const mockSiteUrl = 'https://my-netlify-site.app'
268+ const mockSiteUrl = chance . url ( )
246269 process . env . URL = mockSiteUrl
247270
248271 await moveNextDist ( )
@@ -257,8 +280,6 @@ describe('onBuild()', () => {
257280 const config = await getRequiredServerFiles ( netlifyConfig . build . publish )
258281
259282 expect ( config . config . env . NEXTAUTH_URL ) . toEqual ( `${ mockSiteUrl } /foo` )
260-
261- delete process . env . URL
262283 } )
263284
264285 test ( 'skips setting NEXTAUTH_URL when next-auth package is not found' , async ( ) => {
0 commit comments