proces.env变量始终不确定,在其他项目中效果很好

发布于 2025-02-10 15:48:58 字数 826 浏览 1 评论 0原文

项目A(工作):.

cy.request(`${Cypress.env('mainnet')}api?module=account&action=txlist&address=${ethWallet}&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=${process.env.apiKey}`).then((response) => {

ENV文件:

apiKey=YOUR_KEY_HERE

项目B(不工作):.

const key = process.env.apiKey
const token = process.env.authToken

Cypress.Commands.add('createBoard', (name) => {
    cy
      .request('POST', 'boards', { name, key: process.env.apiKey, token: process.env.authToken })
      .then(({ body }) => {
        Cypress.env('boards').push(body)
      })
  })

ENV文件:

apiKey=MY_KEY
authToken=MY_TOKEN

两个项目都具有相同的软件包和正确的.env文件设置。那么,为什么在项目B中,我会继续不确定呢?这根本没有意义。

Project A (working):

cy.request(`${Cypress.env('mainnet')}api?module=account&action=txlist&address=${ethWallet}&startblock=0&endblock=99999999&page=1&offset=10&sort=asc&apikey=${process.env.apiKey}`).then((response) => {

.env File:

apiKey=YOUR_KEY_HERE

Project B (not working):

const key = process.env.apiKey
const token = process.env.authToken

Cypress.Commands.add('createBoard', (name) => {
    cy
      .request('POST', 'boards', { name, key: process.env.apiKey, token: process.env.authToken })
      .then(({ body }) => {
        Cypress.env('boards').push(body)
      })
  })

.env File:

apiKey=MY_KEY
authToken=MY_TOKEN

Both projects have the same packages and a correct .env file setup. So why in project B do I keep getting undefined? It makes no sense at all.

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

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

发布评论

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

评论(1

不喜欢何必死缠烂打 2025-02-17 15:48:58

测试中它始终是不确定的的原因是Process是nodejs对象。它在运行测试的浏览器中不可用。

但是,有两种方法可以使用cypress使用dotenv文件:

  1. 使用插件 Cypress-dotenv插件它将您的nodejs .env文件vars vars vars to to cypress env变量。
  2. 插件/index.js文件中手动映射它们:
require('dotenv').config()

module.exports = (on, config) => {
  config.env.apiKey = process.env.apiKey
  config.env.authToken = process.env.authToken
  return config
}

The reason it's always undefined in the test is because process is a nodejs object. It's not available in the browser where the tests are running.

There's two ways to use a dotenv file with Cypress though:

  1. Use the plugin cypress-dotenv plugin and it'll map your nodejs .env file vars to the Cypress env variables.
  2. Map them manually in the plugins/index.js file:
require('dotenv').config()

module.exports = (on, config) => {
  config.env.apiKey = process.env.apiKey
  config.env.authToken = process.env.authToken
  return config
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文