1

I confuse why I get different result when try to unit test a case using await async and .then in javascript

so here it is my await code

 it.only('should successfully return property data based on userRef', async() => {
 const result = await server.executeOperation({
    query: GET_LISTINGS,
    variables: {
      filter: {
        userRef,
      },
    },
  });
  console.log('await-->', result);
 }

the output of await :

await--> {
  http: { headers: Headers { [Symbol(map)]: [Object: null prototype] {} } },
  errors: undefined,
  data: [Object: null prototype] {
    getListings: [Object: null prototype] { edges: [Array] }
  },
  extensions: undefined
}

and here it is my .then code

it.only('should successfully return property data based on userRef', () => {
server
    .executeOperation({
      query: GET_LISTINGS,
      variables: {
        filter: {
          userRef,
        },
      },
    })
    .then((result) => {
      console.log('-->then', result);
    });
  }

the output of .then

-->then {
      http: { headers: Headers { [Symbol(map)]: [Object: null prototype] {} } },
      errors: undefined,
      data: [Object: null prototype] {
        getListings: [Object: null prototype] { edges: [] }
      },
      extensions: undefined
    }

the different is when I use await I get the data but when I use .then I got 0 data , what i mean by 0 data is in egdes properties it contain [] which mean 0 data

bramasta vikana
  • 274
  • 1
  • 12
  • What is the userRef value? – Manos Kounelakis Jan 05 '22 at 07:54
  • its randomString to mock the userRef object id @ManosKounelakis – bramasta vikana Jan 05 '22 at 07:56
  • yeay i know but how that possible , should it work if we use .then in a promise ? @pilchard – bramasta vikana Jan 05 '22 at 07:57
  • Your two snippets are not fully equivalent. Does you `.then` test still work if you write `it.only('should successfully return property data based on userRef', () => { return server.executeOperation({ ....` (adding `return` in front of `server.executeOperation`)? – t.niese Jan 05 '22 at 08:10
  • it work @t.niese but can you give me a bit explanation of this? – bramasta vikana Jan 05 '22 at 08:14
  • 4
    In your `then` case you don't return the promise chain created so the test does not wait and thus is finished before `server.executeOperation(` is finished, so another test might interfere and could have been the reason that `executeOperation` succeeds. – t.niese Jan 05 '22 at 08:18
  • And you are sure that this is the only thing you changed between the `executeOperation` returning data and not returning anything? – t.niese Jan 05 '22 at 08:19
  • Is there any chance your userRef changes at sometime? – Manos Kounelakis Jan 05 '22 at 08:41
  • @t.niese oh i seee thank youu for the explaination – bramasta vikana Jan 05 '22 at 08:56
  • @t.niese yes only userRef i changed – bramasta vikana Jan 05 '22 at 08:59
  • @ManosKounelakis yes, but i test it in another differenct test case – bramasta vikana Jan 05 '22 at 08:59
  • 1
    @bramastavikana for propper debugging you should only change one thing at a time. The problem is (with the `return` in front of `server.executeOperation(` for your `then` case) both `then` and `await` should be equivalent, so something else you changed is likely to be the problem. If only changing this particular test case and nothing else form `then` to `await` still results in different results, then the problem is also somewhere else in your code. – t.niese Jan 05 '22 at 09:18
  • @t.niese okayy thank you so much – bramasta vikana Jan 05 '22 at 09:22

0 Answers0