Sometimes users might need to know the name of the current test (current 'it' block and its parents). It would be nice to add the name to the object Cypress.spec
. The name should have parts of each "describe" block from the parent to the test
describe('foo', () => {
describe('bar', () => {
it('log spec info', function() {
console.log(Cypress.spec)
// {
// name: 'filter.spec.js',
// relative: 'cypress/integration/filter.spec.js',
// absolute: '/Users/janelane/Dev/web-app/cypress/integration/filter.spec.js',
// . new property "test" with block and test names
// test: ['foo', 'bar', 'log spec info']
// }
})
})
})
currently users can find the test names using helper package https://github.com/bahmutov/its-name but it is less than convenient.
I was looking for this today. I thought maybe just a Cypress.test
It would also be useful to be able to get the whole test names from the beforeEach
declared in support files.
Use case I have in mind is generating stubs for each test file, and storing them with a hash of the test they are created for
upvote! Would like to have this, too!
[SOLVED]
This worked for me:
beforeEach(function() {
console.log("=======>>>", Cypress.mocha.getRunner().suite.ctx.currentTest.title)
})
The workaround from @Doogiemuc does not work in all circumstances.
Today after updating from cypress 3.4.1 to 3.6.1 i saw a "cannot read 'title' of undefined" exception a few times.
@jennifer-shehane I am interested in a solution for this. In the beforeEach and afterEach I write logs with the testname in it. Would be nice if we must not rely on this workaround.
I already took a look into the driver package but did not found the place that needs to be modified. Can you give me just a little hint so I do not have to search the whole package for the right place?
This works for me
it('dashboard to challenge flow is visually correct', function () {
console.log(this.test.title); // dashboard to challenge flow is visually correct
});
it only gives me what is inside the it block and that's what I needed.
I'm planning on accessing the test names in beforeEach
for a larger project to conditionally skip them based on whether the test name satisfies certain requirements.
@jennifer-shehane Would you recommend sticking to the workaround suggested by @Doogiemuc? I would like to avoid the situation of depending on a specific version of Cypress incase the workaround breaks for subsequent versions.
I encountered a similar issue working on a project where i wanted to add a custom screenshot name at the end of the original cypress-generated filename, so i wrote this little function based on @Doogiemuc solution:
// utils/index.js
function getTestName () {
let cypressContext = Cypress.mocha.getRunner().suite.ctx.test;
let testTitles = [];
function extractTitles (obj) {
if (obj.hasOwnProperty('parent')) {
testTitles.push(obj.title);
let nextObj = obj.parent;
extractTitles(nextObj);
}
}
extractTitles(cypressContext);
let orderedTitles = testTitles.reverse();
let fileName = orderedTitles.join(' -- ');
return fileName;
}
This function iterates over every parent element saving all their titles in an array, then it reverses it, join it with " -- " and returns the final string.
This way i can use it like this:
import { getTestName } from '../utils';
describe('Describe One', () => {
describe('Describe Two', () => {
it('MyTest', () => {
cy.screenshot(`${getTestName()} -- something`)
// This will result in
// "Describe One -- Describe Two -- MyTest -- something.png"
})
})
})
Maybe it's not a beautiful solution, but for now it works, hope it helps
Small addition to the solution of @Doogiemuc and @Hecsall:
If you are using typescipt on Cypress you will get the error
TS2339: Property 'mocha' does not exist on type 'Cypress & EventEmitter'.
To solve this you can write
(Cypress as any).mocha.getRunner().suite.ctx.test
It's a kind of hacky...but it works ;)
Most helpful comment
[SOLVED]
This worked for me: