I have a large set of unit test written in jasmine-node for a Node project. I want to use Mocha for the expanded feature set but I'm pretty in bed with jasmine both for style and extensive use of spies. I have several helpers and custom code that is very jasmine dependent.
How can I use jasmine-node or the jasmine library as the framework while mocha is the testing engine? Can the two play nicely or do I have to rewrite my testing environment for mocha, chai, and sinon?
How can I use jasmine-node or the jasmine library as the framework while mocha is the testing engine?
What? Do you mean you want to code your tests against jasmine and then somehow run them in mocha? While I'm sure it's possible, it just sounds bizarre.
For a given suite of tests, it's one or the other. They have similar but different APIs so you have to choose one. Choosing BOTH in a single project is almost certainly poor judgement IMHO. Other than mocha's vastly superior async support, I can't see how you could justify using both when they are so closely related. It's just going to create a confusing annoyance for maintenance.
Suggestion: split your project apart into smaller, separate modules. Than you can port each of these smaller modules when the time is right if you want to migrate from jasmine to mocha.
Related
I want Jenkins to run JavaScript unit tests.
Although I came across different JavaScript unit test frameworks, like Jasmine or Tape, I cannot find any documentation on how to make them work and set-up their output to co-operate with Jenkins.
Can anyone point me to a documentation for Jasmine, Tape and other unit test frameworks, explaining on how to configure them properly to be triggered and evaluated by Jenkins?
There is nothing special to do with regards to Jenkins. You need to install the testing libraries as you normally would on the Jenkins executor nodes, and then you can use them as you normally use them from within your Jenkins job. Note that this may mean calling npm install from within your Jenkins job - this is pretty typical; you would do something similar whether you were using Python virtualenv, Ruby bundler, etc.
As the title says, I'd like to be able to able to run Jasmine tests using Mocha on node. As an experiment I've installed Jasmine and Mocha and ran
jasmine examples
to install the examples.
Running Jasmine runs the tests as expected:
$ ./node_modules/jasmine/bin/jasmine.js
Started
.....
5 specs, 0 failures
Finished in 0.012 seconds
But running the tests in Mocha doesn't work:
$ ./node_modules/mocha/bin/mocha spec/jasmine_examples/PlayerSpec.js
0 passing (11ms)
5 failing
1) Player
should be able to play a Song:
ReferenceError: expect is not defined
at Context.<anonymous> (spec\jasmine_examples\PlayerSpec.js:14:5)
etc
Similarly if I try and add a call to jasmine.createSpy() into a test, it works fine under Jasmine but under Mocha it reports
ReferenceError: jasmine is not defined
May be I shouldn't be entirely surprised but as I'm new to this Javascript world could someone explain to me either how to get it working or why it doesn't work?
In case anyone is wondering why I want to do this, as a team we're using Jasmine but I'm using IntelliJ as my IDE. This doesn't understand Jasmine tests so I have to manually create run configurations to run specific tests. If I could get them to run under Mocha, I could use the built-in Mocha support and just click on the little arrows IntelliJ puts next to Mocha tests.
There are superficial resemblances between Jasmine and Mocha (describe, it, etc.) but there are a lot of differences that have to be bridged if you want to have a suite run under both. You cannot take a Jasmine suite and generally expect it to work in Mocha without modifications. Jasmine is not designed to run Mocha tests and Mocha is not designed to run Jasmine tests.
For instance Mocha cannot do anything with jasmine.createSpy(). It does not even have an equivalent for it built into Mocha itself. If you were to port your suite to Mocha (i.e. abandon Jasmine in favor of Mocha), you'd have to use a library like Sinon to provide similar functionality. If you want to have your suite run both in Mocha and Jasmine, then you might be able to bridge the gap with a wrapper library that detects which runner it is running under and calls jasmine.createSpy() or a Sinon equivalent as needed but with any non-trivial test suite the work required would be substantial. (And frankly, there's no project I work on where I could justify the expense.)
You'd also have to use a library like Chai to provide expect.
Until now, I've done all my testing with just Jasmine and jQuery to automate the browser. I am starting to explore Karma but it seems to be set up only for unit tests that involve loading HTML fragments as test fixtures and I find no way to load a whole web page. Am I missing something or is this simply the wrong tool for what I am trying to do?
From the karma FAQ's section:
Can I use Karma to do end to end testing?
Karma has primarily been designed for low level (unit) testing. If it's an AngularJS app, you can use Karma with the karma-ng-scenario plugin. However, we recommend Protractor for high-level testing.
Typically, loading a whole web page goes beyond the scope of single units of code: you need to spin up a browser, load the page and make your actions and assertions. End-to-end testing frameworks like Protractor, Nightwatch.js or Webdriver.io might be more suitable for this specific case.
You can do integration testing using the following toolchain:
Karma ➔ Mocha ➔ Mocha-CasperJS ➔ CasperJS.
Check out the mocha-casperjs package.
I can run my specs with either jasmine-node or just jasmine. They both run my specs. So, what value does jasmine-node add? The readme says:
This node.js module makes the wonderful Pivotal Lab's jasmine spec framework available in node.js.
https://github.com/mhevery/jasmine-node/blob/master/README.md
I don't understand. My app runs on node, my specs require node modules .. so when I run jasmine, I'm already using both node and jasmine. What does jasmine-node add?
I specifically am not asking for opinions about why jasmine-node is your favorite, or recommendations of other libraries. I only want to know, what is the purpose of jasmine-node?
The subtitle of jasmine-node is a good answer to your question:
DOM-less simple JavaScript BDD testing framework for Node
Let's go and look at the different parts of the answer:
DOM-less simple testing framework
jasmine is a JS testing tool. At the beginning JS was just for browsers. To give an output inside the browser there is this DOM Model, which is not so easy to use. Node.js gives you the possibility to run JS also on a server. On the server-side there is no DOM. To make things faster and easier you don't need a DOM implementation for your testing tool, when it just runs inside node.js
Jasmine itself is independent of a browser, so that is an intention for both jasmine and jasmine-node.
for Node
This is easy - jasmine-node is just for node and not for browser JS.
But behind that part there is the main purpose. Because the requirements between a brwoser test and a node.js test are totaly different. Because jamsine supports both ways it can not have all features, which are possible with node. If you look at the possible arguments at the documentation you see that there are much more options inside the CLI of jasmine-node. Some of the most interesting features are maybe:
Test a file automaticaly, when it changes
test coffeescript files directly
So to give you an answer to your question:
What is the purpose of jasmine-node?
jasmine-node provides you more CLI options for you tests. It can make some work automaticaly and it uses more of the node functions to provide that. So the future way for jasmine-node will be in providing more functions, which are just able to implement, when you just test on node.js
I am trying to choose a setup for testing a webapp that uses Backbone and RequireJS.
I will try to do most of my unit testing from javascript and I am in doubt whether it is better to load my whole app before calling the testing framework or to use RequireJS to dinamycally load just the modules that are being tested by each test.
As this site is just for Q&A and not open to discussion, I will reformulate: Do you have any strong argument against not loading the whole app upfront when unit testing?
The best way is to test each of your Models/Collections/Views separately in unit test. Starting the whole app in a unit test would it made really hard to test all cases for a module. As you mention requireJS you should also mock most of the dependencies of a module using squireJS.
But you should additionally have some integration tests where you test the behavior of the whole app in a headless browser using a tool like selenium, casperJS or capybara.
With requirejs, there are two ways for you to interpret loading upfront:
During development, where r.js is not be used, and just depend on configuration on requirejs.config
After r.js optimization, where the modules can be restructure by r.js configuration