5

I'm getting started with javascript unit testing (with Jasmine).

I have experience in unit testing C# code. But given that javascript is a dynamic language, I find it very useful to exploit that, and writing tests using the expressive power of javascript, for instance:

describe('known plugins should be exported', function(){
    var plugins = ['bundle','less','sass','coffee','jsn','minifyCSS','minifyJS','forward','fingerprint'];

    plugins.forEach(function(plugin){
        it('should export plugin named ' + plugin, function(){
            expect(all[plugin]).toBeDefined();
        });
    });
});

As far as doing this kind of non-conventional test-writing, I haven't gone further than doing this kind of tests (array with a list of test cases that are very similar)

So I guess my question is

Is it fine to write tests like this, or should I constrain myself to a more "statically typed" test fixture?

1 Answer 1

3

Great question!

Yes, it is perfectly fine to write unit tests like this. It's even encouraged.

JavaScript being a dynamic language lets you mock objects really easily. DI and IoC are really easy to do. In general, testing with Jasmine (or Mocha which I personally prefer) is a pleasant and fun experience.

It's worth mentioning that since you're in a dynamic language you need to have tests you did not in statically typed languages. Tests commonly enforce members and methods existing, and types.

Having no interfaces to define your contract, often, your tests define your code's contract so it's really not uncommon to see tests do this sort of verification (like in your code) where you wouldn't in C#.

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

1 Comment

absolutely, I mostly wanted an opinion on whether it felt messy to express test cases like that

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.