Skip to content

Remove setTimeout from weather tests #19

@kyrivanderpoel

Description

@kyrivanderpoel

This code in src/plugins/weather/plugin.js is responsible for the funky setTimeout function in the final test of the corresponding test file:

this.call_weather_api(message, url, config);
return true;

The bug is due to call_weather_api using a Promise, and it is handled with setTimeout in this code from test/test_weather.js:

let axios_mock = build_axios_mock(response);
let plugin = new WeatherPlugin({
  openweather_api_key: 'not-a-real-api-key',
  axios: axios_mock
});

// Make sure that the string passed to message.reply is captured.
let recorded_message = '';
let message_fixture = {
  content: '!weather denton',
  author: {
    username: 'notabot'
  },
  reply: (message) => {
    recorded_message = message;
  }
};

let expected = "\nTemp: " +
               response.data.main.temp +
               " **|** Weather: " +
               response.data.weather[0].description +
               " **|** Wind: " +
               response.data.wind.speed;

plugin.handle_message(message_fixture, config_fixture);

// wait 1 seconds to make sure the message gets recorded
setTimeout(() => {
  assert.equal(recorded_message, expected);
}, 1);

recorded_message isn't getting set by the call_weather_api in time for the test to complete because message.reply is inside of an Promise.then resolving. The handle_message function ends up returning before the value of recorded message (part of the message_fixture) before it is set, causing the test to fail.

This test should be implemented in a way that it will run without the use of setTimeout.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions