question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Jest does not automatically mock AsyncGenerator functions

See original GitHub issue

Version

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:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

1reaction
ReFruitycommented, Jun 4, 2022

Can confirm this issue in 2022 for jest version 27.5.1

0reactions
github-actions[bot]commented, Nov 17, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found