# A new hidden gem for JS testing in 2024
This article explains why some current popular JS testing frameworks are losing to this new emerging and beastly tool.
## :memo: Existing well-known JS testing tools
### Mocha
Mocha used to be the main tool for writing tests in the 2010s due to its simplicity and flexibility. However, as of now, its usage has dropped gradually.
The statistics are from [stateofjs](https://2022.stateofjs.com/en-US/libraries/testing/)

There can be several reasons behind this:
- **Requires many plugins to run a simple unit test**: You need Chai for assertion, and Sinon for mocking, and potentially other libraries for doing other stuff, which is painful.
- **Potential inconsistencies among the plugins**: Some libraries may be deprecated or outdated. Some may be incompatible when using with more modern NodeJs versions.
- **Problems when transpling code**: Many projects now use TypeScript for type-safety. However, configuring Chai to transpile TypeScript to JavaScript when testing may be a problem and increase complexity.
### Jest
Jest is still the most used JavaScript framework because:
- **Simple to set up**: Jest is a unified framework, so we only need to install Jest and start writing tests immediately.
- **Compatibility**: Jest is not only compatible for writing React components, but also suitable for NodeJs, VueJs, and other projects.
- **Built-in auto-mocking**: Jest supports mocking by default and automatically mocks modules when imported into the test files, which simplifies the process of writing tests.
However, when I use Jest, I still many times encountered several problems:
- **Transpiling problems**: A good example is `Import is not a module`. This error exists because `Import` is a keyword used in ES Modules. Meanwhile, Jest does not support ESM natively, and we have to use a transpiler to transform ES code to CommonJs. This process introduces many unexpected problems and inconsistencies.
- **Performance**: Depending on the transplier, Jest can be super slow like `ts-jest` or a bit faster like `@swc/jest`. However, it is still nowhere near fast. The last straw for me was when I wrote an integration test suite and it took more nearly 5 minutes to finish running. Don't believe me?? Take a look at the screenshot below:

## :rocket: New Gem
### Vitest
I got introduced to this awesome tool from the CTO of Oraichain Labs, Mr. Tu Pham, and I was suprised by how fast and simple this tool was.
With vitest, the same test suite above ran from 5 minutes to 1 minute :arrow_upper_right:. I know I know, you are asking for proof again. Here you go:

The main reason why Vitest is so fast is because it uses `esbuild` - a blazingly fast transpiler, bundler for building ESM and CommonJs.
Other pros when using Vitest that I used:
- **Simplicity**: Vitest supports ESM natively and TypeScript out of the box. I didn't have to write any config file before running the tests. The only extra steps I did was importing Jest-compatible methods before running the tests.
- **In-source testing**: I love this feature a lot. It makes the the repository overall cleaner and increases readability. Helper methods should have unit tests right under them without having to navigate to a completely different location.
There are other cool features like multithreading, workspace testing, and built-in mocking as well, but let's save them for another post ^^
> *© 2024 Duc Pham. All rights reserved. This article may be shared but not copied.*