2

I have problem with api-testing with jest

What is the current behavior?

 Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.
      at ../../../../Users/chhoeurng.sakona/AppData/Roaming/npm/node_modules/jest-cli/node_modules/jest-jasmine2/build/queue_runner.js:68:21

My current code

it ('GET should return a status of 200 OK', function (done) {
        frisby
            .get('url-api')
            .expect('status', 200)
            .done(done);
    });

What is the current behavior? It should work normally and no error.

Please provide your exact Jest configuration I do not have configuration

Run npx envinfo --preset jest in your project directory and paste the results here

 System:
    OS: Windows 10
    CPU: x64 Intel(R) Core(TM) i7-7700 CPU @ 3.60GHz
  Binaries:
    Node: 8.11.1
    Yarn: Not Found
    npm: 5.6.0
   jest v22.4.3
Andreas Köberle
  • 88,409
  • 51
  • 246
  • 277

1 Answers1

0

You need to return a promise or use an async function when woking with promises:

it ('GET should return a status of 200 OK', async() => {
        await frisby
            .get('url-api')
            .expect('status', 200)

    });

or

it ('GET should return a status of 200 OK', function () {
        return frisby
            .get('url-api')
            .expect('status', 200)

    });

Also have a look at the docs

Andreas Köberle
  • 88,409
  • 51
  • 246
  • 277
  • Hi Andreas, I still got that error when I use your second code. The first block of code is not correct. Please could you help for this. – Sakuna Chheourng Apr 10 '18 at 08:45
  • Now I try to use this change this line then it works `it ('GET should return a status of 200 OK', async (done) => {` – Sakuna Chheourng Apr 10 '18 at 08:52
  • Could you help me, I do not know where my code wrong, and it still turn timeout. `const frisby = require('frisby'); const Joi = frisby.Joi; // Frisby exports Joi for convenience on type assersions it ('Should sakona return a status of 200', function (done) { return frisby .get('url-api') .expect('status', 200) .expect('json', 'status', '3') .expect('jsonTypes', 'merchants*', { // Assert *each* object in 'items' array 'uid': Joi.number().required(), 'category': Joi.string().required(), }) .done(done); ` – Sakuna Chheourng Apr 11 '18 at 09:33
  • the best way is to dont use `done` after all, as it is not needed when working with async function as described in my answer. I will update the code to make it more clear – Andreas Köberle Apr 11 '18 at 09:39
  • Thank you so much – Sakuna Chheourng Apr 19 '18 at 07:11