【问题标题】:Setting headers for JEST integration testing on Apollo在 Apollo 上为 JEST 集成测试设置标头
【发布时间】:2022-08-10 09:30:39
【问题描述】:

嗨,我的代码遇到错误,这是代码。 我无法在 Apollo 的 executeOperation 函数中设置标头,因为我需要 accessToken 来运行下一个突变或查询。 loginToken 正在设置中,我可以检索它的值。但是,当我在查询 \"getClientConfiguration\" 之前设置标题时,由于标题,它给了我未经授权的响应。

const { createServerWithSchema } = require(\'../../gql/server\')

let loginToken;

describe(\'Space test suite\', () => {
  
  it(\'returns login token\', async () => {
    const testServer = await createServerWithSchema()

    const result = await testServer.executeOperation({
        query: `
        query loginFromEmail{
            loginFromEmail(
                email:\"qa+stadiumexperiencehost@pavemint.com\"
                password:\"Password1\"
            )
            {
                accessToken
            }
        }
    `
    })

    
    const { accessToken } = result.data.loginFromEmail
    loginToken = accessToken
    expect(result.errors).toEqual(undefined)
  })


  it(\'returns clientConfiguration\', async () => {
    const testServer = await createServerWithSchema()

    const result = await testServer.executeOperation({
        http: {
            headers : {
                \"Authorization\": `Bearer ${loginToken}`
            }
        },
        query: \'query getClientConfiguration { getClientConfiguration { GOOGLE_MAPS_API_KEY } }\',
    })
    
    console.log(\"result\", result)
  })
})

    标签: jestjs


    【解决方案1】:

    为了

    在 Apollo 上为 JEST 集成测试设置标头

    Here is the answer

    【讨论】:

      猜你喜欢
      • 2021-09-02
      • 2019-03-08
      • 1970-01-01
      • 2021-04-07
      • 1970-01-01
      • 2014-03-29
      • 1970-01-01
      • 2012-08-01
      • 2012-07-10
      相关资源
      最近更新 更多