Skip to content

Instantly share code, notes, and snippets.

@elliottsj
Created January 17, 2020 17:14
Show Gist options
  • Save elliottsj/fff86bc2b64ff68871f09cc0cd517393 to your computer and use it in GitHub Desktop.
Save elliottsj/fff86bc2b64ff68871f09cc0cd517393 to your computer and use it in GitHub Desktop.
Cucumber Nx builder example
const {
createBuilder,
targetFromTargetString,
scheduleTargetAndForget,
} = require('@angular-devkit/architect');
const { readWorkspaceJson } = require('@yolkai/nx-workspace');
const execa = require('execa');
const path = require('path');
const { Observable, from, of } = require('rxjs');
const { catchError, map, mergeMap } = require('rxjs/operators');
/**
* Run the Cucumber tests.
*
* This builder works by first bundling support sources (such as `step_definitions/` and `support/` files)
* via the `bundleTarget` option. Then, cucumber-js is executed on a glob of feature files given
* by the `features` option.
*
* This was created following a tutorial:
* https://nxplaybook.com/courses/701247/lectures/12570377
*/
function run(options, context) {
const projectName = context.target.project;
const project = readWorkspaceJson().projects[projectName];
const bundleTarget = targetFromTargetString(options.bundleTarget);
const bundle$ = scheduleTargetAndForget(context, bundleTarget);
return bundle$.pipe(
mergeMap(result => {
const supportEntryPath = result.outfile;
const features = options.feature;
const subprocess = execa(path.join('node_modules', '.bin', 'cucumber-js'), [
options.features,
'--require',
supportEntryPath,
]);
subprocess.stdout.pipe(process.stdout);
subprocess.stderr.pipe(process.stderr);
return from(subprocess);
}),
map(result => {
return { success: true };
}),
catchError(error => {
context.logger.error('Cucumber tests failed. See errors above.');
return of({ success: false });
}),
);
}
module.exports = {
default: createBuilder(run),
};
{
"title": "Cucumber",
"description": "Execute Cucumber tests",
"type": "object",
"properties": {
"bundleTarget": {
"type": "string",
"description": "Target which bundles support files into a single entry point."
},
"features": {
"type": "string",
"description": "Glob of Cucumber feature files."
}
},
"required": ["bundleTarget", "features"]
}
{
"version": 1,
"projects": {
"myapp": {
"root": "apps/myapp",
"sourceRoot": "apps/myapp",
"projectType": "application",
"schematics": {},
"architect": {
"bundleCucumber": {
"builder": "@nrwl/node:build",
"options": {
"outputPath": "dist/apps/myapp-e2e/cucumber",
"main": "apps/myapp-e2e/cucumber/support.js",
"tsConfig": "apps/myapp-e2e/tsconfig.cucumber.json",
"assets": []
}
},
"cucumber": {
"builder": "./tools/builders:cucumber",
"options": {
"bundleTarget": "myapp-e2e:bundleCucumber",
"features": "apps/myapp-e2e/cucumber/features/**"
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment