Reputation: 1759
I don't know if I am stupid, but, I have installed web-request module:
npm install web-request
it is installed, it is present in node modules. I try to use it:
import * as WebRequest from 'web-request';
export class MyHttp {
public static getUrl() {
console.log('hello');
WebRequest.get('http://www.google.com/').then(()=> {
console.log('success');
});
}
}
Then I use it in the test:
import {MyHttp} from '../../../services/MyHttp';
describe('Request', () => {
fit('should be successful', () => {
MyHttp.getUrl();
setTimeout(()=> {
expect(true).toBe(true);
},5000);
});
});
The console output is:
hello
I cannot see 'success' output at all.
The typings is ok, I am able to enter web-request\index.d.ts, that looks fine.
What am I doing wrong ? :(
Upvotes: 0
Views: 189
Reputation: 2111
I'm assuming the tests needs a callback to be called in order for the test runner to know that it finished and that it is Async. Here is an example base on your code. You can read about jasmine for instance here.
import * as WebRequest from 'web-request';
export class MyHttp {
public static async getUrl() {
console.log('hello');
await WebRequest.get('http://www.google.com/')
console.log('success');
}
}
it('should be successful', () => {
MyHttp.getUrl();
expect(true).toBe(true);
});
edit: If you look in the docs of web-request it seems that they use await. There is no need for then
after the function call. This pauses execution until the promise is resolved and gives you the value in the return object. While not suitable for all things it can make sense for testing.
Upvotes: 1