2022-12-22
2600
#node
Godwin Ekuma
9184
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 The Aha Stack: Astro, Htmx, Alpine — A Complete Tutorial With A Demo Project And Comparison To Other Stacks

Exploring the AHA stack: Tutorial, demo, and comparison

The AHA stack — Astro, htmx, and Alpine — is a solid web development stack for smaller apps that emphasize frontend speed and SEO.

Oyinkansola Awosan
May 3, 2024 â‹… 13 min read
Comparing Hattip Vs Express Js For Modern Application Development

Comparing Hattip vs. Express.js for modern app development

Explore what Hattip is, how it works, its benefits and key features, and the differences between Hattip and Express.js.

Antonello Zanini
May 2, 2024 â‹… 8 min read
Using React Shepherd To Build A Site Tour

Using React Shepherd to build a site tour

React Shepherd stands out as a site tour library due to its elegant UI and out-of-the-box, easy-to-use React Context implementation.

Onuorah Bonaventure
May 1, 2024 â‹… 14 min read
A Guide To Cookies In Next Js

A guide to cookies in Next.js

Cookies are crucial to web development. This article will explore how to handle cookies in your Next.js applications.

Georgey V B
Apr 30, 2024 â‹… 10 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!!!

    …\node_modules\yargs\yargs.js:1172
    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