2

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:

  • Installed node.js
  • Installed mocha globally and locally to my project.
  • Wrote package.json setting dependencies.
  • Wrote makefile.
  • Wrote .gitignore to avoid uploading node_modules folder.

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

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much! So there is no way to prevent adding the exports... in the original file to test through the command line right?
implementation itself is hidden, so you have encapsulation. the only exposed part is your function name and parameters, which could be used by another files/modules. using TDD you shall ensure that for given input function provides needed output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.