Running test cases selectively with Mocha - javascript

There is a total of 20 test case files. I want to test a particular set of 10 test cases. Is there any script file or any other method to run the test cases selectively with Mocha?

There are two principal ways to specify a subset of tests to run:
You can give Mocha the name of the file that contains the tests you want to run:
$ mocha path/to/file
It is possible to give paths to multiple files if needed. For instance, if you have 10 test files and want to run all the tests from only 2 of them, you could give the paths of the 2 files.
This method relies on you splitting your tests into separate files according to a logic that suits your situation.
You can use the --grep option:
$ mocha --grep pattern
The pattern is a regular expression that Mocha will use to test each test title. Each test for which the pattern matches will be run.
The two methods could be combined to run only tests that match a pattern and that are from one specific file: $ mocha --grep pattern path/to/file

Following command works for me
$ node_modules/.bin/mocha test/file1.js test/file2.js

mocha.describe("test1", () => {
mocha.it("test1_1", (done) => {
done();
})
mocha.it.only("test1_2", (done) => {
done();
})
})
OutPut :
test2
✔ test2_2 //because of it.only

Related

How can I use modules with mocha ? My mjs files get an error

With commonjs and using require() for .js files and a command of
mocha `**/*.spec.js`
Files:
capitalize.js
test/capitalize.js
my tests run
Output:
mocha
Capitalize sentences
✓ calls the function
✓ calls the function
✓ calls the function
✓ Works for a second sentence
✓ Handles 3 dots
✓ Handles existing capitalization dots
✓ Preserves existing last dot
✓ Preserves carriage returns ("\n")
I want to use ES6 modules. So I:
changed both file extensions to `.js from .mjs
changed file I an running (the test) from
export function capitalize(paragraph) {
to
exports.capitalize = function(paragraph) {
changed file I include from
export function capitalize(paragraph) {
to
exports.capitalize = function(paragraph) {
However, mocha isn't recognizing my tests and I get
Error: No test files found: "**/*.spec.js"
// Not too surprising given I changed the file extensions
If I now changed package.json from
"test": "mocha **/*.spec.js"
to
"test": "mocha **/*.spec.mjs"
I get
$ mocha
Error: No test files found
from mocha directly, or with npm test I get
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
How to fix?

Cypress: run only one test

I want to toggle only running one test, so I don't have to wait for my other tests to see the result of one test.
Currently, I comment out my other tests, but this is really annoying.
Is there a way to toggle only running one test in Cypress?
to run only one file
cypress run --spec path/to/file.spec.js
or using glob patterns:
cypress run --spec 'path/to/files/*.spec.js'
Note: you need to wrap your glob patterns in single quotes to avoid shell expansion!
to run only one test in a file
You can use a .only as described in the Cypress docs
it.only('only run this one', () => {
// similarly use it.skip(...) to skip a test
})
it('not this one', () => {
})
Also, you can do the same with describe and context blocks
edit:
there's also a nice VSCode extension to make adding/removing .only's easier with keyboard shortcuts. It's called Test Utils (install with ext install chrisbreiding.test-utils). It works with js, coffee, and typescript:
There are multiple ways of achieving this.
You can add .onlyto it or describe see #bkucera answer
You can do it from the terminal as explained in the doc here
npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
You can mute not needed test suites and particular cases by prepending x to testrunner methods call (describe, it, etc.)
So it would look like:
// this whole testsuite will be muted
xdescribe('Visit google', () => {
it('should visit google', () => { cy.visit('https://google.com/'); });
});
// this testsuite will run
describe('Visit youtube', () => {
it('should visit youtube', () => { cy.visit('https://youtube.com/'); });
// this testcase will be muted
xit('is not necessary', () => { ... });
});
You can run the test like this.
cypress run --spec **/file.js
The best way to do such kind runs are by using the .only keyword that cypress provide.
To run all the test cases in one describe function from many describe functions add the .only in the required describe.
describe("1st describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe.only("2nd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe("3rd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
So here only the 2nd describe will run.
Similarly if you want to run some test cases in 1 describe add the .only in front of all the test cases that you want to run.
describe("describe statement", () => {
it("Should check xx", async function(){
});
it.only("Should check yy", async function(){
});
it.only("Should check zz", async function(){
});
});
So here the it for yy and zz will run
This is similar to the fit and fdescribe in karma and jasmine that you might be familiar with.
You can skip the test in cypress with it.skip or xit
There is one way I have found to skip tests which I don't need to run (in the current test), and that is to use: this.skip();
it('test page', function () {
// skip this test for now
this.skip();
cy.visit('http://example.com/')
cy.contains('test page').click()
cy.url()
.should('include', '/test-page/')
})
1. it is important to use regular function as second argument of it, this will not be available in arrow function
2. Whole of the test will be skipped no matter where we write this.skip()
My test files have a structure like this path/something.test.jsx and commands npx cypress run --spec path/something.test.jsx gives the following exception in the terminal:
Can't run because no spec files were found.
We searched for any files matching this glob pattern:
...
Surprisingly enough the following works and run the test exactly for one file (providing you have jest installed):
jest path/something.test.jsx
A very easy solution is to prefix your tests in with numbers, as testing frameworks will typically will run tests in alpha/numeric order by default - so if I have to check one spec file - I will copy the contents into a file 0-[file-name].spec and re-run the test command. Once the test completes - you terminate the test run - as you will have the results you were looking for. This answer is targeted at projects where your testing framework is abstracted and as a developer, you do not have all available options for your testing framework. Not the best answer, but it works and is intuitive and super easy to do. I have found this to be a way to avoid adding a bunch of conditional skips() or only() calls that will not make it to production, will have to be removed and you can easily add the file pattern to .gitignore file so these local files do not get checked in.
The best-known solution for that already exists and requires adding just one simple argument in the console.
https://github.com/cypress-io/cypress/tree/develop/npm/grep
Simply run:
npx cypress run --env grep="TestName" --spec "filename"
Cypress .only() function is used only for development.
put .only for the test you want to execute and then run the spec as npx cypress run --spec path/to/your-file.spec.js
To run a specific file through Terminal:
npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
You can use this
cypress run -- --spec 'path/to/files/*.spec.js'
or
npm run --spec 'path/to/files/*.spec.js'
It worked for me.
Many thanks
use the #focus keyword in the test scripts when execute using cypress open

How can I configure the jasmine's random on gruntfile?

How can I configure the random option using grunt-contrib-jasmine? I can do it directly with jasmine's command line, but running jasmine's task by grunt-cli I didn't find the random option. Then the output of command line always shows the specs' randomic output.
I found the answer to my question. At least I've tested and it worked.
On the each describe declaration's top, you can configure the random option of your Suit Test. It can be with the following statement:
describe('My suite', function(){
jasmine.getEnv().configure({random:false});
// There are several tests here...
afterAll(function(){
jasmine.getEnv().configure({random:true});
});
...
If you use jasmine.d.ts and your tests are in typescript, you could also add to the Env interface in jasmine.d.ts a funtion like:
interface Env {
// some code
// add function:
configure(b: any): void;
}
Then in your tests you could write something like:
/// <reference path="../../../../typings/jasmine/jasmine.d.ts" />
jasmine.getEnv().configure({ random: false });
I tested this approach and in the end I didn't have to set the random option to false in each describe function. I added it right after the reference paths and it worked for all tests.
Edit: You could also include the jasmine configuration in the options/helpers part of your grunt-contrib-jasmine task as a separate file. Something like:
jasmine: {
src: [some source files],
options: {
specs: [some spec files],
helpers: 'helpers.js'
}
}

How to run same test file twice in protractor?

I have a situation where I want to run the same test file twice. Let's say I have a test1.js and login.js and I define my suite in such a way in configuration:
specs: [
'test1.js',
'login.js',
'test1.js'
]
So as you can see I want to run test1.js twice, but protractor runs test1.js, login.js and then finishes. Do you have any idea how I can achieve this?
Regards
Adam
Protractor is globbing all of your specs file patterns to get the list of tests it should run, so there's no way to make this work via the list of specs. What I would do instead is use node's require to organize your tests:
// In your configuration file
specs: [
'thetest.js'
]
// thetest.js
require('test1.js')();
require('login.js')();
require('test1.js')();
// test1.js
module.exports = function() {
describe(...)
};
After reading all the above answers, i achieved this"running same test case multiple times" in most stupidest way :D
In a folder i copy pasted same testcase, 10 times and renamed them like below:
test1.js
test2.js
test3.js
.
.
test10.js
then in my config file
// In your configuration file
specs: [
"./path to folder/**/TC*_spec.js"
]
It run the test 10 times :)

How can I have mocha reporter for protractor?

I'm using protractor (0.22.0) for testing my app.
Is this possible to have a mocha-style reporter instead of the basic jasmine one?
It currenlty looks like this:
(....F...)
And I'm looking something more like:
my set of tests 1
my test 1-1
my test 1-2
my set of tests 2
my test 2-1
my test 2-2
See the response here: Custom Jasmine reporter in Protractor tests
I'm using this module and it works perfectly: https://www.npmjs.com/package/jasmine-spec-reporter.
Check out the Frameworks Protractor docs. Once you've installed Mocha, you can set Mocha options in your .protractor.conf.js file:
mochaOpts: {
reporter: "spec",
}
You can use tap file. Its pretty good. https://github.com/proverma/tap-file
I'm not sure if mocha reporters work with Jasmine, but there are some other Jasmine reporters that work better than the default reporter.
You need to require jasmine-reporters. It's required to have it as a dependency. Then you can call any of Jasmine Reporters listed here in your onPrepare function inside your protractor config object.
npm i --save-dev jasmine-reporters
Using TapReporter for example. Do this inside your protractor.config.js:
onPrepare: function() {
// The require statement must be down here, since jasmine-reporters
// needs jasmine to be in the global and protractor does not guarantee
// this until inside the onPrepare function.
require('jasmine-reporters');
jasmine.getEnv().addReporter(
new jasmine.TapReporter());
},
For Jasmine 2.x and Protractor >1.6
framework: "jasmine2",
onPrepare: function() {
// The require statement must be down here, since jasmine-reporters
// needs jasmine to be in the global and protractor does not guarantee
// this until inside the onPrepare function.
var TapReporter = require('jasmine-reporters').TapReporter;
jasmine.getEnv().addReporter(new TeamCityReporter());
}

Categories

Resources