简体   繁体   中英

Test cases using Chai and mocha

I have following function:

function showAllUrlsData(inputUrl, inputUrlPromise) {
      fetch(inputUrl)
        .then(function(response) {
            return response.text()
        })
        .then(function(result) {
          var inputObjectData =  htmlResult(result);
          inputObjData(inputObjectData, inputUrl ,inputUrlPromise);
      });
    }

how to write the test cases using fetch? I'm very new to this test case framework,any inputs will be really helpful.

You can use fetch-mock . Install with

npm i -D fetch-mock

Then use it like this to mock a fetch:

var fetchMock = require('fetch-mock');
var makeRequest = require('./make-request');

// Mock the fetch() global to always return the same value for GET
// requests to all URLs.
fetchMock.get('*', { hello: 'world' });

makeRequest().then(function(data) {
  console.log('got data', data);
});

// Unmock.
fetchMock.reset();

For more information here's the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM