Hey everyone,
My team is thinking about implementing unit testing using JEST for our large and complex codebase. I would appreciate any advice on how to structure our code for unit tests and any recommendations you might have for getting started with this.
5 Answers
The tool you use is less important; you can go with either JEST or Vitest—they're quite similar. Just make sure to start small with your unit tests.
I’ve had a rocky experience with JEST myself. I’d suggest exploring Vitest or Playwright instead. As for structuring tests, functional programming can simplify this process a lot.
It's tough to give specific advice without seeing your codebase, but generally, if a piece of code is easy to test, it indicates good design. If it's hard to test, it's a sign that it might need refactoring. The act of testing can often lead you to improve your code.
If you're starting fresh, consider using Vitest instead of JEST. It has a similar API but offers a more modern development experience. For structuring tests, I recommend placing your test files next to the components they test. This approach makes it easier to spot any missing tests and keeps import paths consistent.
In our codebase, we keep tests in a '__test__' directory, which we exclude from the build. This way, you avoid bundling test files accidentally. It's a solid structure that works well for us.
Related Questions
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically
[Centos] Delete All Files And Folders That Contain a String