Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking using mocha in NodeJs

Tags:

How can I mock a client and a server in Mocha using NodeJs. Specifically, I have the following code:

app.post ('path name', function (req, res) {    // Some Action    res.send(response);  }); 

I want to mock the req, res parameters and test res (status, header, message).

like image 568
ravi tandon Avatar asked Aug 27 '12 17:08

ravi tandon


People also ask

How do you mock in NodeJS?

In Jest, Node. js modules are automatically mocked in your tests when you place the mock files in a __mocks__ folder that's next to the node_modules folder. For example, if you a file called __mock__/fs. js , then every time the fs module is called in your test, Jest will automatically use the mocks.

What is Mocha in NodeJS?

Mocha is a testing library for Node. js, created to be a simple, extensible, and fast. It's used for unit and integration testing, and it's a great candidate for BDD (Behavior Driven Development).


1 Answers

Mocha itself doesn't provide mock/stub/spy type functionality. Sinon is a popular library that does. The home page includes examples of testing ajax as well as their Fake XMLHTTPRequest object.

like image 142
Peter Lyons Avatar answered Sep 18 '22 12:09

Peter Lyons