Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nightwatch Mock HTTP Requests

I try mocking HTTP requests with nock and other libs like sinonjs but without success.

import nock from "nock"

const URL = "http://localhost:8080/"

const SIGN_IN_PATH = "/fake/users/sign_in.json"

export const signInRequest = (status, payload = {}) => {
  return nock(URL).get(SIGN_IN_PATH).reply(status, payload)
}

-

import { signInRequest } from "./../../utils/fakeRequests"

const doLogin = (browser) => {
  return browser
          .url("http://localhost:8080")
          .waitForElementVisible('form', 1000)
          .setValue('input[name=email]', '[email protected]')
          .setValue('input[name=password]', 'somepass')
          .click('button[type=submit]')
          .pause(500)
}

export default {
  "Do login and shows error message": (browser) => {
    signInRequest(403)

    doLogin(browser)
      .waitForElementVisible('.error', 1000)
      .end()
  }
}

Its possible mock http requests with nightwatch?

like image 988
Bruno Quaresma Avatar asked Jul 15 '16 19:07

Bruno Quaresma


People also ask

How to implement API testing in Nightwatch JS?

Nightwatch JS doesn’t provide any out of the box solution for API testing. Hence we would need some external node packages and then use them in our tests. Although there are a number of ways to implement API Tests in Nightwatch, the steps mentioned in this Github thread are very simple and straight-forward.

What is the depth of an HTTP request or response mock?

The depth of an HTTP request or response mock brings a level of complexity to the whole system. In this article, we revisit some techniques used to mock HTTP request/response when used in the same test case.

How to approach mocking request and response objects in expressjs?

When trying to figure out how to approach mocking request and response objects, the following points may be a challenge: Testing an expressjs middleware provides a good use case where mocking a request and response in the same test case makes sense. Make sure the requests don't leave the local machine.

What is the use of Nock?

We use Nock to intercept the request and mock a 500 response, then test what the function returns. Being able to mock non-200 response codes, delaying the connection, and socket timeouts, is incredibly useful.


1 Answers

Nightwatch.js is an end to end testing tool - so the point is that actual UIs as well as the api they call will be live (and not mocked) So maybe you are looking for a framework designed for integration tests like casper.js (http://casperjs.org/) or nightmare (https://github.com/segmentio/nightmare)

However mocking http calls in nightwatch should be doable with nock i believe (if you have some strange use case that warrants it) Ensure that you're nock http calls are before tests (they can even be in a before block), eg:

module.exports = {
  'test abc' : function (browser) {
    nock('http://example.com')
      .get('/users')
      .query({name: 'martin'})
      .reply(200, {results: [{id: '123'}]});

    // do test stuff
  },
};

Possible problem with your example is that your mocking the entire UI - nightwatch might be somehow preventing the same domain from being intercepted. Having your UI and API on different domains (or ports) might solve the issue

like image 70
Marty Avatar answered Oct 06 '22 06:10

Marty