Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bookfinder.js unit testing with mocha #2300

Merged
merged 15 commits into from
Nov 18, 2023

Conversation

mikiher
Copy link
Contributor

@mikiher mikiher commented Nov 9, 2023

Adds unit tests for the search method and the TitleCandidates and AuthorCandidates classes in BookFinder.
As you can see from the commits progression, the tests were initially written using Jest, and then re-written to use Mocha (plus Sinon and Chai).

There aren't any fundamental differences between the two implementations, but they allowed me to examine the differences between Jest and Mocha by myself. My main observations were:

  • Jest is more self-contained, it has built-in assertion, mocking, and coverage capabilities, and these capabilities are good enough to write adequate unit tests.
  • Mocha requires additional libraries (like Chai for assertions and Sinon for mocking). Sinon and Chai provide similar and sometimes stronger capabilities, and their syntax is slightly more to my preference.
  • Writing tests for both Mocha and Jest was relatively easy (given the wealth of information on both). Mocking was also quite easy in both. I found the Sinon API to be a bit more intuitive for my taste, and it allowed me a bit more flexibility.
  • Mocha was much faster than Jest running tests. Running the 'npm test' command (in a dev container) took ~6-7 seconds in Jest, and about ~1-1.5 seconds on Mocha. A lot of this time is spent on loading of the framework in both cases, which will likely be amortized over many test suites, but running the tests themselves was also 4-5x faster on Mocha (typically reported by Mocha to be ~70-80 ms for the whole suite of 72 tests).

It is mainly due to the last point that I think I'd recommend Mocha over Jest at this point in time. Moving to jest in the future would be relatively easy if it becomes necessary/desirable (in fact, Jest can run my Mocha test as-is without any modifications, though the other way round is not true).

I also naturally made some changes to BookFinder itself while writing the tests, some of them just refactoring changes like
this, and some additional functionality like this.

As for the tests themselves, please judge for yourself.
I tried to use parametrized testing quite a bit because it reduces repetition.
I hope the test code and descriptions speak for themselves.

@mikiher mikiher marked this pull request as ready for review November 9, 2023 21:47
@mikiher
Copy link
Contributor Author

mikiher commented Nov 10, 2023

Also added test coverage tooling with Istanbul (nyc)

@mikiher mikiher mentioned this pull request Nov 12, 2023
@advplyr
Copy link
Owner

advplyr commented Nov 18, 2023

This is great, thanks for digging into this. The tests for me take about 100ms when not running in a dev container.

I still have to learn more about the tools but I like it so far. These decisions are easier to make because they don't impact users if we switch it up later.

@advplyr advplyr merged commit d2aea86 into advplyr:master Nov 18, 2023
1 check passed
@advplyr advplyr mentioned this pull request Dec 2, 2023
@mikiher mikiher deleted the bookfinder-testing-mocha branch July 12, 2024 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants