Currently, I am attempting to retrieve HTML content from a webpage by utilizing a class equipped with a single asynchronous method. This process involves Typescript 3.4.3 and request-promise 4.2.4.
import * as rp from 'request-promise';
class HtmlFetcher {
public uri: string;
public html: string;
public constructor(uri: string) {
this.uri = uri;
}
public async fetch() {
await rp(this.uri).then((html) => {
this.html = html;
}).catch((error) => {
throw new Error('Unable to fetch the HTML page');
});
}
}
export { HtmlFetcher };
To validate my class functionality, the following code is employed in conjunction with Jest 24.8.0. It's important to note that the URI specified at line 6 is strictly for testing purposes; alternative URIs have also been experimented with.
import { HtmlFetcher } from './htmlFetcher.service';
describe('Fetch HTML', () => {
it('should fetch the HTMl at the given link', () => {
const uri = 'http://help.websiteos.com/websiteos/example_of_a_simple_html_page.htm';
const fetcher = new HtmlFetcher(uri);
fetcher.fetch();
expect(fetcher.html).toBeDefined();
});
});
The objective is for the html
property to store the fetched HTML string from the designated address post-execution of the fetch() method. Nevertheless, the test currently fails, indicating that fetcher.html
remains undefined
. Unfortunately, reference to the Typescript, Jest, and request-promise documentation has not yielded any insights. What could be the issue?