Can I run Jasmine tests through Mocha - javascript

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.

Related

How to test Angular 6 with jest and cucumber?

I have been trying to test an Angular 6 application with Jest and cucumber.
I currently using this module with the application https://www.npmjs.com/package/jest-preset-angular but would like to use cucumber js https://github.com/cucumber/cucumber-js with it and use feature files with the tests and have them in steps
Can this be done?
And how would I go about this?
There are several ways of doing it:
protractor: https://www.npmjs.com/package/protractor-cucumber-framework (Medium)
cypress: https://www.npmjs.com/package/cypress-cucumber-preprocessor (Medium)
jest: https://www.npmjs.com/package/jest-cucumber
Protractor is kind of old so I would go with cypress. Jest is an alternative to Jasmine, it is faster (some comparison).

How can I configure popular JavaScript unit test frameworks to co-operate with Jenkins?

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.

How to make `it.only` environment-aware using mocha?

Mocha supports it.only syntax to reduce the amount of tests to run. In our team, a developer might use it.only temporally within a code base.
Now it happened that such an only test was approved and deployed by accident. This led the build server to happily run only one test and to declare that build a success.
My question now becomes: Is there a way to tell mocha:
It should only allow only on a developer's machine? Can it be make environment aware so that either all tests always run on a build machine or an only test case would also declare the job a failure.
(I know that there are different ways to reduce the amount of tests run by mocha. E.g. within WebStorm one can run a subset of tests within a project without changing code. The scope of the question is to allow for the fact that a developer might use it.only and they shall be free to do so. I want to detect if such a change might sneak its way into the codebase though.)
On your CI, you should run mocha with the forbid-only flag.
--forbid-only causes test marked with only to fail the suite
Depending on your setup it may look like:
mocha --require babel-register --recursive tests/unit --forbid-only
A developer still may chose to run the test suite without the flag.

How do I integrate jasmine into mocha?

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.

Using testacular is it possible to run Jasmine and Mocha through tests at the same time?

Using testacular is it possible to run Jasmine and Mocha through tests at the same time using the same configuration file 'testacular.conf.js and running it in intellij?
OR can you only use one at a time? So only Jasmine OR Mocha?
No worries. So far I found out it is impossible but that is ok. I am using chai to replace jasmine. However, now i have problems with angular-mocks.js being understood. New problem woo

Categories

Resources