How to test Angular 6 with jest and cucumber? - javascript

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).

Related

How to unit test Capacitor project?

I have some JavaScript unit tests that I need to run. We use a combination of mocha and chai for the tests. We are looking into possibly switching to Capacitor as a tool to convert web apps to mobile and electron projects. In order to get Capacitor working in our unit tests we have to run the tests using jsdom as Capacitor makes use of the window object. Now the unit tests are run through jsdom these parts of Capacitor are happy and our tests pass.
However we have one module that will require the use of Capacitor's Filesystem API. Filesystem has no web implementation so these tests now fail as we are using jsdom. The log states Filesystem does not have web implementation.. Is there any way I can get a combination of having window available in our unit tests while also allowing the use of Filesystem?
I was able to fix this. I used jsdom from the terminal and it didn't work. However using the JavaScript API I was able to get that test to pass.

Can I run Jasmine tests through Mocha

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.

Difference between Protractor and Grunt?

On the Protractor site (http://www.protractortest.org/#/infrastructure), it says Protractor uses Selenium for browser automation. Then on the Grunt site (http://gruntjs.com/) it says Grunt is also for automation. If they are both for automation, I'm not sure what are the differences between the two and how they fit together. In some ways, my question is this: Why would I need Grunt if I'm using Protractor?
"Automation" itself is quite a broad term.
These are completely different tools:
protractor is a wrapper around WebDriverJS and used for end-to-end testing
grunt is a javascript task runner
You can actually run protractor tests via grunt.

How to use Protractor with Angular 2?

I use Protractor with Angular 1.x. I would like to migrate to Angular 2.0 step by step but I don't see Protractor in the docs.
Has protractor been dropped since Angular 2? Should I write my tests without using Protractor, using Jasmine instead (or others)?
You can test Angular 2 applications with Protractor (starting from Protractor 2.5.0).
For Protractor 5.0.0+, you don't have to do anything specific, Protractor will auto-detect the Angular version used in the application under test.
For Protractor >= 2.5.0 and <= 4.0.14, you would only need to add useAllAngular2AppRoots: true to your config. Here is a sample.
Note that several built-in Protractor matchers would not yet work with Angular2, see:
Protractor Angular 2 Failed: unknown error: angular is not defined
There is also that Protractor+Angular2 problem in Firefox (still unresolved), see:
Can't run Selenium via Protractor on Firefox after update to Angular 2

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