@@ -378,6 +378,67 @@ describe("validateNextConfigOverride", () => {
378378 } ) ;
379379} ) ;
380380
381+ describe ( "next config restore" , ( ) => {
382+ let tmpDir : string ;
383+ const nextConfigOriginalBody = `
384+ // @ts-check
385+
386+ /** @type {import('next').NextConfig} */
387+ const nextConfig = {
388+ /* config options here */
389+ }
390+
391+ module.exports = nextConfig
392+ ` ;
393+ const nextConfigBody = `
394+ // This file was automatically generated by Firebase App Hosting adapter
395+ const fahOptimizedConfig = (config) => ({
396+ ...config,
397+ images: {
398+ ...(config.images || {}),
399+ ...(config.images?.unoptimized === undefined && config.images?.loader === undefined
400+ ? { unoptimized: true }
401+ : {}),
402+ },
403+ });
404+
405+ const config = typeof originalConfig === 'function'
406+ ? async (...args) => {
407+ const resolvedConfig = await originalConfig(...args);
408+ return fahOptimizedConfig(resolvedConfig);
409+ }
410+ : fahOptimizedConfig(originalConfig);
411+ ` ;
412+
413+ beforeEach ( ( ) => {
414+ tmpDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "test-overrides" ) ) ;
415+ } ) ;
416+
417+ it ( "handle no original config file found" , async ( ) => {
418+ const { restoreNextConfig } = await importOverrides ;
419+ fs . writeFileSync ( path . join ( tmpDir , "next.config.mjs" ) , nextConfigBody ) ;
420+ await restoreNextConfig ( tmpDir , "next.config.mjs" ) ;
421+
422+ const restoredConfig = fs . readFileSync ( path . join ( tmpDir , "next.config.mjs" ) , "utf-8" ) ;
423+ assert . equal ( restoredConfig , nextConfigBody ) ;
424+ } ) ;
425+
426+ it ( "handle no config file found" , async ( ) => {
427+ const { restoreNextConfig } = await importOverrides ;
428+ assert . doesNotReject ( restoreNextConfig ( tmpDir , "next.config.mjs" ) ) ;
429+ } ) ;
430+
431+ it ( "original config file restored" , async ( ) => {
432+ const { restoreNextConfig } = await importOverrides ;
433+ fs . writeFileSync ( path . join ( tmpDir , "next.config.mjs" ) , nextConfigBody ) ;
434+ fs . writeFileSync ( path . join ( tmpDir , "next.config.original.mjs" ) , nextConfigOriginalBody ) ;
435+ await restoreNextConfig ( tmpDir , "next.config.mjs" ) ;
436+
437+ const restoredConfig = fs . readFileSync ( path . join ( tmpDir , "next.config.mjs" ) , "utf-8" ) ;
438+ assert . equal ( restoredConfig , nextConfigOriginalBody ) ;
439+ } ) ;
440+ } ) ;
441+
381442// Normalize whitespace for comparison
382443function normalizeWhitespace ( str : string ) {
383444 return str . replace ( / \s + / g, " " ) . trim ( ) ;
0 commit comments