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:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 â‹… 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 â‹… 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 â‹… 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 â‹… 8 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