Node.js unit testing [closed] - javascript

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
Are there any good Node.js (server-side JavaScript) unit testing frameworks currently out there?
I'm looking for something a little deeper than the provided Assert module.

I ended up using Nodeunit and am really happy with it.
I was using Expresso originally, but the fact that it runs tests in parallel caused a few problems. (For example, using database fixtures doesn't work well in this situation.)

I was also looking for a decent test framework for node and found Mocha. It is the official successor to Expresso and seems very mature.
It allows to plug-in different assertion libraries, it offers reporters for code coverage and other things (you can plug-in your own). It can run synchronously or asynchronously and it has a concise API.
I will give it a try and report back...
EDIT:
After an incredible amount of time dedicated to other projects I finally came back to a JavaScript project and had time to play around with mocha. I can seriously recommend using it. The tests read very nicely, integration with Gulp.js is great and tests run very fast. I was able to setup automatic standalone as well as in-browser (Browserify) test runs and corresponding code coverage reports in about half a day (most of the time spent on understanding how to use Browserify from Gulp.js). To me, Mocha seems a very good choice for a testing framework.
UPDATE:
I am still very convinced about Mocha. Integration with Chai allows to plugin different assertion styles. You can checkout a working setup in this GitHub project. I am using it with karma now, integrating code coverage report, automatic watchers and good integration with IntelliJ IDEA.

Personally I've stuck with Expresso, but there are a bunch of different frameworks out there, accommodating most testing styles.
Joyent has an extensive list; give that a go.

I've personally only used the assert module, but I also find myself wanting more. I've looked through many Node.js modules and popular unit testing frameworks are Nodeunit and should (which is made by the same guy as Espresso (maybe an updated name?)
Vows also looks promising.

vows is a solid unit testing library for Node.js, but the syntax is tedious.
I've written a thin abstraction called vows-fluent which makes the API chainable.
And I've written another abstraction, [vows-is] which builds on vows-fluent and exposes a BDD style syntax.
An example would be
var is = require("vows-is");
is.suite("testing is fun").batch()
.context("is testing fun?")
.topic.is("yes")
.vow.it.should.equal("yes")
.suite().run({
reporter: is.reporter
});
More examples.

I think among various testing frameworks available, Mocha is the most latest, and very simple to implement. Here is a wonderful tutorial about how to use it:
How to build and test your Rest API with Node.js, Express and Mocha

If you are familiar with QUnit, you could use node-qunit which is a sort of a node wrapper around QUnit's existing framework.

test-studio is an npm package that provides a powerful, web based front end for unit testing. It supports things like executing individual or groups of tests and stepping node-inspector into individual tests. It currently supports mocha and more frameworks will be supported in future given demand.
Read more about it here.
Disclaimer: I am the author.

Originally made for Node.js, deadunit is a JavaScript unit testing library for Node.js and the browser. Some of its unique attributes:
Easy learning curve
Can output test results on the command line (colored or plain-text) or as HTML
It prints out the actual lines of code where your assertions are, so your output makes sense even if you don't spend a lot of time writing test commentary
It has a simple count assertion that makes dealing with expected exceptions and asynchronous asserts easy
it prints out exception and any attached data they have
it'll let you know if your code is hanging (something you don't want, but usually goes unnoticed)
Has an event driven API enables streaming test results across a network, or in any way you want.
Supports testing with node-fibers

I just uploaded a project I am using to unit test Node.js with Karma and Jasmine: Narma.
Your Node.js modules get loaded into a node-webkit browser, so you can execute Node.js modules and use libraries like jQuery in the same heap.

Related

What is a proper way of end-to-end (e2e) testing in Vue.js

Of cause I can use selenium-standalone with xpath to test an app. But testing SPA could be challenging sometime.
But, for example angularjs's team provides protractor for this purpose.
The reason behind protractor as I can see is that protractor waits till angularjs will be loaded and few more features:
Protractor provides some new locator strategies and functions which
are very helpful to automate the AngularJS application. Examples
include things like: waitForAngular, By.binding, By.repeater,
By.textarea, By.model, WebElement.all, WebElement.evaluate, etc.
So, the question is: Is it any tool or best practice for e2e testing in Vuejs?
UPD: feel free to post links to tutorials, example and everything cool about e2e-testing in vue.js. Thanks.
The tool you are thinking about is Nightwatch.
With this, you can do E2E testing with Vue.js.
Even better, this is bundled by default when you are using vue-cli, ready to run.
The command line to create a project with Nightwatch activated by default is vue init webpack myProjectName.
Here are small tutorials about it.
EDIT: Lately I used Webdriver.io a lot, and I must say I prefer it to Nightwatch (better documentation, reactive community with a live gitter, issues that are treated in a timely fashion, etc.)
I recommend to use https://devexpress.github.io/testcafe.
Pros:
easy install
complete test harness
javascript ES2016 with (async/await)
flexible selector system
smart assertions with retry policy
reports
See the simple tutorial here
I recommend Cypress.
single NPM dependency
video recording right out of the box
GUI that shows every step of the test.
Our docs are great: https://on.cypress.io/intro
For Vue specifically see this tutorial: https://vuejsdevelopers.com/2018/01/29/vue-js-e2e-test-hacker-news/ and if you want to do unit testing of Vue components https://github.com/bahmutov/cypress-vue-unit-test
Happy testing.
Seems to be an old question, but at the end of 2019 the best way is webdriverio:
Pros:
Large ecosystem of plugins and integrations.
Mocha, cucumber, jasmine runners.
Sync mode of test runner.
Allure reporter and others out of the box.
Chromedriver service from the box.
Easy integrate with selenoid and get cluster of browsers in docker for parallel test execution.
Integration with devtools protocol and puppeteer, can use huge amount of functions.
Integration with cloud service providers.
Appium integration out of the box.
Cons:
Have to manually write waits.
Some functions require to use promises.

Test Driven JavaScript Development with Django vs Node.js

I have a web project coded 80% in JavaScript and 20% in Django without a single unit testing as I rushed for Minimum Marketing Features. Now that the project is getting funded, I decided to invest some time to introduce TDD. I had a great deal of inspiration from this KickStarter-funded tutorial.
http://www.letscodejavascript.com/
The author uses Node.js, Jake, Lint, Nodeunit, and Karma to simplify the whole integration process. The server/client tests in all major browsers is done in a single command and I was really hooked to this idea, but it requires switching to Node.js.
I've searched for TDD in Django and ran into this tutorial that makes use of Selenium.
http://www.tdd-django-tutorial.com/
However this TDD was primarily based on unit testing in server. Here are the questions.
Can multiple client JavaScript testing be done in Django/Python?
I assume the answer is no since js files are nothing more than static library in Django. Correct me if I'm wrong.
Is it worth using Node.js just for the sake of TDD Javascript?
My logic was either you use Python or Node.js for the server, but since tools like Karma and Buster.js requires Node.js, I was wondering whether setting up the Node.js alongside Django just for multiple client testing is plausible choice when considering lower cost of maintenance.
Thank you :D
You can take a look at using selenium in your django test suite. Django's official docs cover this in moderate detail
To answer your question about Node.js - I would say that it's probably not worth the complexity to add node.js SOLELY for the purpose of running unit tests. Also, since your javascript is likely built to run in a browser, it's less likely that things will break down if you use a tool like selenium (which runs the code in an actual browser, providing a python scripting interface).

JavaScript library development [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
I am a java-programmer and a few months ago I discover frontend programming with javascript and html, and it is absolutely cool. So here is a js newbie's question.
Is there any IDE, default workflow or maybe best practices for javascript libs development? I didn't mean coverage and unit testing or something like this. I mean is there any tools or techniques which should I use while developing html control with some js-logic, for example? Should I write small test page in text editor and open it in dozen of browsers pressing F5 each minute and watching at browser's console or maybe there is some magic IDE which will on button press reload all browser instances and collect reports from browsers?
JavaScript Fundamentals
Be sure that you know the language. JavaScript is a bit tricky in this regard: you can easily think that you understand the language, but there are weird things that can sneak up on you. I highly recommend JavaScript: The Good Parts by Douglas Crockford. He also has some talks on Youtube with the same name that are definitively worth watching.
JSLint
Integrate JSLint or a similar tool in your workflow. It is a style checker and static analysis tool for Javascript and helps catching subtle bugs.
Avoiding F5
Check out live.js:
Just include Live.js and it will monitor the current page including
local CSS and Javascript by sending consecutive HEAD requests to the
server. Changes to CSS will be applied dynamically and HTML or
Javascript changes will reload the page. Try it!
Browser Developer Tools
Get familiar with them. I personally strongly prefer Chrome's developer tools over Firefox/Firebug, but regardless which one you choose, learn how to use the debugger.
Node.js
You should also be aware that you don't have to test Javascript logic in your browser: you can use it as any other scripting language using node.js.
As a seasoned programmer here are the steps you should follow for JS lib production.
Separate the UI from the application logic. In this case create one component for the application logic that is completely separated from all API access. API access, which means DOM and WSH and Node.js, should be separated out. I go so far as to use different files to force and ensure separation.
Create UI environments to access your logic. Have a production UI control for access by your audience and also a separate internal UI control for your sandboxed development. For example I have written my application at http://prettydiff.com/ to work from commandline, and from a browser. I have also written access methods to the application access similar to the published HTML but different for my own development for more rapid unit testing. In a sandboxed UI can use a recursive setTimeout to refresh the page in intervals for automated test plan verification as you are writing and saving code.
Focus on availability and distribution. People can get my libraries directly from my website, Github, and NPM for Node. I have a regimented process where by I upload code to the site during a production release and then perform a quick verification in the browser. If the release did not break the application then push to Github and then push this exact same location to NPM.
Distributed access to code operation is even better. Its nice having rapid and even automated access to libraries online, but being able to access unit testing via request is even better. I can remotely unit test my application's code by accessing code samples online directly by telling my application request a code sample via URI. This means that not only is my static code available for distribution and testing, but so is its operation.
Good documentation is everything. I will never even bother to examine a lib if the documentation is weak. I am not talking about code comments, though these are important. I am talking about end user documentation. I want to be able to read documentation and know everything about the subject. Node.js became popular because even before the codebase was stable it's documentation kicked ass. Get other people to QA your documentation before they QA your code. Without stunning documentation your lib is less than worthless to me.
Know your mission. Each and every lib should have a clear, simple, and well stated purpose. If this is not established then the lib is not ready for release. If an enhancement confuses the lib's purpose then it may be time to divide one library into multiple smaller libraries. Focus on precision, clarity, directness, and only the sole stated mission of the lib.
Independence is vital to adoption. I don't like libs dependent upon other libraries or frameworks. Its great that your jQuery library may be the best thing since slice bread, but I won't be looking at it. Independence means greater portability and freedom it mix and match it with other libraries that you are not aware of.
Style is important. This is touchy subject, but style is important. Keep the logic in your lib as simple and declarative as possible. If your code is absolutely declarative in nature then your algorithmic patterns are awesome. Avoid use of the new keyword unless you are an experienced JavaScript badass and severely limit use of this keyword as it will fail you in future maintenance operations. Do not algorithmically build out large strings with concatenation and continually watch the execution speeds of your code. Because even trivial changes to style or seemingly minor enhancements to the logic can destroy execution efficiency I put a timer on all my UI controls. In your development use profilers, such as Chrome's web tools to track down long operations in JS execution.
Be open about your failures. Software is never perfect and other developers will always respect this. If you encounter a bug before anybody else then be open about the existence of the bug. If it will take more than a week to get the solution out then don't delay notification. Notify your users immediately so that they are aware. I recently rolled back a major enhancement to the logic of my diff algorithm because additional unit testing showed heavy slippage. I rolled back the same day I made a decision the prior release or two was flawed and was open about the rollback. If you want people to contribute to your code base or provide bug reports then openness is critical.
I know you may have explicitly said that you didn't mean unit testing, but that is precisely the way I recommend you write javascript libraries.
If you're a Java developer, you may be familiar with jUnit. If so, qUnit may be more natural to you. Otherwise, I recommend you take a look at Jasmine or Mocha. While I prefer Mocha, my experience with Jasmine has generally been better for in-browser development thanks to the awesome Jasmine-Jquery plugin.
If you're writing qUnit tests, take a look at IntelliJ IDE which allows you to execute tests in addition to providing code coverage.
If you're developing on the browser, take a look at LiveReload. It'll watch files for you and auto-refresh your browser - great for instant feedback.
For browser compatibility, I would recommend you just get it to work on one first reasonably before worrying about the others. Check in from time to time to see if you find issues. See if jQuery can abstract some of that mess for you. Otherwise, take a look at Adobes BrowserLab.

Examples of JavaScript RIAs with unit tests

Can anyone give me examples of large-scale JS apps (including AJAX, different UI widgets, and a sophisticated architecture) with unit tests?
I'm not talking about Selenium tests here, just plain ol' stupid unit tests using mocks, decent result reporting and such.
Not sure why people voted to close, or downvoted the question. Maybe a comment would be nice.
Seriously, I've been trying hard to find unit tested web apps, since I'm having a hard time building mocks and I wonder if it's even possible with reasonable effort. It made me think about the benefits of unit tests on widgets as compared to Selenium tests. People are babbling a lot about unit tests in theory but evidently nobody actually has done it in JS-RIAs. Or have they?
Personally I like Qooxdoo, check it out for your self and see if this is what you want
http://qooxdoo.org/demo#real-life_examples
This is one good tool: http://www.uize.com/
You should look at Jasmine & Sinon.js : http://sinonjs.org/
Here is a good tutorial on testing using Backbone.js, Jasmine & Sinon.js : http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html
I also recommend Phantom.js for integration testing... It's a headless browser and much faster than using Selenium... http://www.phantomjs.org/
Btw here is an example of unit-tested app from Pivotal : https://github.com/pivotal/cimonitor. You can find client-side tests there -> cimonitor/public/javascripts/js-common
I'm not sure if this answer will qualify but I'm working on the next iteration of my pet project "Atomic OS" (an OS-metaphor for web developers) which will, eventually, meet your criteria.
I'm working on a related project (which I can't share just yet) that is built on a bare-bones Atomic OS v2 foundation and provides a rich set of UI widgets for mobile web apps.
I built & use JSDog to produce documentation from a subset of JSDoc syntax and unit test runners with QUnit.
For an example of where I'm intending to go with unit tests, please see the Atomic OS documentation. (Click "Docs" in the taskbar and select a class, such as HxJSFS)
Just one perspective:
I work on a web application that is the front end of a video analytics system. (The back end is typically an IP camera, DVR or video router running a very, very lean, embedded web server.) It uses a number of jQueryUI widgets, allows user to configure the device, create video analysis rules, and draw markup over video frames using canvas elements. I think of it as fairly sophisticated.
We use unit tests (originally written for JSUnit, but now using qunit) for a very limited subset of the code. We have unit tests to verify the behavior of business objects, including the ability to serialize/deserialize to/from XML. And we have unit tests to test the basic geometry classes we've written for the canvas markup.
However, we have no unit tests that manipulate the DOM or that verify that the elements on a page are in the correct state. Doing that correctly struck us as too difficult a problem to solve, so we rely on Selinium tests to verify that a given set of inputs will put the DOM into the correct state.

Javascript Buildmanagement - "Must have" tools?

Are there any must have tools for Java Script (RIA) development like maven, jUnit, Emma, link4j etc. for Javascript? What is the best way to set up a continous integration system for a bigger application or framework? How do projects like jQuery test their code? How to manage dependencies and different project configurations?
tools i know so far:
javascript-maven-tools (is maven the right choice?)
jslint
yuicompressor
sprockets (found it 5 mins ago)
jsunit
selenium
Hudson is a good tool for managing your build and test jobs. And it has a Selenium plugin.
Edited to provide a better answer:
Your question is quite broad, with few details about your situation or the actual problems that you've encountered, so I'll give you a quick overview based on my experience.
Build automation is a good thing. It's good to detect build failures and test failures as soon as possible. I recommend automating your tests in a standard environment so that they run as soon as possible after a commit. And I recommend making your release build as simple and reproducible as pushing a button.
Martin Fowler has a good list of recommended practices.
And a previous question contains a description of how to run javascript unit tests with CI.
Specifically regarding Hudson, we use Hudson because it's flexible, it allows a central server to manage builds on Windows and Linux slaves, and it's got plugins to support lots of different workflows and tools.
As for dependencies, Hudson allows you to trigger builds on the completion of other builds, which is good for firing off various tests after a successful product build. If you're asking how you should deal with external dependencies, you'll have to figure out what policy makes sense based on your application's design and requirements. I've seen a couple other javascript questions go by recently about how to handle such dependencies.
jQuery uses QUnit.
Another compressor is Crockfords JSMIN.

Categories

Resources