mocha paralel test without copy pasting test files - selenium

To run the same mocha test file multiple times I have to copy paste it inside the current folder, is there a better way to tell mocha to run the current single file 10 times parallelly ?
I'm using this command for execution : mocha --no-timeouts --parallel

Related

Limiting run to only one file does not work

I just installed Cypress and was test running it.
Running npm run cy-run will run all test files which takes quite a lot of time and can become confusing.
Note that I have not added a single test of mine. The tests are the default examples coming from Cypress installation.
When attempting to limit to a single file I found several sources - including this question - that all seem to agree that the following would limit the run to just one single file:
npm run cy-run --spec cypress/integration/2-advanced-examples/viewport.spec.js
But Cypress does not care and goes on to pick up all tests and run them:
Instead of trying to run this from the command line, rather just - while writing and running your tests - prefix the only chain to it.
Example, change this:
it("should do stuff", () => ...);
to this:
it.only("should do stuff", () => ...);
You can add this to describe.only as well if you want to run a whole suite - or in your case, file - alone.
Another Option:
If you'd like to only run tests that you've written, you can either just remove all those example files or change describe to xdescribe or it to xit and cypress will skip running those specified tests.
Command Line Solution:
You're missing --, add that in and it should work as per your solution.
It should be written like this:
npm run cy-run -- --spec cypress/integration/2-advanced-examples/viewport.spec.js

Quit out of test file on failure, but not entire test suite

First of all, my terminology may be incorrect with some of these terms as I am very new to jest so if that is the case I would love to be corrected to help me learn.
In case I am using the jest terminology incorrectly, here is what I mean:
Test Suite - The entire group of test files I am attempting to run
Test File - The actual .js test file that is being run
Test - The individual 'it' code blocks in each test file.
Currently, I am using a group of around 20 jest tests to test my API EPs for my SQL Server and its corresponding linked server.
To do this, I run an npm command like so in the terminal.
npm run test:file ape/linked -- --env=monke.env
With how jest currently is working, if one of tests in the 20 test files fails, then it quits out of the test suite entirely.
I would like it to just fail out of whatever test file it is in, then continue to the next text file.
I know jest currently has the --bail flag, but enabling this continues the same test file on failure which I can't have happen due to the nature of my linked server to my actual SQL server.
Any help would be greatly appreciated and I am new to all of this so let me know if more info needs to be included.
This will be running on various mac versions as well as Ubunutu server

only 1 file is ever executed under my tests/ folder for TestCafe

I have multiple test_*.js files underneath a /testcafe/tests/folder. When executing TestCafe from commandline OR via Visual Studio, only ONE file is ever executed....why would that be?
tests (folder)
test_somefilename.js
test_someOtherfilename.js
test_onlyfileexecuted.js
test_anotherfilename.js
When running the following command, only 1 of the files is ever executed:
testcafe <pathToTestFolder>/tests/*.js
this will only ever execute the file named test_onlyfileexecuted.js
WHY? I can manually type in the command to execute every single file and TestCafe works perfectly, but when doing glob pattern, that is the only tests found/executed....So Confused
Just run testcafe {browser} <pathToTestFolder>/tests/ (no *.js) and Testcafe will automatically pick up all the tests in that directory

How to generate mocha test case report and show using Jenkins?

I am doing mocha unit testing for my JavaScript functions. I am running mocha in a browser not using Node. I am using require.js to load files.
When I do mocha.run() it shows reports in the browser.
Now I want to make a Jenkins job to display the report.
So how do I generate the report file so that I can provide it to Jenkins?
Running the command:
$ npm install mocha-junit-reporter --save-dev
Will generate an XML file that you can give to Jenkins
Run the command
$ mocha ./test.js --reporter mocha-junit-reporter --reporter-options ./test-results.xml
For docker file use below command
CMD ["mocha", "./test.js" , "--reporter", "mocha-junit-reporter", "--reporter-options","./test-results.xml"]
Both command will do the same.
test-results.xml file be generate in the folder .
I am answering my own question, I have solve this problem by using NGINX server which is open source.
I have added form tag in index.html of mocha and wrote on function on submit form to run mocha suit, this will return you output , parse that output and make file of that according to your need like total success count, failure count etc. and gave to Jenkins.

Running a set of actions before every test-file in mocha

I've started recently working with mocha to test my expressjs server.
My tests are separated to multiple files and most of them contain some duplicated segments (Mostly before statements that load all the fixtures to the DB, etc) and that's really annoying.
I guess I could export them all to a single file and import them on each and every test, but I wonder if there are some more elegant solutions - such as running a certain file that has all the setup commands , and another file that contains all the tear-down commands.
If anyone knows the answer that would be awesome :)
There are three basic levels of factoring out common functionality for mocha tests. If you want to load in some fixtures once for a bunch of tests (and you've written each test to be independent), use the before function to load the fixtures at the top of the file. You can also use beforeEach function if you need the fixtures re-initialized each time.
The second option (which is related), is to pull out common functionality into a separate file or set of files and require that file.
Finally, note that mocha has a root level hook:
You may also pick any file and add "root"-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has an implied describe() block, called the "root suite").
We use that to start an Express server once (and we use an environment variable so that it runs on a different port than our development server):
before(function () {
process.env.NODE_ENV = 'test';
require('../../app.js');
});
(We don't need a done() here because require is synchronous.) This was, the server is started exactly once, no matter how many different test files include this root-level before function.
The advantage of splitting things up in this way is that we can run npm test which runs all tests, or run mocha on any specific file or any specific folder, or any specific test or set of tests (using it.only and describe.only) and all of the prerequisites for the selected tests will run.
Why not mocha -r <module> or even using mocha.opts?
Have your common file in, say, init.js and then run
mocha -r `./init`
Which will cause mocha to run and load ./init.js before loading any of the mocha test files.
You could also put it in mocha.opts inside your tests directory, and have its contents be
--require ./init