0

In one component I sent a xhr request to '/api/somecall'

I am trying to mock the HTTP request for testing purposes with MochaJS.

I went into nock but the first parameter is the domain, which cannot be fixed because we use the same app on several domains.

I though about mocking a DOM with a location.href so that the xhr would be sent to a specific domain this way in a helper at the beginning of Mocha's testing session like this (I found part of this code in the web):

// setup the simplest document possible
var doc = jsdom.jsdom('<!doctype html><html><body></body></html>', {url: "http://localhost"})

// get the window object out of the document
var win = doc.defaultView

// set globals for mocha that make access to document and window feel 
// natural in the test environment
global.document = doc
global.window = win

// take all properties of the window object and also attach it to the 
// mocha global object
propagateToGlobal(win)

// from mocha-jsdom https://github.com/rstacruz/mocha-jsdom/blob/master/index.js#L80
function propagateToGlobal (window) {
  for (let key in window) {
    if (!window.hasOwnProperty(key)) continue
    if (key in global) continue

    global[key] = window[key]
  }
}

Unfortunately it still doesn't work when I try this afterwards:

nock('http://localhost')
      .get('/apv/v2/listings/' + county)
      .reply(200, responseData)

All the test return a request tiemout as before.

How could I possibly get it to work ?

Mijamo
  • 3,436
  • 1
  • 21
  • 21

1 Answers1

0

For some reasons the problem was that the path add non ASCII characters so using regex path instead of plein string fixed the issue.

Mijamo
  • 3,436
  • 1
  • 21
  • 21