【发布时间】:2016-08-22 18:10:39
【问题描述】:
我不知道我是不是傻,但是,我已经安装了 web-request 模块:
npm install web-request
它已安装,它存在于节点模块中。 我尝试使用它:
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');
});
}
}
然后我在测试中使用它:
import {MyHttp} from '../../../services/MyHttp';
describe('Request', () => {
fit('should be successful', () => {
MyHttp.getUrl();
setTimeout(()=> {
expect(true).toBe(true);
},5000);
});
});
控制台输出为:
hello
我根本看不到“成功”输出。
打字没问题,我可以输入web-request\index.d.ts,看起来不错。
我做错了什么? :(
【问题讨论】:
-
您的测试运行程序将立即退出,因为它不知道您正在运行异步行为。阅读 jasmine 文档了解如何编写异步测试函数。
-
不要创建只包含静态方法的类,也不要导出它们:stackoverflow.com/q/29893591/1048572
-
您是否尝试过在 promise 中添加
catch错误处理程序? -
即使我在测试中使用 Promise 也不会成功。我用的是静态的,所以测试很清楚,我看过一个规则,测试中没有新关键字。
标签: javascript node.js typescript npm