Guillermo
Guillermo

Reputation: 1533

Testing in node.js with mocha - Newbie

I have started implementing TDD in my JS project. I've implemented mocha for that purpose. As these are my first steps what I did:

Folder structure

project

-- js ----filetotest.js

-- test ---- test.js

What I want to do is to run the command make test in order to run the tests inside test.js that tests the filetotest.js file. I read about the node.js approach using exports. But is there some way to include the file in the test suite?

I'm stuck here, and I think that my doubt is more about the concept than the tech thing. Will appreciate a lot your help.

To clarify a little bit what I would like to do: https://nicolas.perriault.net/code/2013/testing-frontend-javascript-code-using-mocha-chai-and-sinon/

I would like to get a similar result through the command line.

Thanks so much,

Guillermo

Upvotes: 2

Views: 644

Answers (1)

Andrei Karpuszonak
Andrei Karpuszonak

Reputation: 9044

You are doing it right.

Now export your function from filetotest.js, like this:

var f1 = function(params) {
  // ...
}

exports.f1 = f1

In test.js, require this file

var f1 = require("./filetotest.js").f1

// test f1

Btw, if you will put your tests in /test directory, mocha will execute them automatically (given that it will be executed from the root of your project)

Upvotes: 1

Related Questions