How to temporarily silence logs in tests
Logger package in Changesets source code provides a documentation about silencing log message in tests. This got me wonder how Changesets do it and made me look into its source code.
Changesets repository search for silencing logs
I searched for temporarilySilenceLogs across the Changesets repo using Github search.
What made me choose to search for temporarilySilenceLogs is the fact that it is mentioned in the Logger
package Readme.
1 2 3 4 5 6 7 |
|
When you are trying to understand the source code, you can use documentation as your starting point and search for variables and functions to set the direction for your exploration when you are dealing with large projects like Changesets.
temporarilySilenceLogs
The below code is picked from Changesets source code.
This function accepts a function as argument and then silences the logs using a function named createLogSilencer.
Pay attention to the setup function here:
1 2 3 4 5 6 |
|
createLogSilencer
Below code is picked from Changesets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
What is happening here?
- The assignment
1 2 3 4 5 6 7 8 9 10 11 |
|
2. Returns setup
If you noticed above, setup is called inside temporarilySilenceLogs, this is returned by createLogSilencer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
What is happening in the setup function?
2.1 Loggers and console API are initialised to jest.fn()
1 2 3 4 5 6 7 8 9 10 11 |
|
This pretty much silences the logs since jest.fn() gets called when you use any logger, hence this is considerd as setup, an important step to silence your logs.
2.2 setUp returns original loggers
If you have noticed, the sequence of function calls are
a. const silencer = createLogSilencer();
b. const dispose = silencer.setup();
c. In the finally block.
1 2 3 4 5 |
|
dispose is returned by setup function that is returned by createLogSilencer. This step restores the logging mechanism after executing your test function.
About us:
At Thinkthroo, we study large open source projects and provide architectural guides. We have developed reusable Components, built with tailwind, that you can use in your project. We offer Next.js, React and Node development services.
Book a meeting with us to discuss your project.
References:
https://github.com/changesets/changesets/tree/main/packages/logger#silencing-messages-in-tests
https://github.com/search?q=repo:changesets/changesets temporarilySilenceLogs &type=code
https://github.com/changesets/changesets/blob/baf56448606e005577dbe2fb1e78ff457dcaaefd/scripts/test-utils/src/index.ts#L16
The above is the detailed content of How to temporarily silence logs in tests. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...
