Assertions in Loops in Tests: Tips and Best Practices
Assertions, such as expect in vitest and jest, are an indispensable part of tests. However, assertions inside loops risk being silently skipped. In this post, we discuss some tips and best practices in situations where assertions live in loops. ...
Assertions in If-Clauses in Tests: Tips and Best Practices
Assertions, such as expect in vitest and jest, are an indispensable part of tests. However, assertions inside if-clauses risks being silently skipped. There is even an ESLint rule that checks conditional assertions. In this post, we discuss some tips and best practices in situations where assertions live in if-clauses. ...
An Elegant and Safe Solution for the Strict Typing of Array.includes
Array.includes is a commonly used function in JavaScript. However, in TypeScript, its typing is quite strict: The element being searched for must have the same type as that of the array element. This causes type errors if the array is of a literal type. For example, with the following code snippet: const okNumbers = [1, 3, 5, 7] as const; console.log(`2 is OK? ${okNumbers.includes(2)}`); The TypeScript compiler complains: Argument of type ‘2’ is not assignable to parameter of type ‘1 | 3 | 5 | 7’. ...
JavaScript Performance Tips: The Hidden Cost of Literals
A literal is a textual representation (notation) of a value as it is written in source code. Many people generally associate the concept of literals with performance “cheapness”: A literal always seems to consume very little resource. Is this true? This post discusses the hidden performance cost of literals in JavaScript. ...
Tips to Improve Git Experience for JavaScript Projects
Git is an open source distributed version control system. It is currently the most popular version control system according to various surveys, and has been the core driver of many popular development platforms, such as GitHub, GitLab, Bitbucket, etc. Many JavaScript developers also use Git as their main version control system. Therefore, improving Git experience is key to a productive and happy JavaScript development process. ...