javascript - NodeJS:无法通过带有 start-server-and-test 的包脚本设置 PERCY_TOKEN
问题描述
我一直在努力根据percyPERCY_TOKEN
的官方指南设置本地测试运行并通过.yarn
该指南说:
$ PERCY_TOKEN=aaabbbcccdddeeefff PERCY_BRANCH=local npm test
我现在的尝试是这样的:
包.json
...
"scripts": {
"start": "react-scripts -r @cypress/instrument-cra start",
"start:silent": "BROWSER=none yarn start",
"start:server": "start-server-and-test start:silent http://localhost:3000",
"build": "react-scripts build",
"eject": "react-scripts eject",
"envGenerateExample": "cat .env | sed 's/=.*/=/g' > .env.example",
"jest:test": "react-scripts test --env=jest-environment-jsdom-sixteen",
"cy:run": " 'yarn start:server './node_modules/.bin/cypress run'",
"cy:open": "yarn start:server './node_modules/.bin/cypress open'",
"cy:ci": "yarn start:server cy:chrome",
"cy:chrome": "cypress run --browser chrome --record",
"percy:exec": "yarn percy exec -- cypress run",
"cy:percy": "yarn start:server percy:exec",
"percy:local": "PERCY_TOKEN=$(grep 'PERCY_TOKEN.*' .env | sed 's/.*=//'); PERCY_BRANCH=local;",
"cy:percy:local": "yarn percy:local && yarn start:server percy:exec"
},
...
$ yarn cy:percy:local
Compiled successfully!
You can now view playground in the browser.
Local: http://localhost:3000
On Your Network: http://192.168.1.163:3000
Note that the development build is not optimized.
To create a production build, use yarn build.
...
> playground@0.2.0 percy:exec /Users/norfeldt/Repos/playground
> yarn percy exec -- cypress run
warning From Yarn 1.0 onwards, scripts don't require "--" for options to be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.
$ /Users/norfeldt/Repos/playground/node_modules/.bin/percy exec cypress run
› Warning: Skipping visual tests. PERCY_TOKEN was not provided.
...
TL;DR 解决方案
包.json
...
"scripts": {
"start": "react-scripts -r @cypress/instrument-cra start",
"start:silent": "BROWSER=none yarn start",
"start:server": "start-server-and-test start:silent http://localhost:3000",
...
"percy:local": "PERCY_TOKEN=$(grep 'PERCY_TOKEN.*' .env | sed 's/.*=//') PERCY_BRANCH=local yarn start:server 'percy exec cypress run'"
},
...
解决方案
yarn 内部用于sh
执行脚本中给出的命令(cmd
在 windows 的情况下) - Source。这与npm 所做的非常相似。
Here &&
is handled by a shell, so your command yarn percy:local && yarn start:server percy:exec
is run as 2 separate child processes. This means yarn percy:local
runs in a process and sets the env variables as desired in its context but the second process which is running yarn start:server percy:exec
has no idea about the env variables set by process 1.
Lets see how OS X handles this:
- With
&&
sh -c 'PERCY_TOKEN=asdfasdf; PERCY_BRANCH=local' && sh -c 'echo $PERCY_TOKEN'
This prints nothing
- Without
&&
sh -c 'PERCY_TOKEN=asdfasdf; PERCY_BRANCH=local echo $PERCY_TOKEN'
This prints asdfasdf
I think removing the &&
in the cy:percy:local
should fix your issue.
Edit: Solution for your particular situation as discussed in comments
As explained in the 2nd point of this answer: stackoverflow.com/a/37141993/8266093 I would suggest you run this script in the format ENV_KEY1=value1 ENV_KEY2=value2 command
which in your case becomes PERCY_TOKEN=$(grep 'PERCY_TOKEN.*' .env | sed 's/.*=//') PERCY_BRANCH=local yarn percy exec cypress run
. Along with this, you can add your start-server yarn command as well if running it separately is not a solution for you.
推荐阅读
- angular - 为什么它在尝试在 angular6 中查找引导代码时显示错误“错误”?
- dart - 字节数据操作缓慢
- ruby - 使用“执行”编写厨师 rspec 测试
- .net - 如何从同一项目中的 Angular 4 页面导航到 asp.net Web 表单
- apache-spark - 胶水dpu和最大并发的关系
- ckeditor - Indentblock CKEditor (4) 插件导致包含边距属性的样式不会出现在样式组合中
- html - 不能将单选按钮垂直放置在中间
- java - 如何通过 Gradle Builder 将项目作为 lib 导出到 JAR 文件?
- database-design - Cassandra 在表中支持的最大列数
- c# - 如何从 URI 源(不是 StorageFile)获取 MP3 元数据/ID3 标签?