

Jest Mocks with ES Modules
source link: https://www.kevinhooke.com/2022/12/23/jest-mocks-with-es-modules/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Jest Mocks with ES Modules – Kev's Development ToolboxSkip to content

Articles, notes and random thoughts on Software Development and Technology
Jest’s support for mocks with CommonJS modules works great, they’re easy to use and it works great, as per the docs.
With ES Modules on the other hand, the docs are vague, and after spending several hours trying to get them to work with code I needed to test and not having any success, I spent a few more hours getting it to work with even the simplest example code.
This current issue describes multiple approaches which others have had varying success with, but there’s one tip mentioned that I can confirm is key to getting the mocks to work with ES modules and this is currently not mentioned in the docs (at least when I last looked) – you must await the import for the module you are mocking. This needs to be combined with the following:
- jest.unstable_mockModule() need to be called at the top level, and must be before the import for the module you are mocking
- You must await the import
To explain the last point, you must use this:
const { example2WithModule } = await import(‘./example.js’);
and not this:
import { example2WithModule } from ‘./example.js’;
Here’s an example of my working usage of jest.unstable_mockModule():
jest.unstable_mockModule('./example-module.js', () => ({ exampleFunctionFromModule: jest.fn( () => { return 'mocked return!'; } ) }));
CategoriesKev's BlogTagses6, javascript, js, unit test
Leave a Reply Cancel reply
Your email address will not be published. Required fields are marked *
Comment *
Name *
Email *
Website
Save my name, email, and website in this browser for the next time I comment.
Notify me of follow-up comments by email.
Notify me of new posts by email.
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Post navigation
Recommend
-
75
Writing tests for your code? Consider using fakes instead of mocks because fakes provide more flexibility and allow for easy testing and refactoring. They also reduce dependencies and are easier to maintain compared to mocks. Read more.
-
39
Testing can be simple. In fact, it is simple. Well, it is simple until impurities slip in. Code that would be easy to test becomes a nightmare as soon as you get impure data (like date...
-
39
Suppose you have an application that manages employees or users. You need to make a simple function that will create a new employee, but you want to make sure that the employee always contains certain fields and will have...
-
6
This post is a personal opinionated piece around unit testing and the dogmas of our industry. Be sure to come here with an open mind and respectul toughts. I will also link to some
-
9
How WePay mocks SFTP payment processor backends By Tao Long on Mar 16, 2016 In payments, as the volume of transactions increases, the number of ways things can g...
-
8
RSpec mocks and stubs in plain English by Jason Swett, January 29, 2019 One of the most common questions I see from beginners...
-
3
常用的 native timer 包括 setTimeout、setInterval、clearTimeout、clearInterval 等,用到這些 timer 的函數可...
-
8
manual mock 是用於透過 mock 資料來對功能進行 stub out。例如:若你不想存取網站或 DB 之類的遠端資源,可能需要使用 fake data 來 manual mock 這些功能,以確保可以...
-
17
Jest Mocks for Google Maps Description Jest mocks for Google Maps in TypeScript. Note: If you find a missing mock, please open an issue...
-
7
node16 and Jest with ES6+ modules I’ve run into this a few times, so leaving some notes for myself for next time, By default, attempting to use...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK