Async setup of environment with Jest

后端 未结 3 1121
北恋
北恋 2021-01-04 14:01

Before running e2e tests in Jest I need to get an authentication token from the server.

Is it possible to do this globally one and set it somehow to the glo

3条回答
  •  被撕碎了的回忆
    2021-01-04 14:46

    Ini my case, I had to get the token for current session once. Here I used globalSetup property in jest config. Ref: https://jestjs.io/docs/en/configuration#globalsetup-string

    My jest.config:

    module.exports = {
      globalSetup: "./tests/global-setup.js",
      // ..other config... 
      globals: {
          TEST_EMAIL: 'test@test.com',
          TEST_PASSWORD: 'some_password',
      }
    }
    

    global-setup.js:

     module.exports = async function() {
      const getSessionToken = () => {
        // api call to get SessionToken
        /* return fetch() */
      }
    
      sessionToken = await getSessionToken();
      process.env.SESSION_TOKEN = sessionToken;
     }
    
    

提交回复
热议问题