@@ -5,6 +5,7 @@ jest.mock('../plugin/src/helpers/utils', () => {
5
5
}
6
6
} )
7
7
8
+ const Chance = require ( 'chance' )
8
9
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir, readJson } = require ( 'fs-extra' )
9
10
const path = require ( 'path' )
10
11
const process = require ( 'process' )
@@ -29,6 +30,7 @@ const { getRequiredServerFiles, updateRequiredServerFiles } = require('../plugin
29
30
const { dirname } = require ( 'path' )
30
31
const { getProblematicUserRewrites } = require ( '../plugin/src/helpers/verification' )
31
32
33
+ const chance = new Chance ( )
32
34
const FIXTURES_DIR = `${ __dirname } /fixtures`
33
35
const SAMPLE_PROJECT_DIR = `${ __dirname } /../demos/default`
34
36
const constants = {
@@ -221,8 +223,31 @@ describe('onBuild()', () => {
221
223
} )
222
224
} )
223
225
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
+
224
249
test ( 'sets NEXTAUTH_URL when next-auth package is detected' , async ( ) => {
225
- const mockSiteUrl = 'https://my-netlify-site.app'
250
+ const mockSiteUrl = chance . url ( )
226
251
227
252
// Value represents the main address to the site and is either
228
253
// a Netlify subdomain or custom domain set by the user.
@@ -237,12 +262,10 @@ describe('onBuild()', () => {
237
262
const config = await getRequiredServerFiles ( netlifyConfig . build . publish )
238
263
239
264
expect ( config . config . env . NEXTAUTH_URL ) . toEqual ( mockSiteUrl )
240
-
241
- delete process . env . URL
242
265
} )
243
266
244
267
test ( 'includes the basePath on NEXTAUTH_URL when present' , async ( ) => {
245
- const mockSiteUrl = 'https://my-netlify-site.app'
268
+ const mockSiteUrl = chance . url ( )
246
269
process . env . URL = mockSiteUrl
247
270
248
271
await moveNextDist ( )
@@ -257,8 +280,6 @@ describe('onBuild()', () => {
257
280
const config = await getRequiredServerFiles ( netlifyConfig . build . publish )
258
281
259
282
expect ( config . config . env . NEXTAUTH_URL ) . toEqual ( `${ mockSiteUrl } /foo` )
260
-
261
- delete process . env . URL
262
283
} )
263
284
264
285
test ( 'skips setting NEXTAUTH_URL when next-auth package is not found' , async ( ) => {
0 commit comments