jestjs - how to parametrize test execution from cli in ci?

ε祈祈猫儿з 提交于 2020-01-16 19:33:08

问题


i have 4 environments :

  • dev (developers area)
  • test (test area)
  • preprod (pre production environment)
  • production (production environment)

these environments needs different configuration to execute tests (differents urls, usernames, assets, and so on).

how to pass there configurations to jest as a parameter in continous integration?


回答1:


As you can read here, jest would not permits to pass custom arguments you can use to handle custom configuration loaded at runtime.

i propose a workaround working for me.

1) create a configuration file, e.g. config.js 2) edit config.js and exports modules switching for the environment

switch (env) {
    case "test":
        module.exports = {
            baseUrl: 'https://test.website.com'
        }
        break;
        case "production":
        module.exports = {
            baseUrl: 'https://production.website.com'

}

3) create a javascript files for every environment you need

  • test-configuration.js
  • production-configuration.js

4) edit these files writing in the environment variables

for example test-configuration.js will be

process.env.ENVIRONMENT = "test"

5) edit config.js in order to get access to process.env

let env = process.env.ENVIRONMENT || "test" //test will be the default
switch (env) {
    case "test":
        module.exports = {
            baseUrl: 'https://test.website.com'
        }
        break;
        case "production":
        module.exports = {
            baseUrl: 'https://production.website.com'

}

6) load configuration for you test files as it was a static file

const config = require('./config.js')

7) use jest setupFiles to add a setupFiles that load the environment variables.

for example, running

jest --setupFiles=./test-configuration.js

jest will load the test-configuration.js file that will set "test" on the "process.env.ENVIRONMENT" variables, so config.js file will "switch" on the "test" environment and all your test will use it.

so now you can (or CI can) loads configuration as needed.



来源:https://stackoverflow.com/questions/59502578/jestjs-how-to-parametrize-test-execution-from-cli-in-ci

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!