Skellington-Closet / slack-mock

A Slack API mocker for Slack bot integration tests.
MIT License
64 stars 15 forks source link

Can't stop RTM socket #25

Closed finferflu closed 6 years ago

finferflu commented 6 years ago

Hi,

I'm trying to use this with Jasmine, and so far I'm only mocking a Web API response. I can see that there is a socket listening on localhost:9001, and neither slackMock.reset(), nor slackMock.rtm.stopServeR() seem to do the trick. Jasmine hangs indefinitely. Is there anything that I might be missing?

This is my current test:

const slackMock = require('slack-mock')({ logLevel: 'silly' })
const slackNotify = require('../helpers/slack-notify')

describe('slackNotify.findChannelId()', () => {
  afterEach(() => {
    slackMock.reset()
  })
  it('returns a channel ID for an existent channel', done => {
    slackMock.web.addResponse({
      url: 'https://slack.com/api/channels.list',
      statusCode: 200,
      body: {
        ok: true,
        channels: [
          {
            id: '1',
            name: 'channel1'
          },
          {
            id: '2',
            name: 'channel2'
          }
        ]
      }
    })

    return slackNotify
      .findChannelId('channel1')
      .then(id => {
        expect(id).toBe('1')
      })
      .then(() => {
        done()
      })
      .catch(e => {
        console.log(e)
        done()
      })
  })
})

Thank you.

finferflu commented 6 years ago

I have fixed the issue by adding server.close() to rtm.stopServer(), as follows:

rtm.stopServer = function(token) {
  return new Promise((resolve, reject) => {
    const wss = wssServers.get(token)
    if (!wss) {
      server.close()
      return resolve()
    }

    wss.close(err => {
      if (err) {
        logger.debug(`there was an error closing server ${token}`, err)
        return reject(err)
      }

      logger.debug(`server ${token} closed`)
      wssServers.delete(token)
      server.close()
      resolve()
    })
  })
}