Skip to content

Commit

Permalink
feat(cliWrapper): wrap the cucumber CLI to keep the use of custom opt…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
fthouraud committed Aug 19, 2021
1 parent f1a3c50 commit e0fa4f9
Show file tree
Hide file tree
Showing 16 changed files with 232 additions and 29 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
- run: yarn run check-fmt
- run: yarn run check-readme
- run: yarn run test-cover
- run: yarn run test-cli
- run: yarn run examples --tags @offline
- name: Coveralls Parallel
env:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jspm_packages
.DS_Store
.idea
*.iml
bin
bin/*
_doc
/examples/features/file_system/files/generated
/examples/features/file_system/files/deeply
Expand All @@ -50,3 +50,5 @@ _doc
!.yarn/sdks
!.yarn/versions
.pnp.*

!bin/veggies.js
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ It's also the perfect companion for testing CLI applications built with commande

- [Requirements](#requirements)
- [Installation](#installation)
- [CLI](#cli)
- [Features](#features)
- [API testing](#api-testing)
- [Making a simple request](#making-a-simple-request-and-testing-its-status-code)
Expand Down Expand Up @@ -88,6 +89,33 @@ httpApi.install({
cli.install()
```

## CLI

Veggies provides a simple CLI allowing the use of custom options to configure the feature it provides.

As `cucumber-js` **no longer supports custom options**, it's the safest way to use Veggies in full without problem.

To make use of it, you can use either of these commands:

```shell
$ yarn veggies

$ npx veggies

$ ./node_modules/.bin/veggies.js
```

The available options are:

| Option | Description |
|-----------------------------|--------------------------------------------------------------------------------------------|
| --cleanSnapshots | removes unused snapshots (not recommended while matching tags) |
| -u, --updateSnapshots | updates current snapshots if required |
| --preventSnapshotsCreation | a snapshot related step that would create one will fail instead (useful on CI environment) |
| --help | prints the veggies CLI help then the cucumber-js CLI help |

Please refer to the [CucumberJS CLI documentation](https://github.com/cucumber/cucumber-js/blob/master/docs/cli.md) to see native Cucumber options (or use `veggies --help`).

## Features

### API testing
Expand Down Expand Up @@ -180,7 +208,7 @@ Scenario: Using GitHub API
It's even possible to mix this approach with scenario outline to have more concise tests
(at the cost of clarity thought).

The following example will generates 3 scenario at runtime
The following example will generate 3 scenario at runtime
using different response values for second request.

```gherkin
Expand All @@ -204,7 +232,7 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
Be warned that cookies do not persist across scenarios in order to ensure they're autonomous.
If you really need to keep a cookie for multiple scenarios, you should consider using a custom step definition
and/or using the [state extention](#state-extension) to store it.
and/or using the [state extension](#state-extension) to store it.

```gherkin
Scenario: Enabling cookies
Expand Down Expand Up @@ -292,7 +320,7 @@ Scenario: Fetching some json response from the internets
| beginDate | equalRelativeDate | -1,week,fr,[Aujourd'hui] YYYY-MM-DD hh[h]mm |
```

By default this assertion does not check for full match.
By default, this assertion does not check for full match.
Properties not listed will just be ignored, if you want a full match:

```gherkin
Expand Down Expand Up @@ -410,9 +438,9 @@ The following directives are available:
| `<value>((boolean))` | `boolean` | `true((boolean))` | `true` |
| `<value>((array))` | `Array` | `one,two,three((array))` | `['one', 'two', 'three']` |

You can now use those directive for most of step definitions accepting data tables.
You can now use those directive for most of the step definitions accepting data tables.

For example you can use it to post typed json data:
For example, you can use it to post typed json data:

```gherkin
Scenario: Creating a resource using typed json payload
Expand Down Expand Up @@ -489,7 +517,7 @@ If the file does not exist, the test will fail.

### Snapshot testing

Snapshot testing test a response / content against a saved snapshot.
Snapshot testing compare a response / content against a saved snapshot.
Snapshots are stored in a file with same name as the feature file with the extension `.snap`
in a folder __snapshots__ in the same folder as the feature file.

Expand All @@ -506,16 +534,16 @@ features/

In a snapshot file, snapshot name follow the pattern:
SNAPSHOT_NAME NUMBER_OF_TIME_THIS_NAME_WAS_ENCOUNTERED_IN_CURRENT_FILE.NUMBER_OF_TIME_WE_HAD_A_SNAPSHOT_IN_THIS_SCENARIO.
For example, this would give: Scnenario 1 1.1
For example, this would give: Scenario 1 1.1

If a snapshot doesn't exists, it will be created the first time.
If a snapshot doesn't exist, it will be created the first time.

To update snapshot use the cucumber command line option '-u'. If you narrowed the tests with tags, only the snapshots
related to the tagged scenarios will be updated.

In case you need to remove unused snapshots, you can use the option `--cleanSnapshots`.
:warning: You shouldn't use this option with tags. It may result in used snapshots removed.
:information_source: Snapshot files related to feature files with no snapshots anymore won't get removed. You need to do it manually.
:information_source: Snapshot files related to feature files with no snapshots won't get removed. You need to do it manually.

Sometimes, it could be useful to prevent the creation of snapshots, for instance in a CI environment. To do this,
you can use the `--preventSnapshotsCreation` flag. An error will be thrown if the snapshot is missing and this option is present.
Expand Down Expand Up @@ -1064,7 +1092,7 @@ There is a special tag which only runs examples not requiring network access:
yarn run examples -- --tags @offline
```

Due to public API rate limit (eg. GitHub API), this tag is used when running on CI.
Due to public API rate limit (e.g. GitHub API), this tag is used when running on CI.

[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?longCache=true&style=for-the-badge
[npm-url]: https://www.npmjs.com/package/@ekino/veggies
Expand Down
5 changes: 5 additions & 0 deletions bin/veggies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env node

const { run } = require('../src/cli')

run(process.argv)
48 changes: 38 additions & 10 deletions doc/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ It's also the perfect companion for testing CLI applications built with commande

- [Requirements](#requirements)
- [Installation](#installation)
- [CLI](#cli)
- [Features](#features)
- [API testing](#api-testing)
- [Making a simple request](#making-a-simple-request-and-testing-its-status-code)
Expand Down Expand Up @@ -88,6 +89,33 @@ httpApi.install({
cli.install()
```

## CLI

Veggies provides a simple CLI allowing the use of custom options to configure the feature it provides.

As `cucumber-js` **no longer supports custom options**, it's the safest way to use Veggies in full without problem.

To make use of it, you can use either of these commands:

```shell
$ yarn veggies

$ npx veggies

$ ./node_modules/.bin/veggies.js
```

The available options are:

| Option | Description |
|-----------------------------|--------------------------------------------------------------------------------------------|
| --cleanSnapshots | removes unused snapshots (not recommended while matching tags) |
| -u, --updateSnapshots | updates current snapshots if required |
| --preventSnapshotsCreation | a snapshot related step that would create one will fail instead (useful on CI environment) |
| --help | prints the veggies CLI help then the cucumber-js CLI help |

Please refer to the [CucumberJS CLI documentation](https://github.com/cucumber/cucumber-js/blob/master/docs/cli.md) to see native Cucumber options (or use `veggies --help`).

## Features

### API testing
Expand Down Expand Up @@ -182,7 +210,7 @@ Scenario: Using GitHub API
It's even possible to mix this approach with scenario outline to have more concise tests
(at the cost of clarity thought).

The following example will generates 3 scenario at runtime
The following example will generate 3 scenario at runtime
using different response values for second request.

{{=<% %>=}}
Expand All @@ -208,7 +236,7 @@ Scenario Outline: Fetching <key> API endpoint from root endpoint
Cookies are disabled by default, but you've got the ability to enable/disable the feature using a gherkin `Given` expression.
Be warned that cookies do not persist across scenarios in order to ensure they're autonomous.
If you really need to keep a cookie for multiple scenarios, you should consider using a custom step definition
and/or using the [state extention](#state-extension) to store it.
and/or using the [state extension](#state-extension) to store it.

```gherkin
Scenario: Enabling cookies
Expand Down Expand Up @@ -296,7 +324,7 @@ Scenario: Fetching some json response from the internets
| beginDate | equalRelativeDate | -1,week,fr,[Aujourd'hui] YYYY-MM-DD hh[h]mm |
```

By default this assertion does not check for full match.
By default, this assertion does not check for full match.
Properties not listed will just be ignored, if you want a full match:

```gherkin
Expand Down Expand Up @@ -414,9 +442,9 @@ The following directives are available:
| `<value>((boolean))` | `boolean` | `true((boolean))` | `true` |
| `<value>((array))` | `Array` | `one,two,three((array))` | `['one', 'two', 'three']` |

You can now use those directive for most of step definitions accepting data tables.
You can now use those directive for most of the step definitions accepting data tables.

For example you can use it to post typed json data:
For example, you can use it to post typed json data:

```gherkin
Scenario: Creating a resource using typed json payload
Expand Down Expand Up @@ -493,7 +521,7 @@ If the file does not exist, the test will fail.

### Snapshot testing

Snapshot testing test a response / content against a saved snapshot.
Snapshot testing compare a response / content against a saved snapshot.
Snapshots are stored in a file with same name as the feature file with the extension `.snap`
in a folder __snapshots__ in the same folder as the feature file.

Expand All @@ -510,16 +538,16 @@ features/

In a snapshot file, snapshot name follow the pattern:
SNAPSHOT_NAME NUMBER_OF_TIME_THIS_NAME_WAS_ENCOUNTERED_IN_CURRENT_FILE.NUMBER_OF_TIME_WE_HAD_A_SNAPSHOT_IN_THIS_SCENARIO.
For example, this would give: Scnenario 1 1.1
For example, this would give: Scenario 1 1.1

If a snapshot doesn't exists, it will be created the first time.
If a snapshot doesn't exist, it will be created the first time.

To update snapshot use the cucumber command line option '-u'. If you narrowed the tests with tags, only the snapshots
related to the tagged scenarios will be updated.

In case you need to remove unused snapshots, you can use the option `--cleanSnapshots`.
:warning: You shouldn't use this option with tags. It may result in used snapshots removed.
:information_source: Snapshot files related to feature files with no snapshots anymore won't get removed. You need to do it manually.
:information_source: Snapshot files related to feature files with no snapshots won't get removed. You need to do it manually.

Sometimes, it could be useful to prevent the creation of snapshots, for instance in a CI environment. To do this,
you can use the `--preventSnapshotsCreation` flag. An error will be thrown if the snapshot is missing and this option is present.
Expand Down Expand Up @@ -1002,7 +1030,7 @@ There is a special tag which only runs examples not requiring network access:
yarn run examples -- --tags @offline
```

Due to public API rate limit (eg. GitHub API), this tag is used when running on CI.
Due to public API rate limit (e.g. GitHub API), this tag is used when running on CI.

[npm-image]: https://img.shields.io/npm/v/@ekino/veggies.svg?longCache=true&style=for-the-badge
[npm-url]: https://www.npmjs.com/package/@ekino/veggies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@


exports[`Setting json body from .yaml fixture file 1.1`] = `Object {
"first_name": "Raphaël",
"gender": "male",
"id": 1,
"last_name": "Benitte",
}`;

exports[`Snapshot testing on a cli with json output 1.1`] = `Object {
"gender": "equal(male)",
"name": "john",
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"url": "https://github.com/ekino/veggies.git"
},
"main": "src/index.js",
"bin": "./bin/veggies.js",
"author": "plouc <https://github.com/plouc>",
"maintainers": [
{
Expand All @@ -28,6 +29,7 @@
},
"engineStrict": true,
"dependencies": {
"arg": "5.0.1",
"chai": "4.3",
"fs-extra": "10.0",
"glob": "7.1",
Expand Down Expand Up @@ -66,6 +68,7 @@
"scripts": {
"test": "jest --verbose --colors tests",
"test-cover": "jest --verbose --colors --coverage",
"test-cli": "veggies --require tests/cli/support tests/cli/features",
"coverage": "cat ./coverage/lcov.info | coveralls",
"fmt": "prettier --print-width 100 --tab-width=4 --single-quote --bracket-spacing --no-semi --color --write \"{src,tests,scripts}/**/*.js\"",
"check-fmt": "prettier --print-width 100 --tab-width=4 --single-quote --bracket-spacing --no-semi --list-different \"{src,tests,scripts}/**/*.js\"",
Expand All @@ -75,7 +78,7 @@
"check-readme": "node scripts/generate_readme --check",
"doc": "jsdoc -c .jsdoc.json --verbose",
"doc-pub": "yarn run readme && yarn run doc && gh-pages -d _doc",
"examples": "cucumber-js --require examples/support examples/features",
"examples": "veggies --require examples/support examples/features",
"changelog": "conventional-changelog -p conventionalcommits -i CHANGELOG.md -s -r 0"
}
}
45 changes: 45 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict'

