使用.env.local与剧作家

发布于 2025-02-11 06:27:41 字数 367 浏览 1 评论 0原文

我正在为我的NextJS项目设置一些剧作家测试。我的.env.local中已经有环境变量,我想将它们拉入我的测试env中。

我正在查看该文档,并且看到我可以添加require(“ dotenv”)。config();到我的playwright.config.js,但是什么也没有发生当我这样做时(由于不确定

。 :{foo:process.env.foo} 从playwright.config.js将我的变量移至.env文件而不是。 Env.Local,没有任何帮助

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2025-02-18 06:27:43

阅读使用dotenv path with Jest 我发现解决方案是为了配置require语句:

  1. .env.local
npm install --save-dev dotenv
  1. 设置vars-
FOO=bar
  1. -在playwright.config.js- 设置what what env文件用于
require("dotenv").config({ path: "./.env.local" });
console.log(process.env.FOO); // prints "bar"

  1. 规格
test("env", async ({ page }) => {
  console.log(process.env.FOO); // also prints "bar"
})

After reading using dotenv path with JEST I found the solution is to configure the require statement:

  1. install the dotenv package (the one that comes with next.js isn't loaded)
npm install --save-dev dotenv
  1. In .env.local - set the vars
FOO=bar
  1. In playwright.config.js - set which env file to use
require("dotenv").config({ path: "./.env.local" });
console.log(process.env.FOO); // prints "bar"

  1. In a spec
test("env", async ({ page }) => {
  console.log(process.env.FOO); // also prints "bar"
})
妥活 2025-02-18 06:27:43
npm install @next/env

next.js具有第一方 @next/env/env可以在playwright.config.ts文件中使用的软件包,以确保playwright知道所需的环境。

import { loadEnvConfig } from "@next/env";

loadEnvConfig(process.cwd());
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());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文