Cypress w/graphql - having issues getting AUTH with testing via UI. Better way to stub mutation?

前端 未结 2 761
旧时难觅i
旧时难觅i 2021-01-22 13:50

So, if I am testing pages in a vacuum without much interaction with the backend, it works great. I am having issues with actually interacting with my UI if it hits any type of s

相关标签:
2条回答
  • 2021-01-22 14:31

    I didn't do the stub and all those, as you were asking how the mutation would work with cy.request in my other post. I did it this way and it just basically works. Hopefully this would help

    I created a const first though

    export const join_graphQL = (query, extra={}) => {
      return `mutation {
          ${query}(join: { email: "${extra.email}", id: "${extra.id}" }) {
                  id, name, email
              }    
      }`
    };
    

    request config const

    export const graphqlReqConfig = (body={}, api=graphQlapi, method='post') => {
      return {
          method,
          body,
          url: api,
          failOnStatusCode: false
      }
    };
    

    mutation query with cy.request

    const mutationQuery = join_graphQL('mutationName', {
      email: "email",
      id: 38293
    });
    
    cy.request(graphqlReqConfig({
      query: mutationQuery
    })).then((res) => {
      const data = res.body.data['mutationName'];  // your result
    });
    

    hopefully it's not too messy to see.

    basically the fields need to be string such as "${extra.email}" else it will give you error. Not sure how the graphql works deeply but if I just do ${extra.email} I would get an error which I forgot what error it was.

    0 讨论(0)
  • 2021-01-22 14:39

    Here's a simpler way of handling a mutation with cy.request

     const mutation = `
       mutation {
         updateUser(id: 1, firstName: "test") {
           firstName
           lastName
           id
           role
         }
       }`
    
     cy.request({
        url: url,
        method: 'POST',
        body: { query: mutation },
        headers: {
          Authorization: `Bearer ${token}`,
        },
      })
    
    0 讨论(0)
提交回复
热议问题