proces.env变量始终不确定,在其他项目中效果很好
项目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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
测试中它始终是
不确定的
的原因是Process
是nodejs对象。它在运行测试的浏览器中不可用。但是,有两种方法可以使用cypress使用
dotenv
文件:Cypress-dotenv
插件它将您的nodejs.env
文件vars vars vars to to cypress env变量。插件/index.js
文件中手动映射它们:The reason it's always
undefined
in the test is becauseprocess
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:cypress-dotenv
plugin and it'll map your nodejs.env
file vars to the Cypress env variables.plugins/index.js
file: