Skip to content

Commit

Permalink
Merge pull request #22 from annaet/skip_ignoring_tags
Browse files Browse the repository at this point in the history
Fix .skip and .only functionality
  • Loading branch information
annaet authored Mar 28, 2021
2 parents ce36fb7 + a7b24f2 commit b181e09
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 285 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

All notable changes to this project will be documented in this file.

## [0.0.21] - 2021-03-28

### Added

- Added tests for `.only`
- Added tests for `.skip`

### Changed

- Fixed bug [#18](https://github.com/annaet/cypress-tags/issues/18) where tests including a `.skip` were not being evaluated for tags.
- Fixes bug [#19](https://github.com/annaet/cypress-tags/issues/19) where tests including a `.only` were not being evaluated for tags.
- Refactor common helper code out of test files and into `helpers/tagify`.
- Refactor common code for evaluating whether code block has tags into `checkBlockForTags` function.
- Updated Cypress to v6.8.0 and other dependancies to latest.


## [0.0.20] - 2021-02-24

### Added
Expand Down
17 changes: 17 additions & 0 deletions cypress/integration/only.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// sample start
describe('Describe block', () => {
it.only('Run this test without tag', () => { });
it.only('smoke', 'Run this test with tag', () => { });

it('Skip this test without tag', () => { });
it('smoke', 'Skip this test without tag', () => { });
});

describe.only('smoke', 'Only run this describe', () => {
it.only('Run this test without tag', () => { });
it.only('wip', 'Run this test with tag', () => { });

it('Skip this test without tag', () => { });
it('wip', 'Skip this test without tag', () => { });
});
// sample end
17 changes: 17 additions & 0 deletions cypress/integration/skip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// sample start
describe('Unskipped describe', () => {
it.skip('Skipped test', () => { });
it.skip('smoke', 'Skipped test', () => { });

it('Unskipped test', () => { });
it('smoke', 'Tagged test', () => { });
});

describe.skip('smoke', 'Skipped describe', () => {
it.skip('Skipped test', () => { });
it.skip('wip', 'Skipped test', () => { });

it('Unskipped test', () => { });
it('wip', 'Tagged test', () => { });
});
// sample end
72 changes: 36 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cypress-tags",
"version": "0.0.20",
"version": "0.0.21",
"description": "Use custom tags to slice up Cypress test runs.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -9,7 +9,7 @@
"watch": "tsc -w",
"cy:open": "cypress open",
"cy:run": "cypress run",
"mocha:run": "mocha -r ts-node/register --spec test/*.ts",
"mocha:run": "mocha -r ts-node/register --spec test/specs/*.ts --timeout 5000",
"test": "npm run cy:run && npm run mocha:run"
},
"author": "Anna Thomas",
Expand All @@ -26,20 +26,20 @@
},
"dependencies": {
"@cypress/browserify-preprocessor": "^3.0.1",
"cypress": "^6.5.0",
"cypress": "^6.8.0",
"through": "^2.3.8"
},
"devDependencies": {
"@types/browserify": "^12.0.36",
"@types/chai": "^4.2.15",
"@types/mocha": "^8.2.1",
"@types/node": "^14.14.31",
"@types/mocha": "^8.2.2",
"@types/node": "^14.14.37",
"@types/through": "0.0.30",
"chai": "^4.3.0",
"mocha": "^8.3.0",
"chai": "^4.3.4",
"mocha": "^8.3.2",
"ts-node": "^9.1.1",
"tsify": "^5.0.2",
"typescript": "^4.2.2"
"typescript": "^4.2.3"
},
"keywords": [
"cypress",
Expand Down
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ const transformer = (config: Cypress.PluginConfigOptions) => <T extends ts.Node>

const firstArgIsTag = firstArg && ts.isStringLiteral(firstArg) && isTitle(secondArg);

if (ts.isIdentifier(node.expression)) {
if (isDescribe(node) || isContext(node)) {
// Check block for tags and skip nodes as required
const checkBlockForTags = (nodeExpression: ts.CallExpression | ts.PropertyAccessExpression, node: ts.CallExpression) => {
if (isDescribe(nodeExpression) || isContext(nodeExpression)) {
// Describe / Context block
if (firstArgIsTag || ts.isArrayLiteralExpression(firstArg)) {
// First arg is single tag or tags list
Expand All @@ -125,7 +126,7 @@ const transformer = (config: Cypress.PluginConfigOptions) => <T extends ts.Node>
returnNode = result.node;
tags = result.tags;
}
} else if (isIt(node)) {
} else if (isIt(nodeExpression)) {
// It block
if (firstArgIsTag || ts.isArrayLiteralExpression(firstArg)) {
// First arg is single tag or tags list
Expand All @@ -137,19 +138,15 @@ const transformer = (config: Cypress.PluginConfigOptions) => <T extends ts.Node>
skipNode = calculateSkipChildren(includeTags, excludeTags, tags);
}
}
};

if (ts.isIdentifier(node.expression)) {
checkBlockForTags(node, node);
} else if (ts.isPropertyAccessExpression(node.expression)) {
// Extra check in case property access expression is from a forEach or similar
if (isSkip(node.expression) || isOnly(node.expression)) {
// Node contains a .skip or .only
if (isIt(node.expression)) {
// It block
if (firstArgIsTag || ts.isArrayLiteralExpression(firstArg)) {
// First arg is single tag or tags list
const result = removeTagsFromNode(node, tags, includeTags, excludeTags);
skipNode = result.skipNode;
returnNode = result.node;
}
}
checkBlockForTags(node.expression, node);
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions test/helpers/tagify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import browserify from 'browserify';
import through from 'through';

// @ts-ignore
const transform = require('../../dist').transform;

const generateConfig = () => {
return {
env: {
CYPRESS_INCLUDE_TAGS: undefined,
CYPRESS_EXCLUDE_TAGS: undefined,
},
} as unknown as Cypress.PluginConfigOptions;
};

const resetConfig = (config: Cypress.PluginConfigOptions) => {
delete config.env.CYPRESS_INCLUDE_TAGS;
delete config.env.CYPRESS_EXCLUDE_TAGS;
};

const tagify = (config: Cypress.PluginConfigOptions, fileName: string, output: string[] = []): Promise<string[]> => {
return new Promise((resolve, reject) => {
const options = {
typescript: require.resolve('typescript'),
extensions: ['.js', '.ts'],
plugin: [
['tsify']
],
};

try {
browserify(options)
.transform((fileName: string) => transform(fileName, config))
.add(`${__dirname}/../../cypress/integration/${fileName}.spec.ts`)
.bundle()
.pipe(through(ondata, onend));

let data = ''
function ondata(d: string) { data += d }
function onend() {
const lines = data.split('\n')
const startline = lines.indexOf('// sample start') + 1;
const endline = lines.length - 3;

output = lines.slice(startline, endline);
resolve(output);
}
} catch (err) {
reject(err);
}
});
};

export {
generateConfig,
resetConfig,
tagify,
};
Loading

0 comments on commit b181e09

Please sign in to comment.