Tuesday 25 June 2019

Please Help Me Get Better At Jest (Especially Mocks)

I'm new to Node, and am working on a hobby project that will monitor a site's downtime. In my quest to become a better developer, I am committed to writing tests. However, I am new to Javascript testing, and am having a difficult time with mocks and testing asynchronous code.QuestionHow do I write a test for getResponse() on my Response class. Specifically, I want to ensure the following:getResponse will return a status code if the Response class is provided a urlgetResponse will console log a message if there's an errorCodebaseRepoTestsMockCode``` const rp = require("request-promise");class Response { constructor(url, emails) { this._url = url; this.emails = emails; this._statusCode = null; this._status = null; this._lastChecked; this.screenshot = this._url ? new Screenshot(this._url) : null; }getResponse() { if (!this._url) { throw new Error(You need to pass Response() a URL); } return rp({ uri: this._url, resolveWithFullResponse: true }) .then(response => { return response.statusCode; }) .catch(error => { console.log(There was an error getting data for ${this._url}); }); } ... } ```

Submitted June 25, 2019 at 10:39PM by P013370

No comments:

Post a Comment