How to use superagent when testing in Jest-CLI?
Asked Answered
L

0

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?

Lynching answered 14/5, 2015 at 14:23 Comment(6)
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.Lynching
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-CLILynching
Do I not get an achievement for having a bounty expire on me, with no answers?Lynching
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.Pop
I explained it was a timeout error.Lynching
:) I was looking for the error message and stack trace, to show which function timed out.Pop

© 2022 - 2024 — McMap. All rights reserved.