Skip to content

Commit

Permalink
fix: exclude .env.local files for test mode (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Jan 6, 2025
1 parent 0c45b0b commit d488d86
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/bright-parents-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@opennextjs/cloudflare": patch
---

fix: exclude `.env.local` files for `test` mode

Aligns with the Next.js behavior of not extracting variables from the `.env.local` file in test environments.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("extractProjectEnvVars", () => {
mockFs({
".env": "ENV_VAR=value",
".env.local": "ENV_LOCAL_VAR=value",
".env.test": "ENV_TEST_VAR=value",
".env.test.local": "ENV_TEST_LOCAL_VAR=value",
".env.development": "ENV_DEV_VAR=value",
".env.development.local": "ENV_DEV_LOCAL_VAR=value",
".env.production": "ENV_PROD_VAR=value",
Expand Down Expand Up @@ -67,4 +69,13 @@ describe("extractProjectEnvVars", () => {
ENV_VAR: "value",
});
});

it("should exclude .env.local files when extracting test env vars", () => {
const result = extractProjectEnvVars("test", options);
expect(result).toEqual({
ENV_TEST_LOCAL_VAR: "value",
ENV_TEST_VAR: "value",
ENV_VAR: "value",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function readEnvFile(filePath: string) {
*
* Merged variables respect the following priority order.
* 1. `.env.{mode}.local`
* 2. `.env.local`
* 2. `.env.local` (when mode is not equal to `test`)
* 3. `.env.{mode}`
* 4. `.env`
*
Expand All @@ -27,7 +27,7 @@ function readEnvFile(filePath: string) {
* the env files at the root of the monorepo.
*/
export function extractProjectEnvVars(mode: string, { monorepoRoot, appPath }: BuildOptions) {
return [".env", `.env.${mode}`, ".env.local", `.env.${mode}.local`]
return [".env", `.env.${mode}`, ...(mode !== "test" ? [".env.local"] : []), `.env.${mode}.local`]
.flatMap((fileName) => [
...(monorepoRoot !== appPath ? [readEnvFile(path.join(monorepoRoot, fileName))] : []),
readEnvFile(path.join(appPath, fileName)),
Expand Down

0 comments on commit d488d86

Please sign in to comment.