MikeNQ
MikeNQ

Reputation: 653

Javascript Unit testing

Recently I try to apply strict OO and testing to my application. And there are few things I would like to ask:

From how I feel, trying to cover all the above will result in distraction and inefficient testing.

What is your suggestion?

Many thanks,

Upvotes: 2

Views: 97

Answers (1)

Alex Wayne
Alex Wayne

Reputation: 186984

As javascript is not type restricted, for parameter input, do you need to write unit test to check if is null, undefined or different type?

It's up to you how defensive you want your code to be. If a requirement of your code is to gracefully handle nonsense arguments then you should write a test that tests that graceful handling. If you have code that handles it, you should have a test that covers it. If you don't have code to handle nonsense input, there isn't much reason to test it.

Part of the last question, if yes, is it necessary to write unit test for constructor input parameter validation?

Really, it's the same answer. If you have code that validates your inputs, you should have tests that cover that code.

Should I try to apply strict OO to javascript OO. For example, all class should encapsulate their instance property (using var) and have getter/setter in closure instead?

Javascript is not a traditional OO language. Only using strictly OO patterns would be doing yourself a disservice since JavaScript's most expressive features are around things on functional side of things. Having truly "private" member variables "using var", for instance, fundamentally changes the way you have to structure your code around those variables. This may not be a compromise worth making.

It's probably wise to architect a large codebase using primarily a class based approach. But don't be afraid to use functional features where appropriate. As long as you have the test coverage either way, of course. But this is primarily opinion at this point.

Upvotes: 2

Related Questions