Sails.js testing with mocha only runs one test not two - node.js

I followed the sails.js testing example at http://sailsjs.org/#!/documentation/concepts/Testing . I have got it to run, but it only runs one test. The command line in package.json script.test is:
mocha test/bootstrap.test.js test/unit/**/*.test.js
"test/unit/**/*.test.js" should be catching 2 tests, UsersControllers.test.js and Users.test.js . It is only running Users.test.js . And yes, both tests are in the test/unit/ directory.
What am I doing wrong here ?

After copying the documented test cases from the sails js docs, make sure to change the following line in the User.test.js file
describe.only('UsersModel', function() {
to
describe('UsersModel', function() {
then you should see the controller test as well.

Related

Express +Jest. Test files are running sequentially instead of in parallel

I have an Express.JS server which uses jest and supertest as a testing framework.
It has been working excellently.
When I call my test npm script, it runs npx jest and all of my test files run in parallel.
However I ran my tests recently and they ran sequentially which takes a very long time, they have done this ever since.
I haven't changed any jest or npm configuration, nor have I changed my test files themselves.
Has anyone experienced this? Or is it possible that something in my configuration is incorrect?
jest.config
export default {
setupFilesAfterEnv: ['./__tests__/jest.setup.js'],
}
jest.setup.js
import { connectToDatabase } from '/conn'
// Override the dotenv to use different .env file
require('dotenv').config({
path: '.env.test',
})
beforeAll(() => {
connectToDatabase()
})
test('', () => {
// just a dummy test case
})
EDIT: Immediately after posting the question, I re ran the tests and they ran in parallel, without me changing anything. If anyone has any knowledge around this i'd be interested to get a second opinion
After intermittent switching between parallel and sequential for unknown reasons. I have found it work consistently by adding the --no-cache arg to the npx jest call.
See below where I found the answer
Github -> jest not always running in parallel

What is the command for running the test with mocha expressjs

My test file,
describe('a basic test',function(){
it('it should pass when everything is okay',function(){
console.log('hi')
})
})
My test result with the command 'mocha'
hi
․
1 passing (9ms)
But how can i get results like below,
a basic test
it should pass when everything is okay
hi
With tick mark as success,can any one help me.
The default reporter for Mocha is spec, which outputs the report like you want:
a basic test
hi
✓ it should pass when everything is okay
1 passing (6ms)
So the question is why Mocha isn't using the default reporter in your case (instead, it's using dot). The most likely reason for that is that you have a file called ./test/mocha.opts that contains this line:
--reporter dot
If you don't want dot as the default, just remove the line. If you want dot to be the default, but occasionally want to override it, pass another reporter on the command line:
mocha --reporter spec
# or shorter:
mocha -R spec

Coverage report with istanbul and mocha

I'm a newbie on Node.js. I have to set up some tests in my application and I'm getting really mad trying to generate a back-end code coverage report with mocha and istanbul in my loopback application.
Searching through thousand of dab explained articles on Github I found some good articles and then I figured out that I had to use something like this:
istanbul cover _mocha -- [path/to/test/files] -R spec
I was happy because it says: "What you are essentially doing is passing the command to run your tests to Istanbul which, in turn, will run those tests on your behalf." However, every time I try to run Istanbul, I get this error:
No coverage information was collected, exit without writing coverage information
C:\...\proj-name\node_modules\.bin\_mocha:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
My working test file is:
var userService = require('../TestBusinessLogic.js');
var should = require('chai').should();
describe('API Utenti', function() {
it('should throw Exception on missing UserName', function() {
(function() {
userService({ Name: 'Pippo', Surname: 'Baudo' });
}).should.Throw(Error);
});
});
Is this command good to use? If not, could someone please explain me how to make a coverage report using istanbul with mocha?
Figured that I was running node_modules\.bin\_mocha instead of node_modules\mocha\bin\_mocha and this solved my problem.
When running istanbul from the command line you need to run it from the root of your project directory, it by default looks for the files to run the coverage reporting for at the root of your directory.
Additionally make sure your path to your test folder is relative to your project directory.
So you should navigate to your project directory using cd and then when inside your project directory then run
istanbul cover _mocha -- ./path-to/test.js -R spec

Run several Appium Tests after another with nodejs

I'm trying to build a testsuite in which are eg. 5 testfiles named Test1.js - Test5.js.
I want to execute the testsuite which then runs Test1.js.
After Test1.js is complete it should run Test2.js.
I tried to do it with require()
.fin(function() {
console.log("Test succeeded")
require('./Test2.js')
return browser.quit();
But if the require is before the browser.quit() I can't execute the second test because the Appium server is still up and running, and after browser.quit() the require() doesn't get called at all.
I am fairly new to Appium, nodejs and Javascript in general so maybe I have overlooked something.
Figured it out myself. The problem wasn't the calling of the function but the server arguments.
I used "sessionOverride":false instead of "sessionOverride":true.

How to run jasmine-node tests with RequireJS

How can I properly run jasmine tests using jasmine-node and RequireJS?
I already tried something like this, but doesnt work (CoffeeScript):
requirejs = require 'requirejs'
requirejs.config { baseUrl: __dirname + '/../' }
requirejs ['MyClasses', 'FooClass'], (MyClasses, FooClass) ->
describe "someProp", ->
it "should be true", ->
expect(MyClasses.FooClass.someProp).toEqual true
Finished in 0 seconds 0 tests, 0 assertions, 0 failures
My goal is to write modular classes using RequireJS, CoffeeScript and classes must be testable with jasmine-node (CI server).
How can I do that please?
Thank you!
EDIT:
I executing tests with command (at directory with tests):
jasmine-node ./
Jonathan Tran is right, it's the spec in the file name for me.
I have this:
"scripts": {
"install": "cake install",
"test": "node_modules/jasmine-node/bin/jasmine-node --verbose --coffee --runWithRequireJs --captureExceptions spec"
},
in my package.json and I installed jasmine-node from inside the project npm install jasmine-node
Minimal test file called RingBuffer.spec.coffee
require ["disrasher"], (mod) ->
describe "A test", ->
it "should fail", ->
expect(1).toEqual 0
It doesn't actually work at the moment because I haven't got the project hooked up with require properly I don't think. I'll post back here when it does.
If anyone is running into this, much has changed since this question was asked. The first thing to check is still that you're naming your files like thing.spec.coffee.
But if you're running the tests and still seeing the output "0 tests", you need to make a JavaScript file with your requirejs config. This must be JavaScript, not CoffeeScript.
// requirejs-setup.js
requirejs = require('requirejs');
requirejs.config({ baseUrl: __dirname + '/../' });
Then tell jasmine to use this setup file:
jasmine-node --coffee --requireJsSetup requirejs-setup.js ./
One nice thing about this is that you don't need to include the requirejs config in every spec file.
I've tested this on node v12.16, jasmine-node v3.0.0, and requirejs v2.3.6.
It seems that jasmine-node and require.js are completely incompatible. That said, it is possible to run jasmine tests on require.js modules in node using a bit of extra code. Take a look at https://github.com/geddski/amd-testing to see how.

Resources