6

I need to fetch some real data in my tests from a remote url. I Superagent is not being mocked. I have done that by including node_modules/superagent/ in unmockedModulePathPatterns.

This is the file I am trying to test, the .end() function is never called.

This is my test, which fails with a timeout error.

jest.dontMock("../Stocks.js");
jest.dontMock("superagent");

describe("Stock Actions", () => {
  var makeRequest = require('../Stocks')

  pit("doesn't crash", function () {
    var promise = makeRequest("Hello World")

    promise.then(function (str) {
      expect(str).toBe("yay");
    });

    return promise;
  });
});

And this is the module it's trying to test:

import Reflux     from 'reflux';
import request    from 'superagent';

console.log("request-superagent", request)

const makeRequest = Reflux.createAction({ asyncResult: true });

const Store = Reflux.createStore({
  init() {
    this.listenTo(makeRequest, 'onMakeRequest');
  },

  onMakeRequest(url) {

    request('GET', 'http://api.example.com/list/')
        .end(function (err, res) {
          console.log("res.text", res.text);
          if (!res.ok) {
            makeRequest.failed("aw");
          }

          makeRequest.completed("yay");
        });
  }
});

module.exports = makeRequest;

How do I use superagent in jest-cli?

  • Aw, my question is going to die. Please feel free to comment on the question, like how I could make it better, or resources that may help me solve my problem. –  May 23 '15 at 20:32
  • Apologies for the grammar, I wrote this question when I was very tired and frustrated. But it's still relevant to me, being able to reliably not mock objects is quite vital for me to be able to use Jest-CLI –  May 23 '15 at 22:29
  • Do I not get an achievement for having a bounty expire on me, with no answers? –  May 25 '15 at 02:15
  • 1. You didn't post your error 2. If it is timeout, you probably aren't mocking superagent, and superagent is timing out 3. Was your question about dontMock or about superagent? 4. If you fixed it, please post an answer. – dcorking Nov 11 '17 at 13:04
  • I explained it was a timeout error. –  Nov 12 '17 at 13:10
  • :) I was looking for the error message and stack trace, to show _which_ function timed out. – dcorking Nov 13 '17 at 08:45

0 Answers0