Reputation: 957
I'm setting up some first playwright tests for my nextjs project. I already have environment variables in my .env.local
and I'd like to pull them into my test env.
I'm looking at the documentation and I see that I can add require("dotenv").config();
to my playwright.config.js
but nothing is happening when I do that (the scripts are erroring out because of undefined
.
I tried both calling process.env.foo
directly within the script, and also adding a use: {foo: process.env.FOO}
clause to the playwright.config.js
and moving my variables to .env
file instead of .env.local
but nothing worked.
Help would be much appreciated! thank you.
Upvotes: 3
Views: 8825
Reputation: 5981
npm install @next/env
Next.js has a first party @next/env
package that can be used in the playwright.config.ts
file to ensure Playwright knows about the desired environment.
import { loadEnvConfig } from "@next/env";
loadEnvConfig(process.cwd());
Upvotes: 0
Reputation: 957
After reading using dotenv path with JEST I found the solution is to configure the require
statement:
npm install --save-dev dotenv
.env.local
- set the varsFOO=bar
require("dotenv").config({ path: "./.env.local" });
console.log(process.env.FOO); // prints "bar"
test("env", async ({ page }) => {
console.log(process.env.FOO); // also prints "bar"
})
Upvotes: 8