[PR #2300] [MERGED] Bookfinder.js unit testing with mocha #3692

Closed
opened 2026-04-25 00:16:40 +02:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/advplyr/audiobookshelf/pull/2300
Author: @mikiher
Created: 11/9/2023
Status: Merged
Merged: 11/18/2023
Merged by: @advplyr

Base: masterHead: bookfinder-testing-mocha


📝 Commits (10+)

  • 8f5a6b7 Move utility functions to module scope
  • ee3d380 Refactor removing author from title candidate
  • 3a9d09e Add jest to dev dependencies
  • 047e7a7 Make position an internal property of titleCandidates
  • 5a3d450 Refactor diff declarations in title candidate sorting
  • 89055f8 Remove unnecessary includesAuthorDiff from sorting
  • ba60fc7 Add tests for TitleCanidates
  • 819c524 Pass audnexus to AuthorCandidates constructor directly
  • 49e4515 Add stripRedudantSpaces
  • 2730486 Add tests for AuthorCandidates and search() in BookFinder

📊 Changes

6 files changed (+6578 additions, -2063 deletions)

View changed files

📝 .gitignore (+2 -1)
📝 .vscode/settings.json (+2 -1)
📝 package-lock.json (+6134 -1984)
📝 package.json (+12 -3)
📝 server/finders/BookFinder.js (+84 -74)
test/server/finders/BookFinder.test.js (+344 -0)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/advplyr/audiobookshelf/pull/2300 **Author:** [@mikiher](https://github.com/mikiher) **Created:** 11/9/2023 **Status:** ✅ Merged **Merged:** 11/18/2023 **Merged by:** [@advplyr](https://github.com/advplyr) **Base:** `master` ← **Head:** `bookfinder-testing-mocha` --- ### 📝 Commits (10+) - [`8f5a6b7`](https://github.com/advplyr/audiobookshelf/commit/8f5a6b7c959f2a08880b7dbb7822c008939260e4) Move utility functions to module scope - [`ee3d380`](https://github.com/advplyr/audiobookshelf/commit/ee3d3808ef49ecfdd08f8725c57fbe63c52d42ab) Refactor removing author from title candidate - [`3a9d09e`](https://github.com/advplyr/audiobookshelf/commit/3a9d09ea637ac3a098c1b34fc447d56d8391201c) Add jest to dev dependencies - [`047e7a7`](https://github.com/advplyr/audiobookshelf/commit/047e7a72f21d9023b80e32bc0f63c6792acdd6f4) Make position an internal property of titleCandidates - [`5a3d450`](https://github.com/advplyr/audiobookshelf/commit/5a3d450482b31c31f0d60c049f499fb9d871c829) Refactor diff declarations in title candidate sorting - [`89055f8`](https://github.com/advplyr/audiobookshelf/commit/89055f86552965e899a154c3517d22f85a966ee8) Remove unnecessary includesAuthorDiff from sorting - [`ba60fc7`](https://github.com/advplyr/audiobookshelf/commit/ba60fc75814a0cf0bd1cd6ba56a2c602378b69e0) Add tests for TitleCanidates - [`819c524`](https://github.com/advplyr/audiobookshelf/commit/819c524f5192306d2aec5dd1ee2e9a929755714a) Pass audnexus to AuthorCandidates constructor directly - [`49e4515`](https://github.com/advplyr/audiobookshelf/commit/49e4515785c177058a4da9427130e9a6adebb44f) Add stripRedudantSpaces - [`2730486`](https://github.com/advplyr/audiobookshelf/commit/2730486ba58384b6fb1696d2b1ffa803889c1ade) Add tests for AuthorCandidates and search() in BookFinder ### 📊 Changes **6 files changed** (+6578 additions, -2063 deletions) <details> <summary>View changed files</summary> 📝 `.gitignore` (+2 -1) 📝 `.vscode/settings.json` (+2 -1) 📝 `package-lock.json` (+6134 -1984) 📝 `package.json` (+12 -3) 📝 `server/finders/BookFinder.js` (+84 -74) ➕ `test/server/finders/BookFinder.test.js` (+344 -0) </details> ### 📄 Description 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](https://github.com/advplyr/audiobookshelf/commit/8f5a6b7c959f2a08880b7dbb7822c008939260e4), and some additional functionality like [this](https://github.com/advplyr/audiobookshelf/commit/49e4515785c177058a4da9427130e9a6adebb44f). 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-04-25 00:16:40 +02:00
adam closed this issue 2026-04-25 00:16:40 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#3692