const arg = require('arg')
const CucumberCli = require('@cucumber/cucumber/lib/cli').default

const veggiesArgsDefinitions = {
'--cleanSnapshots': Boolean,
'--updateSnapshots': Boolean,
'-u': '--updateSnapshots',
'--preventSnapshotsCreation': Boolean,
}

const printHelp = () => {
console.log('veggies help')
console.log(`
Options:
--cleanSnapshots removes unused snapshots (not recommended while matching tags)
-u, --updateSnapshots updates current snapshots if required
--preventSnapshotsCreation a snapshot related step that would create one will fail instead (useful on CI environment)
For more details please visit https://github.com/ekino/veggies/blob/master/README.md
`)
console.log('cucumber-js help\n')
}

exports.run = async (argv) => {
const { _: cucumberArgs } = arg(veggiesArgsDefinitions, { argv, permissive: true })

try {
if (cucumberArgs.includes('--help')) {
printHelp()
}

const result = await new CucumberCli({
argv: cucumberArgs,
cwd: process.cwd(),
stdout: process.stdout,
}).run()

process.exit(result.success ? 0 : 1)
} catch (error) {
console.error(error)
process.exit(1)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


exports[`Should echo salut 1.1`] = `"salut
"`;

exports[`Should get deleted 1.1`] = `"salut
"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


exports[`Should echo salut1 1.1`] = `"salut
"`;

exports[`Should echo salut2 1.1`] = `"salut
"`;
7 changes: 7 additions & 0 deletions tests/cli/dummy_features/clean_snapshots.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@cleanSnapshots
Feature: Dummy test for --cleanSnapshots

Scenario: Should echo salut
When I run command echo salut
Then exit code should be 0
And stdout output should match snapshot
7 changes: 7 additions & 0 deletions tests/cli/dummy_features/prevent_snapshots_creation.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@preventSnapshotsCreation
Feature: Dummy test for --preventSnapshotsCreation

Scenario: Should echo salut
When I run command echo salut
Then exit code should be 0
And stdout output should match snapshot

0 comments on commit e0fa4f9

Please sign in to comment.