Godwin Ekuma
Dec 22, 2022 ⋅ 9 min read

Node.js unit testing using Mocha, Chai, and Sinon

Godwin Ekuma I learn so that I can solve problems.

Recent posts:

Exploring Stylex And The New Generation Of Styling Libraries

Exploring StyleX and the new generation of styling libraries

StyleX is a build-time, type-safe CSS-in-JS library recently open sourced by Meta. Explore StyleX and the evolution of styling libraries.

Ibadehin Mojeed
Feb 29, 2024 ⋅ 9 min read
Building High Performance Ecommerce Sites With Astro

Building high-performance ecommerce sites with Astro

Learn to set up a completely custom Astro ecommerce implementation that’s also highly performant and type-safe in this straightforward guide.

Onuorah Bonaventure
Feb 28, 2024 ⋅ 64 min read
Implementing Vector Search With Open Ai, Next Js, And Supabase

Implementing vector search with OpenAI, Next.js, and Supabase

Let’s build a Next.js app that implements vector search using Supabase and OpenAI to offer better search experiences for users.

Peter Ekene Eze
Feb 27, 2024 ⋅ 11 min read
Comparing React Native BLE libraries

Comparing React Native BLE libraries

Discover the most popular libraries for enabling Bluetooth Low Energy (BLE) in React Native apps, allowing them to interact with Bluetooth hardware on iOS and Android platforms.

Fimber Elemuwa
Feb 26, 2024 ⋅ 4 min read
View all posts

9 Replies to "Node.js unit testing using Mocha, Chai, and Sinon"

  1. Should the user repository take UserModel as an argument in the constructor? I’m wondering how the test suite for UserRepository knows anything about the stubbed model if the stubbed model is not passed in to the constructor.

  2. The Model exists in the global scope of the user repository, this allows the repository class have access to it. So in the test if the UserModel is referenced the stub will passed instead of the actual model.

  3. Hi thanks for sharing!. I have a question if i wanna return response with 500 status code if repository can not save user to database i.e database connection is broken or like that.

  4. I getting this error, please help me to solve this!!!

    else throw err

    Error: Cannot find module ‘../database’
    Require stack:
    – E:\Intenships\vivasvant systems llp\Web development\src\js\mocks\user.repository.test.js
    – E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\mocha.js
    – E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\cli\one-and-dones.js
    – E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\cli\options.js
    – E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\bin\mocha
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:610:15)
    at Function.Module._load (internal/modules/cjs/loader.js:526:27)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object. (E:\Intenships\vivasvant systems llp\Web development\src\js\mocks\user.repository.test.js:5:23)
    at Module._compile (internal/modules/cjs/loader.js:759:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Module.require (internal/modules/cjs/loader.js:666:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\mocha.js:334:36
    at Array.forEach ()
    at Mocha.loadFiles (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\mocha.js:331:14)
    at Mocha.run (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\mocha.js:809:10)
    at Object.exports.singleRun (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\cli\run-helpers.js:108:16)
    at exports.runMocha (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\cli\run-helpers.js:142:13)
    at Object.exports.handler (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\cli\run.js:292:3)
    at Object.runCommand (E:\Intenships\vivasvant systems llp\Web development\node_modules\yargs\lib\command.js:242:26)
    at Object.parseArgs [as _parseArgs] (E:\Intenships\vivasvant systems llp\Web development\node_modules\yargs\yargs.js:1096:28)
    at Object.parse (E:\Intenships\vivasvant systems llp\Web development\node_modules\yargs\yargs.js:575:25)
    at Object.exports.main (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\lib\cli\cli.js:68:6)
    at Object. (E:\Intenships\vivasvant systems llp\Web development\node_modules\mocha\bin\mocha:162:29)
    at Module._compile (internal/modules/cjs/loader.js:759:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10)
    at Module.load (internal/modules/cjs/loader.js:628:32)
    at Function.Module._load (internal/modules/cjs/loader.js:555:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:826:10)
    at internal/main/run_main_module.js:17:11
    npm ERR! Test failed. See above for more details.

  5. Hi there,
    thanks a lot for the excellent post.

    Just an update: faker is no longer available and has become a community project: npm install @faker-js/faker

Leave a Reply