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

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

Related

No possbile to set "--workers" via NUnit Engine API

Nunit command line has arg --worker to set LevelOfParallelism
We are running the test programmatically via NUnit Engine (https://docs.nunit.org/articles/nunit-engine/Getting-Started.html)
And I could not find a way to set "worker" in Test Engine or Test Runner
Maybe someone knows how to do this?
I've Google and debugged Test runner - could not find anything
UPDATE:
package = new TestPackage(arguments.Value.testDllPath); package.AddSetting(FrameworkPackageSettings.NumberOfTestWorkers, 8);
Short answer...
Add a setting to the TestPackage you use to get a runner from the engine named "NumberOfTestWorkers" with the value set to the number of workers you want the framework to use.
Details...
This is not actually part of the engine but part of the NUnit framework. The engine is generally framework-agnostic, that is, it doesn't assume you are running tests with the NUnit framework. However, it passes through any settings on the package, which it doesn't understand. It's up to the test framework in use to interpret them.
That's why you can't find the details in the engine documentation, although it could probably be improved by adding a paragraph like the above somewhere. :-)
Bear in mind that writing your own test runner using the engine directly is a somewhat advanced activity. I think the engine docs give you a lot of info about how to get started but you will also need to examine source code of existing open-source runners. For example, looking at the console runner itself, you could have seen this code (reformatted to fit):
if (options.NumberOfTestWorkersSpecified)
package.AddSetting(
FrameworkPackageSettings.NumberOfTestWorkers,
options.NumberOfTestWorkers);
The class FrameworkPackageSettings is part of the runner and includes settings used by the NUnit3 framework and exposed by the runner.
Good luck!

sails.getDatastore is not a function

I'm currently in the middle of upgrading our API from v0.12 of Sails to v1. Not the easiest task, but will be worth it.
The current problem I'm having, is converting our old "ModelName.query" calls to the new style, which is supposedly "sails.getDatastore". Great, fine.
Except, that when trying to do this in config/bootstrap.js, I constantly get the error "sails.getDatastore is not a function".
Yes, I am using the default sails-hook-orm, the .sailsrc has it turned on explicitly; and yes, I have globals turned on.
Is the problem that the function isn't registered until after bootstrap? Because that is not an option for us; bootstrap is validating our database schema before lift (custom code, using native queries), so our production servers fail to deploy if we missed a database update. It eliminates a ton of human error.
Thanks for taking the 1.0 plunge!
I'm not sure what you mean by the "default" sails-hook-orm -- that hook is installed directly as a dependency on each Sails 1.0 project -- but I can almost guarantee that the version you're using is not correct. I would do:
npm cache clean
npm install sails-hook-orm#beta
in your project to make sure you get the latest (currently v2.0.0-21). It adds getDatastore to the app object when it initializes.

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.

Structuring webpack config for use with karma testing

I would like to create a test suite which will be run with karma against my app which is using webpack to build itself. I have two entrypoints, app and vendors. These are set up through my webpack.config.js file here. The resulting bundle.js should contain both of these entrypoints in its generated file.
My karma (mocha) tests residing in test/spec/*_spec.js are currently pointing to specific components, via require statements like:
var app = require('../src/scripts/App')
They also utilize react/jsx which seems to be causing problems during the test run where I get jsx errors:
Module parse failed: /Users/dmarr/src/status/test/spec/app_spec.js Line 10: Unexpected token <
You may need an appropriate loader to handle this file type.
I'd like to keep test runs quick as possible as well as fast build times for testing with webpack-dev-server during development by minimizing babel transforms to only where needed.
What do I need to do in karma.conf.js to get my builds working? Here is the karma.conf.js file I'm playing around with.
Note, that I do have this working without breaking out the vendor bundle here: https://github.com/bitwise/status
Thanks for any help,
Dave
In a similar setup, disabling CommonsChunkPlugin (for testing only) worked for me. Give it a shot!

How to ban moreunit for executing automatically the tests?

I install "MoreUnit" as a plugin in eclipse. but, when starting eclipse, tests will be launched automatically. This presents a problem for me, because the tests inclurent of the heads of CRUD. Therefore, because of this automatic startup, the database will be empty after a certain time.
How to ban moreunit for executing automatically the tests ?
MoreUnit is a tool to assist in unit testing. If your tests are doing anything with a database, they are not unit tests. The reason for this is that if you test your class with a real database connection, you are also testing the database along with your class.
You should decouple your dependency on the database with a mock (see my answer here for an idea how to do this).
If you are doing data-driven tests, then it would be better to use a tool such as DbUnit to drive your tests rather than relying on a real database connection. With such a tool, you will have control over the data for each test and won't have to worry that tests fail because someone else updated data in the database or that you executed your tests in "the wrong order".