unit tests and code coverage of loopback apllication using mocha & istanbul

970 views
Skip to first unread message

Nicks

unread,
Dec 17, 2014, 5:15:11 AM12/17/14
to loopb...@googlegroups.com
In  my application, I have few API calls using rest connector . I want to write unit test cases and code coverage for each of the API call, How can I achieve this? 

Bryan Clark

unread,
Dec 17, 2014, 1:35:47 PM12/17/14
to loopb...@googlegroups.com
I'm using nock ( https://github.com/pgte/nock ) to mock the API calls for unit testing.

I start by recording the API calls from my unit test:
var nock = require('nock');
nock.recorder.rec();

You'll get something like this dumped to the console:
nock('https://maps.googleapis.com:443')
  .get('/maps/api/timezone/json?location=43.647%2C-79.39275&timestamp=1414368000')
  .reply(200, {"dstOffset":3600,"rawOffset":-18000,"status":"OK","timeZoneId":"America/Toronto","timeZoneName":"Eastern Daylight Time"});

Copy that into your unit test and it will fake the API call such that you can test that the call was made correctly from your rest connector without hitting the API.

~ Bryan

Simon Ho

unread,
Dec 17, 2014, 1:43:12 PM12/17/14
to loopb...@googlegroups.com

Kevin Werner

unread,
Dec 3, 2015, 3:21:02 PM12/3/15
to loopb...@googlegroups.com
I think the question is about mocking the requests being made through the loopback-connector-rest datasource...testing the rest api that is exposed from the loopback server is something else.  Or did I misunderstand the tests you are referring to?

nock worked for me.

Thanks,

kddubb
Reply all
Reply to author
Forward
0 new messages