[Bug]: Jest does not automatically mock AsyncGenerator functions
See original GitHub issueVersion
27.3.1
Steps to reproduce
module/index.js
async function* asyncGenerator() {
await new Promise(resolve => setTimeout(resolve, 100));
yield 'done';
}
function* syncGenerator() {
yield 'done';
}
module.exports.asyncGenerator = asyncGenerator;
module.exports.syncGenerator = syncGenerator;
module.exports.asyncMethod = async () => {
return 1;
}
module.exports.syncMethod = () => {
return 1;
}
index.spec.js
jest.mock('./module');
const mod = require('./module');
describe('all methods', () => {
test('should be mocked automatically', () => {
expect(mod.syncMethod).toBeDefined();
expect(mod.asyncMethod).toBeDefined();
expect(mod.syncGenerator).toBeDefined();
expect(mod.asyncGenerator).toBeDefined(); // <-- this is where it fails
});
});
Expected behavior
Jest should automatically mock AsyncGeneratorFunction
functions as it also automatically mocks Function
, AsyncFunction
and GeneratorFunction
.
Actual behavior
module.exports.asyncGenerator
is undefined
and not mocked at all. It needs to be explicitly defined and mocked.
Additional context
The issue #5979 described the same problem for synchronous generator functions. In the PR #5983 the support for GeneratorFunction
was added. This could also be done for AsyncGeneratorFunction
.
Environment
System:
OS: Windows 10 10.0.22000
CPU: (12) x64 Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
Binaries:
Node: 14.15.3 - C:\Program Files\nodejs\node.EXE
npm: 6.14.9 - C:\Program Files\nodejs\npm.CMD
npmPackages:
jest: ^27.3.1 => 27.3.1
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:5
Top Results From Across the Web
Unit test asynchronous generator function in Jest
Failed implementation (I tried different mocking of the createReadStream but without success): describe('Work Dir Utils', () => { jest. mock(' ...
Read more >An Async Example - Jest
This module is being mocked in __mocks__/request.js ... in the __mocks__ folder (the folder is case-sensitive, __MOCKS__ will not work).
Read more >Async iteration and generators - The Modern JavaScript Tutorial
Asynchronous iteration allow us to iterate over data that comes asynchronously, on-demand. Like, for instance, when we download something ...
Read more >AsyncGenerator - JavaScript - MDN Web Docs
The AsyncGenerator constructor is not available globally. Instances of AsyncGenerator must be returned from async generator functions.
Read more >jest | Yarn - Package Manager
Features. Fixes. Chore & Maintenance. Performance. 29.3.1. Fixes. [jest-config] Do not warn about preset in ProjectConfig #13583 ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Can confirm this issue in 2022 for jest version 27.5.1
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.