How do I make a Karma compliant test harness for the browser? - karma-runner

My guess is that the Karma test runner does not hook in Mocha and Jasmine, rather the reverse.
so I assume Mocha and Jasmine call something like this:
window.__karma__.testCasePassed(data);
window.__karma__.testCaseFailed(data);
and then Karma will report these events.
I am writing Suman and I want to figure out if I can make Suman Karma compliant.
When I look through the Mocha and Jasmine codebase however, I am not seeing the evidence I am looking for.

You won't find anything in the code of Mocha or Jasmine themselves as they don't know anything about Karma. When you use Mocha and Jasmine in Karma you have to use karma-mocha and karma-jasmine. These are the packages that bridge Mocha and Jasmine on the one hand, and Karma on the other.
If you look at karma-mocha, you'll see that it has a function called createMochaReporterConstructor which creates a Mocha reporter that calls function on window.__karma__. (You can see it called here and the first argument is window.__karma__.)
There is a bit of documentation about the Karma framework API on this page (search for "Karma Framework API" to get to it). It is a very brief description though. If it were me, I'd look at the code of karma-mocha and karma-jasmine to have a better idea of how to use it.

Related

How to skip dependent feature file when parent feature file fails in protractor cucumber

I have 5 test scenarios in my 5 different feature files.
TC-1
TC-2
TC-3
TC-4
TC-5
TC-3&TC-4 are dependent test cases when test scenario TC-3 failed automatically TC-4 should skip and TC-5 should execute how we can achieve this in cucumber any suggestions
Thanks in advance.
I never worked with cucumber closely, but I found out it's not possible in jasmine and pytest. So I assume this is how all test frameworks work.
The problem is that both of these^ build a queue of tests to execute before the browser started. And you can't modify it based on a runtime results.
see this answer for jasmine, and see if you can apply this approach to cucumber Nested it in Protractor/Jasmine

How to test a Sails.js app using Jest instead of Mocha?

The documentation for Sails 0.12.11 explains how to set up testing using the Mocha framework. I would like to use Jest to do that.
I tried using the same code for bootstrap.test.js, but replacing before with beforeAll and after with afterAll; replacing this.timeout(5000) with jasmine.DEFAULT_TIMEOUT_INTERVAL = 5000. Because Jest expects at least one test in a file, I made a dummy test there to make sure it did not complain.
Then, in another file (some.test.js), I tried to reference a dummy service that I created just to see if things work. With Mocha, any Sails service should be available by default, without any require statement.
it('should call the dummy service fine', () => {
expect(DummyService.doSomething()).toBe('ok');
});
Upon running the tests with jest tests/*, I come across the following log:
Furthermore, the dummy service test fails because it says it is not defined.
FAIL tests/some.test.js
● should call the dummy service fine
ReferenceError: DummyService is not defined
Jest does seem to be well suited to tests sails.
Jest philosophy is to have many tests running concurrently with independent state this means jest has no way to use global setup file.
Using jest for sails would mean :
Starting an instance of sails per test which could be memory hungry if you've got many test.
Customize its orm (waterline) so that it uses a random file for the database for each test instance to accomplish clean state.
Now you may argue that sails does it wrong and could be made from the startup to support the kind of testing jest promote.
I hope this is still relevant.
I won't lie you. It was not easy :(. It took me all the day but after all I was able to configure
Eslint
Prettier
Jest
check how it all works in my repo and feel free to contact me if you have any doubt.
https://github.com/tugorez/sails-jest

Generate code coverage of coffeescript with webpack and karma

Is there any way to generate accurate code coverage for Coffeescript using Webpack and Karma? There is not much literature on the internet to get this working. I have tried to use karma-sourcemap-loader to no avail.

SailsJS Model.query() undefined in test

I find that when I call Model.query in a Mocha test, its undefined. Model.find/create etc works. Model.query works when the app runs normally. Why might that be? I am using sails-postgresql
In my mocha test bootstrap I have already lift sails.

How do I run our qunit tests from rake?

I would like to be able to issue the command rake test:qunit and to have our qunit tests run. Is this possible? Can it be done without opening a browser window?
You could integrate PhantomJS into rake. PhantomJS can run QUnit tests and provides useful results, as its using an actual browser engine (WebKit). PhantomJS has an example on integrating QUnit.
This looks promising: https://github.com/jodosha/hanoi