The contents of Cypress.spec are incorrect when I click on "Run all specs".
It now contains:
{
absolute: "__all",
name: "All Specs",
relative: "__all"
}
I use Cypress.spec in my cypress-plugin-snapshots plugin to determine snapshot filename. I found a very hard work around by using a preprocessor to store the latest processed file. But this breaks other stuff. It would be very useful if Cypress.spec would contain the actual current test.
It should contain the details of the spec that is running. Even when you clicked on "Run all specs".
For example:
{
name: 'filter.spec.js',
relative: 'cypress/integration/filter.spec.js',
absolute: '/Users/janelane/Dev/web-app/cypress/integration/filter.spec.js',
}
Put code below in a Spec and see contents from Cypress.spec when running with "Run all tests" and by running test directly.
console.log(Cypress.spec);
All versions that have Cypress.spec.
Similar bug with screenshot names: https://github.com/cypress-io/cypress/issues/2319
I believe implementing this would require this feature to be addressed: https://github.com/cypress-io/cypress/issues/1586
Similar bug with screenshot names: #2319
I believe implementing this would require this feature to be addressed: #1586
I think you are right. Is there an ETA?
No
Any news on this issue ?
@peter-mw in my comment on that issue https://github.com/meinaart/cypress-plugin-snapshots/issues/10#issuecomment-514459554 I show a workaround
While not retrieving the same value of spec name (which is the filename), I'm using:
const spec = Cypress.mocha.getRunner().suite.ctx.currentTest.parent.title;
const testName = Cypress.mocha.getRunner().suite.ctx.currentTest.title;
e.g.
describe('some spec', () => {
it('some test', () => {
//
});
})
Will result in spec === "some spec" and testName === "some test".
I can't use the __filename method as I'm importing my tests from another file in my specs (passing them arguments to change the configuration of the tests).
馃憢 I've been battling with this issue too. I came up with a workaround, extending upon @bahmutov's work in https://github.com/meinaart/cypress-plugin-snapshots/issues/10#issuecomment-514459554
In my case, I was copying the integration folders to another location anyway so I modified the file to include a fix for the spec:
const esprima = require('esprima');
const escodegen = require('escodegen');
/**
* Workaround for incorrect Spec being set in `All Specs`.
* @see https://github.com/cypress-io/cypress/issues/3090
*
* Parses all specs and inserts a before block in each describe block.
* The before block sets the correct spec file which will be used in all
* cases. To ensure consistency, we do this every time, even when not using
* `All Specs`.
*
* @param {String} fileContents - Contents of spec file
* @returns {String} - New spec file with added before block
*/
module.exports = (fileContents) => {
const beforeBlock = esprima.parseScript(`
before(() => {
const path = require('path');
const relative = __filename.substr(1);
const integrationFolder = Cypress.config('integrationFolder');
const absolute = path.join(Cypress.config('projectRoot'), __filename);
const name = absolute.replace(integrationFolder + '/', '');
Cypress.spec = { absolute, name, relative };
});
`).body[0];
const parsed = esprima.parseScript(fileContents);
const parsedFileWithInsert = {
...parsed,
...parsed.body.map((block) => {
if (block.expression.callee.name === 'describe') {
// eslint-disable-next-line no-param-reassign
block.expression.arguments[1].body.body = [
beforeBlock,
...block.expression.arguments[1].body.body,
];
}
return block;
}),
};
return escodegen.generate(parsedFileWithInsert);
};
A little hacky, but works for my needs whilst we wait on this issue being fixed. Hope this saves someone the headache I had 馃槄
Most helpful comment
@peter-mw in my comment on that issue https://github.com/meinaart/cypress-plugin-snapshots/issues/10#issuecomment-514459554 I show a workaround