我有一个异步加载脚本的工具,使用 jest 进行异步测试,但是 jest 超时并且无法响应脚本。如何实现此功能?
我试着按照文档的承诺写了一个测试样本
这是我的脚本加载器代码和 Jest 测试代码
function mapLoader(key, version, url) {
return new Promise((resolve, reject) => {
if (AMap) {
return resolve(AMap);
}
if (!url && (!key || !version)) {
warn('The parameter is incorrect and must contain the url attribute or the key and version attributes.');
}
const aMapUrl = url || `${defaultPath}?v=${version}&key=${key}`;
const jsApi = document.createElement('script');
jsApi.charset = 'utf-8';
jsApi.src = aMapUrl;
jsApi.onerror = reject;
jsApi.onload = () => {
if (window.AMap) {
AMap = window.AMap; …Run Code Online (Sandbox Code Playgroud) jestjs ×1