Assertions in If-Clauses in Unit Tests: Tips and Best Practices

Assertions, such as expect in vitest and jest, are an indispensable part of unit tests. However, assertions inside if-clauses risk being silently skipped. There is even an ESLint rule no-conditional-expect that checks conditional assertions. In this post, we discuss some tips and best practices to avoid situations where assertions live in if-clauses. ...

 ·  · 3 min · 477 words

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’. ...

 ·  · 6 min · 1133 words

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

 ·  · 7 min · 1424 words

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

 ·  · 2 min · 410 words
Loading Speed Changes

HTML Tricks to Speed up Loading React Apps

Besides many tricks that improve the performance from within a React app, there are also tricks to improve the loading speed from the HTML document in which the React app is embedded. In this post, we will walk through some of these tricks. ...

 ·  · 5 min · 1027 words