How do I make a suite of unit [and functional] browser tests? - intern

Intern says it allows browser unit tests, in addition to functional tests running over webdriver. IIUC a unit test can assume it's running in a browser and freely access document and window etc.
If I write a set of such browser unit tests, how do I run all of them and collect the results into one report? And how would I run a set of unit tests and functional tests combined together?

All test suites that are defined in the suites key of your your Intern configuration are loaded and reported using whatever the reporter is that you’ve configured regardless of platform. The same is true of functionalSuites when running tests using the runner. Running tests all at once is explained in the multi-platform section of the running tests documentation.

Related

How to run the same tests with different configuration in jest?

I have a test suite and because it contains some expensive tests, I disable some of them for our CI. However once a day, I'd like to run the whole test suite.
The issue is that running against the same set of test files, it causes snapshot failures because when running the whole test suite it is missing some. If I generate them, then the CI fails because it complains about snapshots being removed (i.e. the one from the whole test suite that are not being checked on the CI.)
What would be the proper way to handle this with jest?
Thanks!

Can Jest be used as browser based testing tool?

I understand that Jest is a unit testing tool for developers used for JavaScript. Is Jest a browser based testing tool similar to Selenium or a functional testing tool?
As you mention, Jest is meant to be a unit testing tool. Normally you'd write small tests for parts/components of a web-page. I'm not exactly sure what you mean by "Is Jest can be used as Browser based Testing tool?" but I've found there are two relevant areas where Jest can come into contact with browser based testing
You can use a virtual DOM (like JSDOM) to render your components and test them in an environment similar to a browser. These are still unit tests but you'll have access to window and document and can test things like document click, window navigation, focused element etc.
You can debug your Jest tests in browser. Follow the instructions here if that is what you want. I've tried this but it was really slow and not very useful for me so I wouldn't recommend it
You can probably render your entire application and test it with Jest, but I wouldn't recommend that either. Jest tests should be designed to run fast and should only tests small units of your code. If you try and build tests that take a long time to run then there is an argument stating that your unit tests will become useless and developers will eventually not run them anymore.
If you are looking for tests that start an actual browser and click around like a user then have a look at Selenium which I would think is the most common approach these days
This npm library can be integrated with your jest tests to run them in a browser :) :
https://www.npmjs.com/package/jest-browser
I can't say how good it is/what the cons are but it looks like it is worth a try!
Yes, you can use Jest Preview (https://github.com/nvh95/jest-preview) to debug your Jest test in a browser like Google Chrome.
You don't have to debug a long HTML text when using Jest Preview anymore.
Read more at https://www.jest-preview.com/docs/getting-started/intro

What is the difference between mocha and Selenium?

I started using Node.js and was looking for a testing framework.
I found :
Mocha
Selenium
I understand that with Mocha one could to write tests in JS while with Selenium, one has to write tests with lower level languages like C#.
Apart from that is there something Selenium can do that Mocha can't?
What use does Mocha have by itself?
Mocha and Selenium both deal with testing software but they solve different problems.
Mocha is test running framework. You tell Mocha what tests you have and what tests you want to run and mocha will run your tests and report those that passed and those that failed. Mocha by itself provides a test running framework. You'll typically want to use an assertion library with it, like Chai. I have test suites where the only libraries providing testing support are Mocha together with Chai. This is a perfectly viable use-case.
Selenium is a library for controlling browsers. A major part of its scope is testing browser-based software. However, it can also be used for scraping web sites. This is something that Selenium can do that Mocha cannot do, by itself. Conversely Selenium is not a test running framework. Selenium has no facilities dedicated to delimiting tests and running only specific tests. You have to rely on a test running framework like Mocha to delimit one test from another.
If you want to write a test suite that tests a browser-based application you could use Mocha together with Selenium. Or Jasmine (another test running framework) with Selenium. Or you could use Behave (a Python based test runner) together with Selenium. Or you could use Mocha together with some other library that controls browsers.
This specific question needs special treatment:
I understand that with Mocha one could to write tests in JS while with Selenium, one has to write tests with lower level languages like C#.
I would not call C# a lower-level language. At any rate, using Mocha you'd have to use JavaScript. (There's a testing library for Ruby also named "Mocha" but which is not a Ruby version of the JavaScript one. I'm assuming you are talking about the JavaScript one, which makes my response a tautological but here we are.) You can use Selenium with JavaScript, Python, C#, Java and a bunch of other languages.

Unit testing vs Integration testing of an Express.js app

I'm writing tests for an Express.js app and I don't know how to choose between unit tests and integration tests.
currently I experimented with:
unit tests - using Sinon for stubs/mocks/spies and Injects for dependency injection to modules. with this approach I have to stub MongoDB and other external methods.
I thought about unit testing the individual routes and then using an integration test to verify that the correct routes are actually invoked.
integration tests - using Supertest and Superagent, much less code to write (no need to mock/stub anything) but a test environment should exist (databases, etc..)
I'm using Mocha to run both styles of tests.
how should I choose between those two different approaches ?
You should probably do both. Unit test each non-helper method that does non-trivial work. Run the whole thing through a few integration tests. If you find yourself having to do tons and tons and tons of mocks and stubs, it's probably a sign to refactor.

Running Unit Tests on NodeJS with Jenkins

How might I setup Jenkins to start my Node.JS Testing Server, run Unit Test probably JSTestDriver/Jasmine, and then top Node.JS?
I can start Node.JS using Execute Shell, but it "hangs" the build (expected since Node runs as a daemon)
I've written an article that explains how I've managed to run my unit tests with Mocha and ant.
The basic idea is:
Set your environment variables so that jenkins can control them
Use mocha's xunit reporter to generate a result report
Use Jenkins' Publish JUnit test result report option to read the result report.
You can read the full article here.
JSTestDriver outputs it's results in a xUnit compliant xml format. Which the Hudson 'xUnit' plugin can then interpret.
We are planning to integrate jsTestDriver into Jenkins. To test our javaScript code in a selection of supported browsers.
there is a plugin to execute nodejs with possibility to choice the nodejs version
https://wiki.jenkins-ci.org/display/JENKINS/NodeJS+Plugin

Resources