What is the best way to test javascript? - javascript

I'm building several jQuery plugin and going ahead I thought that will be useful to know some best practice for testing my code in order to be solid.
I usually use Selenium but this just for debugging.
I'd like to know the best way for doing some real test (like TDD, BDD, DDD) for my code.
Any suggestion will be appreciated.

I use Jasmine https://github.com/pivotal/jasmine and would highly recommend it.
I don't think there is a "best" framework for test your code. I just know that you "should" test your code. So with that said, just find a test/spec framework your like and get testing!
I also at times use https://github.com/webadvanced/jMoney for testing small bits of code or for testing prototype code. It displays results in the console and does not require any setup or dependent files.
If you are more of a TDD guy, you can try QUnit http://docs.jquery.com/QUnit

Related

How Stryker (mutation testing framework) generate mutants?

I am looking into mutation testing and trying to integrate Stryker into my code base. My application is written in React, Nodejs and currently using Jest for client-side testing and Mocha for server-side testing. I am having a few questions regarding this:
Have anybody tried/looked into mutation testing before? Do you have any thoughts/concerns on this regarding pros and cons?
In terms of Stryker framework, I am curious how Stryker generate mutants? Is there any algorithm used in Stryker to generate mutants?
Any input would be much appreciated. Thank you in advance.
For generic Mutation Testing concepts I totally recomend Pitest (MT implementation for java) docs:
https://pitest.org/quickstart/
https://github.com/hcoles/pitest/blob/master/so_you_want_to_build_mutation_testing_system.md
I also wrote on the topic sometime ago: https://pedrorijo.com/blog/intro-mutation/
For your questions, this should give you a good idea of the existing possibilities. Not sure about striker specific details
Mutation testing is a great tool to improve test "coverage" in a meaningful way. If you want more complete regression coverage in an automated unit test suite, mutation testing can help you find holes in your test coverage, and this can help find and prevent bugs. Be aware though: You still need to think about the types of testing you need. Simply adding tests just to kill off mutants can be damaging. Your tests can end up overly coupled to your implementation this way. However, that doesn't make mutation testing bad, it just means you have to use the tool intelligently, like most tools. Another caveat is that mutation testing can be slow, but as you don't have to run mutation tests as often as the regular test suite, that's not a terrible problem.
Stryker uses specific mutators that look for code that it knows how to mutate. You can find the list of mutators in the handbook. If you are feeling adventurous, you can add your own mutators. But the basic way the algorithm works is:
parse the code into something that can be inspected by the algorithm
look for pieces of code that fit a specific template
modify that code in a way that should still be valid syntax, but changes the behavior in a way that is probably "wrong". (The simplest example is to remove behavior entirely)

Javascript Unit test

I have a javascript app within 250 lines and want to add tests to that. Whenever i make a tiny change, i have to run tests for atleast 10 cases manually, which i want to automate.
I could have gone for frameworks as suggested in different posts, but the solution i want is minimum friction and codebase. Something like a single file for unit testing may work.
Is there a way for using JS testing without any frameworks ? I want to write Unit tests / functional tests both. Or if Frameworks is the only option, what frameworks are preferred in terms os ease of plugin to existing code + learning curve.
Unit Testing frameworks are helpful because the provide environments that will run the tests and give you feedback about them. To not use one would be to take implementation of those responsibilities on yourself and would be much more work than it sounds like you are trying to do.
If you are using node, setting up a unit testing framework is very easy. I like to use Karma as a test runner with the mocha testing framework and chai assertion library.
I would go with a framework. While your use case might be simple now, you might find yourself in a project where you need more functionality down the road. You should be familiar with at least one testing framework/library for any language you use seriously. The same goes for build systems and package management.
For javascript, I've only used mocha. It's pretty comprehensive, easy to learn, can be used in browser or with node, and tests actually look really clean and easy to read.
Also, with mocha, you can choose your own assertions library, I use chai. It's built with tdd and bdd in mind, and it makes writing assertions feel more like writing natural sentences than code. Of course, you can still use whatever default assertion facilities your js environment gives you, if any.
its really hard to setup automated unit tests with plain javascript without any framework. if you try to reinvent the wheel of existing javascript unit testing framework yourself, that might be huge effort than your 250 lines of code for which you are writing test.
so if you are going for an framework, jasmine and qunitjs are promising unittesting framework for javascript.
http://jasmine.github.io/
https://qunitjs.com/

how to unit test a multi-domain cookie based javascript library?

I would like to set up a kind of google analytics like javascript library.
I would like to write unit tests for this library that will be cookie intensive on a multi-domain basis.
This seems hard to test because I cannot find a way to use the javascript unit test framework i know for multi-page load tests (domain A.B.com changes location to C.D.com or to E.B.com ...)
Is there something I am missing here ?
I may have found a terrific answer with Node.js Zombie project.
Linked to one of the Node.js testing frameworks it shoud lead to an elegant solution.
maybe not totally real-life-browser based but elegant nonetheless.

Using Javascript/jQuery is there any reason you would use YUITest over QUnit as a testing framework?

Hi i'm starting a new website and am going to be using jQuery as the library in the browser.
I was looking at testing frameworks and the obvious choice for Unit Testing in jQuery is the framework jQuery itself provides which is QUnit.
I have also looked a little at YUITest which looks very well documented. So my question is.
If you are programming the clientside in Javascript/jQuery is there any
reason one would use YUITest over QUnit as a testing framework?
Yui test is providing much more features than qunit, eg. asynchronous testing and also mocking.
Also the documentation is far better.
It's really worth to have a look at Yui test.

Looking for Info on a Javascript Testing framework

Hi Can somebody fill me in on JavaScript Testing Frameworks?
I'm working on a project now and as the JS (Mostly jQuery) libraries grow, it's getting more and more difficult to introduce change or refactor, because I have no way of guaranteeing the accuracy of the code without manually testing everything.
I don't really know anything about JavaScript Testing Frameworks, or how they integrate/operate in a .Net project, so I thought I'd ask here.
What would a good testing framework be for .Net?
What does a JavaScript test look like? (e.g. with NUnit, I have [TestFixture] classes & [Test] methods in a ProjectTests assembly)
How do I run a javascript test?
What are the conceptual differences between testing JS & testing C#?
Is there anything else that would be worth knowing?
Thanks
Dave
As for jQuery testing(and Javascript testing in general) you should maybe take a look at QUint, jQuerys testing suite. You simply include a couple of javascript-files, and a css-stylesheet, write your testes in the same document(or if you want to include them of course), and open the document. The testing framework sets up the page, and lets you run the test.
As for implementation with C# and .Net I can't help you.
Already lot of questions (and replies) about this subject on stack overflow:
The most complete: What are some JavaScript Unit Testing and Mocking Frameworks you have used?
Looking for a better JavaScript unit test tool
Javascript Unit-testing?
You should at least be aware of jsUnit.

Categories

Resources