【发布时间】:2020-10-08 10:32:51
【问题描述】:
我有一个简单的打字稿功能,它只打印一些文本和睡眠。测试完成,无需等待异步函数返回。
这是我的功能。
test('sleep test', () => {
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function delayedGreeting() {
console.log('Hello');
await sleep(5000);
console.log('World!');
await sleep(5000);
console.log('Goodbye!');
}
delayedGreeting();
});
测试以控制台中的“Hello”结束。
我尝试了本网站https://www.sitepoint.com/delay-sleep-pause-wait/ 建议的其他形式。我的测试总是以仅打印“Hello”结束。
项目的骨架是从带有 typescript 选项的 create-react-app 生成的。 我的项目中有这些依赖项。
"dependencies": {
"@types/node": "^12.12.47",
"@types/react": "^16.9.38",
"@types/react-dom": "^16.9.8",
"@types/zeromq": "^4.6.3",
"protobuf-typescript": "^6.8.8",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "^3.7.5",
"zeromq": "^6.0.0-beta.6"
},
"devDependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
"@types/jest": "^24.9.1",
"@typescript-eslint/eslint-plugin": "^3.3.0",
"@typescript-eslint/parser": "^3.3.0",
"eslint-config-prettier": "^6.11.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "^7.20.0",
"prettier": "^2.0.5"
},
我使用npm test 来运行我的测试。
这和 Jest 有关系吗?如何在我的测试中解决这个问题?谢谢。
【问题讨论】:
标签: typescript async-await jestjs