Puppeteer 异步函数有效但在测试中引发错误
Puppeteer async function that works but raises error in test
我正在尝试使用 Chai.js 测试异步函数。当我从程序中调用它时它可以工作,但在测试时它 returns 未定义。
这是函数
async function getLinks(url, minW = 1, minH = 1) {
try {
const pup = await puppeteer.launch({ defaultViewport: null });
let page = await pup.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
return await page.evaluate(
({ minW, minH }) => {
return [...document.getElementsByTagName('a')]
.map(x => {
return { url: x.href, type: 'a' };
})
.concat(
[...document.getElementsByTagName('img')]
.filter(x => x.naturalWidth >= minW && x.naturalHeight >= minH)
.map(x => {
return { url: x.src, type: 'img' };
})
);
},
{ minW, minH }
);
} catch (err) {
console.log(err);
}
}
这是测试
describe('getLinks', () => {
it('should return an array of links.', async function() {
const links = await getLinks('http://www.msftconnecttest.com');
expect(links).to.be.an('array');
}).timeout(5000);
});
而且,这是我得到的错误
1) getLinks
should return an array of links.:
AssertionError: expected undefined to be an array
at Context.<anonymous> (test\test.js:9:27)
at processTicksAndRejections (internal/process/task_queues.js:82:5)
更新:
代码落入 catch 块,这里是原始错误消息:
Error: Evaluation failed: ReferenceError: cov_1xrphtbyiu is not defined
at __puppeteer_evaluation_script__:1:18
at ExecutionContext._evaluateInternal (node_modules\puppeteer\lib\ExecutionContext.js:122:13)
at processTicksAndRejections (internal/process/task_queues.js:82:5)
at async ExecutionContext.evaluate (node_modules\puppeteer\lib\ExecutionContext.js:48:12)
at async getLinks (main.js:1:6360)
at async Context.<anonymous> (test\test.js:8:21)
我不知道它成为问题的原因,因为我不使用 Babel 或类似工具,但在我更改 return
以破坏代码后,现在它在测试和程序中都可以工作。
可能是 version-specific 问题。
return await page.evaluate(
`((minW, minH) => {
return [...document.getElementsByTagName('a')]
.map(x => {
return { url: x.href, type: 'a' };
})
.concat(
[...document.getElementsByTagName('img')]
.filter(x => x.naturalWidth >= minW && x.naturalHeight >= minH)
.map(x => {
return { url: x.src, type: 'img' };
})
);
})(${minW}, ${minH})`,
);
我正在尝试使用 Chai.js 测试异步函数。当我从程序中调用它时它可以工作,但在测试时它 returns 未定义。
这是函数
async function getLinks(url, minW = 1, minH = 1) {
try {
const pup = await puppeteer.launch({ defaultViewport: null });
let page = await pup.newPage();
await page.goto(url, { waitUntil: 'networkidle2' });
return await page.evaluate(
({ minW, minH }) => {
return [...document.getElementsByTagName('a')]
.map(x => {
return { url: x.href, type: 'a' };
})
.concat(
[...document.getElementsByTagName('img')]
.filter(x => x.naturalWidth >= minW && x.naturalHeight >= minH)
.map(x => {
return { url: x.src, type: 'img' };
})
);
},
{ minW, minH }
);
} catch (err) {
console.log(err);
}
}
这是测试
describe('getLinks', () => {
it('should return an array of links.', async function() {
const links = await getLinks('http://www.msftconnecttest.com');
expect(links).to.be.an('array');
}).timeout(5000);
});
而且,这是我得到的错误
1) getLinks
should return an array of links.:
AssertionError: expected undefined to be an array
at Context.<anonymous> (test\test.js:9:27)
at processTicksAndRejections (internal/process/task_queues.js:82:5)
更新:
代码落入 catch 块,这里是原始错误消息:
Error: Evaluation failed: ReferenceError: cov_1xrphtbyiu is not defined
at __puppeteer_evaluation_script__:1:18
at ExecutionContext._evaluateInternal (node_modules\puppeteer\lib\ExecutionContext.js:122:13)
at processTicksAndRejections (internal/process/task_queues.js:82:5)
at async ExecutionContext.evaluate (node_modules\puppeteer\lib\ExecutionContext.js:48:12)
at async getLinks (main.js:1:6360)
at async Context.<anonymous> (test\test.js:8:21)
我不知道它成为问题的原因,因为我不使用 Babel 或类似工具,但在我更改 return
以破坏代码后,现在它在测试和程序中都可以工作。
可能是 version-specific 问题。
return await page.evaluate(
`((minW, minH) => {
return [...document.getElementsByTagName('a')]
.map(x => {
return { url: x.href, type: 'a' };
})
.concat(
[...document.getElementsByTagName('img')]
.filter(x => x.naturalWidth >= minW && x.naturalHeight >= minH)
.map(x => {
return { url: x.src, type: 'img' };
})
);
})(${minW}, ${minH})`,
);