我想知道是否有可能有一个 .testcaferc.json 文件为所有环境提供配置?
我们有很多共享的配置,所以如果不需要的话,我真的不想为每个环境创建单独的配置文件。
像这样的东西会很棒,但似乎在 tc 文档中看不到任何提及环境的内容
{
browsers: "chrome",
concurrency: 5,
baseUrl: "http://localhost:3000",
...
env: {
dev: {
baseUrl: "https://dev.example.com",
...
},
test: {
baseUrl: "https://test.example.com",
...
},
prod: {
baseUrl: "https://example.com",
...
},
}
}
目前我们将一些额外的参数传递到脚本中,如下所示,这在我们的包文件中变得非常混乱且难以维护。
{
"test:e2e": "npm run test:cafe",
"test:e2e:dev": "npm run test:cafe -- --base-url=https://dev.example.com",
"test:e2e:test": "npm run test:cafe -- --base-url=https://test.example.com",
"test:e2e:prod": "npm run test:cafe -- --base-url=https://example.com","
}
谢谢!
您可以使用 JavaScript 配置文件 而不是 JSON 配置文件。在这种情况下,您可以在配置中编写自己的逻辑。比如你可以这样查看环境变量:
.testcaferc.js:
module.exports = {
baseUrl: process.env.dev ? 'http://localhost:3000' : 'http://localhost:3005',
customActions: {},
}
此外,您可以为不同的环境创建不同的配置文件,并为通用设置创建一个。在这种情况下,您可以使用公共配置文件中的公共设置,并从其他配置文件中导入特定的环境设置。
常用:.testcaferc.js
function resolveEnv () {
if (process.env.dev)
return './.testcaferc-dev.js'
else if (process.env.test)
return './.testcaferc-test.js'
else
return './.testcaferc-prod.js'
}
const commonConfig = {
skipJsErrors: true,
customActions: {}
}
const envConfigName = resolveEnv();
const envConfig = require(envConfigName);
module.exports = {
...commonConfig,
...envConfig,
}
.testcaferc-dev.js
module.exports = {
baseUrl: 'http://localhost:3000',
skipJsErrors: false,
}