BedrockStreaming/superagent-mock

Specs got stuck in the end

mauricioklein opened this issue ยท 13 comments

I'm using superagent-mock to mock my requests and, even returning the correct result and the tests are passing, after all specs are done, the suite got stuck and release after 60s (probably a timeout).

Disabling superagent-mock makes all the specs to pass (hitting the real server), and the suite finishes instantly.

Below is my spec:

  describe('search triggers', () => {
    const mockObj = superagentMock(request, rules)

    describe('city search', () => {
      beforeEach(() => triggerSearchByCity())

      it('should update the state', () => {
        expect(subject.state.loaded).toEqual(true)
        expect(subject.state.data).toEqual(apiMocks.cityResponse)
      })
    })

And my mock setup rules:

 rules= [{
    pattern: 'http://api.openweathermap.org/data/2.5/weather(.*)',

    
    fixtures: function (match, params, headers, context) {
      // City search mock
      if (match[1] === '?q=Berlin%2CDE&units=metric&APPID=[MY APP ID]') {
        return cityResponse
      }

    get: function (match, data) {
      return {
        body: data
      };
    }
  }
];

And my superagent call:

  return new Promise((resolve, reject) => {
    request
      .get('http://api.openweathermap.org/data/2.5/weather')
      .query({
        q: `Berlin,DE`,
        units: 'metric',
        APPID: '[My APP ID]'
      })
      .end((err, res) => {
        if (err) { reject (err)      }
        else     { resolve(res.body) }
      })
  })

The promise above is resolved in the triggerSearchByCity() method.

UPDATE:
Here's another simpler setup that causes the same problem:
https://gist.github.com/mauricioklein/b0dd5768bce0075232729596dc71a83f

Thanks!

Hi,

You may have a case sensitive problem here ?

In your setup:
if (match[1] === '?q=Berlin%2CDE&units=metric&APPID=[MY APP ID]') {

In your call:
APPID: '[My APP ID]'

Hello, thanks for your response.

Actually, this is just placeholder, to not share my personal key.
I've checked and the APP key is right on both sides ๐Ÿ‘

Also, the mock is matching and I'm getting the valid mocked response, and all the specs are passing.
The problem is that, after all the specs run, the terminal is not released.
Only after 60s, I get the Jest message of success, and the terminal is released.
This causes my specs, that normally takes 3s, to finish in more than 1 minute.

Thanks!

Here's another (and simpler) setup that causes the same problem.
https://gist.github.com/mauricioklein/b0dd5768bce0075232729596dc71a83f

Certainly because you don't return the promise in your tests ?

it('should process success result', () =>
  getWeatherForCity('Berlin', 'DE').then(body => {
    expect(body).toEqual(apiMocks.cityResponse)
  })
)

instead of

it('should process success result', () => {
  getWeatherForCity('Berlin', 'DE').then(body => {
    expect(body).toEqual(apiMocks.cityResponse)
  })
})

Thanks for your comment, but it didn't solved the problem:

import { getWeatherForGeoloc, getWeatherForCity } from 'api'

import request from 'superagent';
import * as apiMocks from 'mocks'

describe('API', () => {
  var mock = null
  beforeEach(() => mock = require('superagent-mock')(request, apiMocks.rules))
  afterEach(() => mock.unset())

  describe('#getWeatherForCity', () => {
    it('should process success result', () =>
      getWeatherForCity('Berlin', 'DE').then(body =>
        expect(body).toEqual(apiMocks.cityResponse)
      )
    )

    it('should process failure result', () =>
      getWeatherForCity('Foo', 'Bar').then(() => {},
        err => expect(err).toEqual(Error(500))
      )
    )
  })

Any further suggestions about how to fix that?
I really have no clue why is this happening but, since I disable the mock library, the tests finishes instantly, I suppose could be something related to the library itself.
Thanks!

I am experiencing the exact same thing after upgrading..

  • superagent 3.5.2 => 3.8.1
  • superagent-mock 2.0.1 => 3.6.0

With no other changes, I experience the same thing with all the tests completing instantly followed by everything hanging for 60 seconds.

And this is on node 8.9.1.

I have the same issue and downgrading to 2.0.1 seems to fix this.

I can't reproduce with superagent@3.8.1, superagent-mock@3.6.1, jest@20 ๐Ÿ˜’

superagent-mock is designed for functional testing environment.
I encourage you to create a manual mock of superagent with Jest:

// __mocks__/superagent.js
export default {
  get: jest.fn().mockReturnThis(),
  query: jest.fn().mockReturnThis(),
  timeout: jest.fn().mockReturnThis(),
  post: jest.fn().mockReturnThis(),
  del: jest.fn().mockReturnThis(),
  set: jest.fn().mockReturnThis(),
  end: jest.fn(),
}

I had the same issue, and switched to superagent-mocker, which handles superagent's RequestBase::then() correctly. Tests that hung Mocha with superagent-mock, no longer hung when using superagent-mocker (tests stayed exactly the same, only the before/after hooks were modified to use the other mocking library).

I just started experiencing this issue, but after a bunch of digging, haven't really found the cause.

What's strange is that my current setup was lifted from a plain Node project (w/Babel because Jest does not currently support mocking modules with ESM, which was what I wrote it with) which works as a charm. The difference here is that I'm now using TypeScript and I don't really see how that can cause an issue. Even has the same docker environment and dependency versions, so it's not anything related to the runtime.

I noticed that this only happens when there's a match for a defined pattern, and removing any matching pattern will make tests go through, so the issue definitely lies within this library somewhere.

I'll update this thread if I find something just in case anyone else stumbles across this one.

Edit I was unable to figure this out and switched to nock.

y-nk commented

@fdubost not sure why issue is closed. it's still happening.

i've pinpointed to this particular moment https://github.com/M6Web/superagent-mock/blob/master/src/superagent-mock.js#L237-L242 where the setTimeout is never called ; but not sure why. It'd be great to fix it.