NodeJS:无法通过带有 start-server-and-test 的包脚本设置 PERCY_TOKEN
NodeJS: NOT able to set PERCY_TOKEN via package script with start-server-and-test
根据官方 guideline from percy,我一直在努力在本地测试 运行 上设置 PERCY_TOKEN
并通过 yarn
.[=20= 触发它]
指南说:
$ PERCY_TOKEN=aaabbbcccdddeeefff PERCY_BRANCH=local npm test
我现在的尝试是这样的:
package.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 解决方案
package.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. This is very similar to what .
此处 &&
由 shell 处理,因此您的命令 yarn percy:local && yarn start:server percy:exec
是 运行 作为 2 个独立的子进程。这意味着 yarn percy:local
运行s 在一个进程中并根据需要在其上下文中设置 env 变量但是第二个进程 运行ning yarn start:server percy:exec
不知道 env 变量由进程 1 设置。
让我们看看 OS X 是如何处理的:
- 和
&&
sh -c 'PERCY_TOKEN=asdfasdf; PERCY_BRANCH=local' && sh -c 'echo $PERCY_TOKEN'
这什么都不打印
- 没有
&&
sh -c 'PERCY_TOKEN=asdfasdf; PERCY_BRANCH=local echo $PERCY_TOKEN'
这会打印 asdfasdf
我认为删除 cy:percy:local
中的 &&
应该可以解决您的问题。
编辑:评论中讨论的针对您的特定情况的解决方案
如本答案第二点所述:Whosebug。com/a/37141993/8266093 我建议您 运行 此脚本的格式 ENV_KEY1=value1 ENV_KEY2=value2 command
在您的情况下变为 PERCY_TOKEN=$(grep 'PERCY_TOKEN.*' .env | sed 's/.*=//') PERCY_BRANCH=local yarn percy exec cypress run
.除此之外,如果 运行单独设置它不适合您,您也可以添加 start-server yarn 命令。
根据官方 guideline from percy,我一直在努力在本地测试 运行 上设置 PERCY_TOKEN
并通过 yarn
.[=20= 触发它]
指南说:
$ PERCY_TOKEN=aaabbbcccdddeeefff PERCY_BRANCH=local npm test
我现在的尝试是这样的:
package.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 解决方案
package.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. This is very similar to what
此处 &&
由 shell 处理,因此您的命令 yarn percy:local && yarn start:server percy:exec
是 运行 作为 2 个独立的子进程。这意味着 yarn percy:local
运行s 在一个进程中并根据需要在其上下文中设置 env 变量但是第二个进程 运行ning yarn start:server percy:exec
不知道 env 变量由进程 1 设置。
让我们看看 OS X 是如何处理的:
- 和
&&
sh -c 'PERCY_TOKEN=asdfasdf; PERCY_BRANCH=local' && sh -c 'echo $PERCY_TOKEN'
这什么都不打印
- 没有
&&
sh -c 'PERCY_TOKEN=asdfasdf; PERCY_BRANCH=local echo $PERCY_TOKEN'
这会打印 asdfasdf
我认为删除 cy:percy:local
中的 &&
应该可以解决您的问题。
编辑:评论中讨论的针对您的特定情况的解决方案
如本答案第二点所述:Whosebug。com/a/37141993/8266093 我建议您 运行 此脚本的格式 ENV_KEY1=value1 ENV_KEY2=value2 command
在您的情况下变为 PERCY_TOKEN=$(grep 'PERCY_TOKEN.*' .env | sed 's/.*=//') PERCY_BRANCH=local yarn percy exec cypress run
.除此之外,如果 运行单独设置它不适合您,您也可以添加 start-server yarn 命令。