我在为portable-fetch编写测试时遇到问题。我发现当我使用旧的"要求"导入我可以轻松地嘲笑它,但当我使用" import *"一切都崩溃了。
测试类的示例代码:
import * as fetch from "portable-fetch";//(1) not working
const fetch = require('portable-fetch');//(2) working
const factory = ({ appid }) => (
async ({ city }) => {
const url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${appid}&units=metric`;
console.log("fetch:", fetch)
const currentWeather = await fetch(url)
.then(response => (response.json)())
.then(data => (data));
return currentWeather;
}
);
module.exports = factory;
带有两个导入样本的类代码需要/ import *
TypeError: fetch is not a function
console.log fetch: { _isMockFunction: true,
getMockImplementation: [Function],
mock: { calls: [], instances: [] },
...
输出(1)测试失败:
console.log
fetch: function () {
const mockState = mocker._ensureMockState(f);
const mockConfig = mocker._ensureMockConfig(f);
...
输出(2)测试通过:
SELECT {some_columns} FROM {tableA} WHERE bus='ABCD' AND
datetime(date_column||" "||time_column) >= datetime('2019-03-19 05:30:00')
INTERSECT
SELECT {some_columns} FROM {tableA} WHERE bus='ABCD' AND
datetime(date_column||" "||time_column) <= datetime('2019-03-19 05:30:00', '+135 minutes');
我使用https://editor.swagger.io/生成typescript-fetch api Swagger使用import(1)生成客户端。正如您所看到的,即使使用require导入,模拟对象看起来也不同。我无法通过使用其他导入类型来理解为什么这么多的差异。 forked sample of code