Gibarian2001
Gibarian2001

Reputation: 937

JavaScript Unit Test. What to mock?

I'm trying to introduce Unit Test in my development process and I'm not sure how to do it.

I have a big application with a lot of modules ( separated js files ). For example:

There are also a few modules that provide services for the rest:

The question is: how do I test a module like videoInfo.js that uses all the services modules? I see two approaches:

What is the good approach to do this when you have a lot of internal dependencies?

Upvotes: 0

Views: 371

Answers (1)

Schleis
Schleis

Reputation: 43790

Ideally, you want both of your approaches in your test suite.

You want to mock all of the dependencies (in your example for videoInfo.js) so that you can isolate its code from anything else and be sure that it is working correctly. Depending on the design of the code that you have, this can be difficult. As the code may not have been written with testing in mind and so won't lend itself to mocking.

You would also want to have an entire app level series of tests that make sure that all the interactions between the parts will work correctly. Depending on the app, these tests can be very brittle due to dependence on data that you have no control over.

These tests would likely be in separate suites so that they can be run independently of each of other.

If you are just getting started in writing tests for code, write the best test that you can. Tests are code and should be refactored as you go along. Write the easiest test that you can. Writing tests for legacy code is extremely difficult. The current design may be difficult to test properly. So when you are making your changes do what you can.

Upvotes: 2

Related Questions