Nightwatch Mock HTTP Requests

前端 未结 1 1275
不思量自难忘°
不思量自难忘° 2021-02-09 14:00

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 S         


        
相关标签:
1条回答
  • 2021-02-09 14:46

    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

    0 讨论(0)
提交回复
热议问题