Skip to content

Commit 24a9465

Browse files
committed
unit test
1 parent 68eaa28 commit 24a9465

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

packages/@apphosting/adapter-nextjs/src/overrides.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
382443
function normalizeWhitespace(str: string) {
383444
return str.replace(/\s+/g, " ").trim();

packages/@apphosting/adapter-nextjs/src/overrides.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ export async function validateNextConfigOverride(
143143
}
144144

145145
/**
146-
*
146+
* Restores the user's original Next Config file (next.config.original.[ts|js|mjs])
147+
* to leave user code the way we found it.
147148
*/
148149
export async function restoreNextConfig(projectRoot: string, nextConfigFileName: string) {
149150
// Check if the file exists in the current working directory

0 commit comments

Comments
 (0)