cucumber-enhancement
Advanced tools
Comparing version 1.0.0 to 3.0.0
@@ -1,40 +0,38 @@ | ||
var expect = require('chai').expect; | ||
'use strict'; | ||
module.exports = function () { | ||
const { defineSupportCode } = require('cucumber'); | ||
const expect = require('chai').expect; | ||
this.When(/^I list the branches$/, { | ||
api: this.joi.string(), | ||
token: this.joi.string(), | ||
owner: this.joi.string(), | ||
repo: this.joi.string() | ||
}, function (data, callback) { | ||
this.request({ | ||
defineSupportCode(({ When, Then }) => { | ||
When(/^I list the branches$/, function (callback) { | ||
const world = this; | ||
const data = world.getAttributes({ | ||
request: world.joi.func(), | ||
api: world.joi.string(), | ||
owner: world.joi.string(), | ||
repo: world.joi.string() | ||
}); | ||
data.request({ | ||
method: 'GET', | ||
uri: [ | ||
data.api, 'repos', data.owner, data.repo, 'branches' | ||
].join('/'), | ||
qs: { | ||
access_token: data.token | ||
} | ||
}, function (err, message, response) { | ||
uri: `${data.api}/repos/${data.owner}/${data.repo}/branches` | ||
}, (err, message, response) => { | ||
if (err) { | ||
throw err; | ||
return callback(err); | ||
} | ||
callback(null, { | ||
branches: response.map(function (obj) { | ||
return obj.name; | ||
}) | ||
}); | ||
world.setAttribute('branches', response.map(obj => obj.name)); | ||
return callback(); | ||
}); | ||
}); | ||
this.Then(/^I should see "([^"]*)" as one of the branches$/, { | ||
branches: this.joi.array() | ||
}, function (data, branch, callback) { | ||
Then(/^I should see "([^"]*)" as one of the branches$/, function (branch) { | ||
const world = this; | ||
const data = world.getAttributes({ | ||
branches: world.joi.array() | ||
}); | ||
expect(data.branches).to.contain(branch); | ||
setImmediate(callback); | ||
}); | ||
}; | ||
}); |
@@ -1,38 +0,38 @@ | ||
var expect = require('chai').expect; | ||
'use strict'; | ||
module.exports = function () { | ||
const { defineSupportCode } = require('cucumber'); | ||
const expect = require('chai').expect; | ||
this.When(/^I list the languages$/, { | ||
api: this.joi.string(), | ||
token: this.joi.string(), | ||
owner: this.joi.string(), | ||
repo: this.joi.string() | ||
}, function (data, callback) { | ||
this.request({ | ||
defineSupportCode(({ When, Then }) => { | ||
When(/^I list the languages$/, function (callback) { | ||
const world = this; | ||
const data = world.getAttributes({ | ||
request: world.joi.func(), | ||
api: world.joi.string(), | ||
owner: world.joi.string(), | ||
repo: world.joi.string() | ||
}); | ||
data.request({ | ||
method: 'GET', | ||
uri: [ | ||
data.api, 'repos', data.owner, data.repo, 'languages' | ||
].join('/'), | ||
qs: { | ||
access_token: data.token | ||
} | ||
}, function (err, message, response) { | ||
uri: `${data.api}/repos/${data.owner}/${data.repo}/languages` | ||
}, (err, message, response) => { | ||
if (err) { | ||
throw err; | ||
return callback(err); | ||
} | ||
callback(null, { | ||
'languages': Object.keys(response) | ||
}); | ||
world.setAttribute('languages', Object.keys(response)); | ||
return callback(); | ||
}); | ||
}); | ||
this.Then(/^I should see "([^"]*)" as one of the languages$/, { | ||
languages: this.joi.array() | ||
}, function (data, language, callback) { | ||
Then(/^I should see "([^"]*)" as one of the languages$/, function (language) { | ||
const world = this; | ||
const data = world.getAttributes({ | ||
languages: world.joi.array() | ||
}); | ||
expect(data.languages).to.contain(language); | ||
setImmediate(callback); | ||
}); | ||
}; | ||
}); |
@@ -1,12 +0,15 @@ | ||
module.exports = function () { | ||
'use strict'; | ||
this.Given(/^I am on the "([^/"]*)\/([^"]*)" repository on GitHub$/, function (owner, repo, callback) { | ||
callback(null, { | ||
owner: owner, | ||
repo: repo, | ||
api: 'https://api.github.com', | ||
token: process.env.ACCESS_TOKEN | ||
const { defineSupportCode } = require('cucumber'); | ||
defineSupportCode(({ Given }) => { | ||
Given(/^I am on the "([^/"]*)\/([^"]*)" repository on GitHub$/, function (owner, repo) { | ||
const world = this; | ||
world.setAttributes({ | ||
owner, | ||
repo, | ||
api: 'https://api.github.com' | ||
}); | ||
}); | ||
}; | ||
}); |
157
index.js
@@ -1,5 +0,5 @@ | ||
var hoek = require('hoek'), | ||
joi = require('joi'), | ||
request = require('request'); | ||
'use strict'; | ||
const joi = require('joi'); | ||
/** | ||
@@ -10,11 +10,11 @@ * Set a dot-deliminated value in an object | ||
* @param {Object} object Object to manipulate | ||
* @param {String} key Dot-deliminated key | ||
* @param {String} search Dot-deliminated key | ||
* @param {Varied} value Value to set to | ||
*/ | ||
function setValue(object, key, value) { | ||
var keys = key.split('.'), | ||
last = keys.length - 1, | ||
ref = object; | ||
function setValue(object, search, value) { | ||
const keys = search.split('.'); | ||
const last = keys.length - 1; | ||
let ref = object; | ||
keys.forEach(function (key, index) { | ||
keys.forEach((key, index) => { | ||
if (index === last) { | ||
@@ -25,3 +25,3 @@ ref[key] = value; | ||
} else if (typeof ref[key] !== 'object') { | ||
throw new Error('Unable to set a non-object ' + key + ': ' + typeof ref[key]); | ||
throw new Error(`Unable to set a non-object ${key}: ${typeof ref[key]}`); | ||
} | ||
@@ -32,110 +32,53 @@ ref = ref[key]; | ||
module.exports = function () { | ||
this.World = function (callback) { | ||
callback(Object.freeze({ | ||
/** | ||
* Internal values saved by the steps | ||
* @internal | ||
* @type {Object} | ||
*/ | ||
_values: {}, | ||
module.exports = function ({ attach, parameters }) { | ||
const world = this; | ||
/** | ||
* Default values in all requests | ||
* @internal | ||
* @type {Object} | ||
*/ | ||
_defaults: {}, | ||
// Load defaults | ||
world.attach = attach; | ||
world.parameters = parameters; | ||
/** | ||
* Set a default key to the request object | ||
* @method default | ||
* @param {String} key Dot-deliminated key | ||
* @param {Varied} value Value to set to | ||
*/ | ||
default: function (key, value) { | ||
setValue(this._defaults, key, value); | ||
}, | ||
/** | ||
* Internal attributes saved by the steps | ||
* @internal | ||
* @type {Object} | ||
*/ | ||
world._attributes = {}; | ||
/** | ||
* Store some value to be used in a later step | ||
* @method store | ||
* @param {String} key Dot-deliminated key | ||
* @param {Varied} value Value to store to | ||
*/ | ||
store: function (key, value) { | ||
if (typeof key === 'object') { | ||
Object.keys(key).forEach(function (k) { | ||
setValue(this._values, k, key[k]); | ||
}.bind(this)); | ||
} else { | ||
setValue(this._values, key, value); | ||
} | ||
}, | ||
/** | ||
* Validate and retrieve the values this step needs | ||
* @method retrieve | ||
* @param {Object} schema Joi validation schema | ||
* @return {Object} Values you were validating | ||
*/ | ||
retrieve: function (schema) { | ||
var result = joi.validate(this._values, schema, { | ||
allowUnknown: true, | ||
presence: 'required' | ||
}); | ||
if (result.error) { | ||
throw new Error(result.error.annotate()); | ||
} | ||
return result.value; | ||
}, | ||
/** | ||
* Make an API call | ||
* @method request | ||
* @param {Object} options Request options | ||
* @param {Function} callback Function to run when done (err, request, response) | ||
*/ | ||
request: function (options, callback) { | ||
request(hoek.applyToDefaults(this._defaults, options), callback.bind(this)); | ||
} | ||
})); | ||
/** | ||
* Store some value to be used in a later step | ||
* @method setAttributes | ||
* @param {String} key Dot-deliminated key | ||
* @param {Varied} value Value to store to | ||
*/ | ||
world.setAttributes = (key, value) => { | ||
if (typeof key === 'object') { | ||
Object.keys(key).forEach((k) => { | ||
setValue(world._attributes, k, key[k]); | ||
}); | ||
} else { | ||
setValue(world._attributes, key, value); | ||
} | ||
}; | ||
world.setAttribute = world.setAttributes; | ||
/** | ||
* Define a step with requirements | ||
* @defineStepWithRequirements | ||
* @param {String} regex Regex to match | ||
* @param {Object} [requirements] Joi object requirements | ||
* @param {Function} callback Function to run when done ([requirements], [args..], callback) | ||
* Validate and retrieve the attributes this step needs | ||
* @method getAttributes | ||
* @param {Object} schema Joi validation schema | ||
* @return {Object} Values you were validating | ||
*/ | ||
this.defineStepWithRequirements = function (regex, requirements, callback) { | ||
// requirements is not required | ||
if (!callback) { | ||
callback = requirements; | ||
requirements = null; | ||
world.getAttributes = (schema) => { | ||
const result = joi.validate(world._attributes, schema, { | ||
allowUnknown: true, | ||
presence: 'required' | ||
}); | ||
if (result.error) { | ||
throw new Error(result.error.details.map(detail => detail.message).join('\n')); | ||
} | ||
this.defineStep(regex, function () { | ||
var args = Array.prototype.slice.call(arguments), | ||
next = args.pop(); | ||
if (requirements) { | ||
args = [this.retrieve(requirements)].concat(args); | ||
} | ||
args.push(function (err, data) { | ||
this.store(data || {}); | ||
next(err); | ||
}.bind(this)); | ||
callback.apply(this, args); | ||
}); | ||
return result.value; | ||
}; | ||
this.When = this.defineStepWithRequirements; | ||
this.Given = this.defineStepWithRequirements; | ||
this.Then = this.defineStepWithRequirements; | ||
this.joi = joi; | ||
world.joi = joi; | ||
}; |
{ | ||
"name": "cucumber-enhancement", | ||
"version": "1.0.0", | ||
"version": "3.0.0", | ||
"description": "Enhances cucumber.js to make it easier to interact between steps", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "jenkins-mocha tests/**", | ||
"functional": "cucumber.js --require lib/* --require features/" | ||
"pretest": "eslint .", | ||
"test": "jenkins-mocha", | ||
"posttest": "cucumber.js" | ||
}, | ||
@@ -21,2 +22,5 @@ "repository": { | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">= 6" | ||
}, | ||
"bugs": { | ||
@@ -27,13 +31,13 @@ "url": "https://github.com/stjohnjohnson/cucumber-enhancement/issues" | ||
"dependencies": { | ||
"hoek": "^2.11.0", | ||
"joi": "^5.1.0", | ||
"request": "^2.51.0" | ||
"hoek": "^5.0.2", | ||
"joi": "^13.0.2" | ||
}, | ||
"devDependencies": { | ||
"cucumber": "^0.4.7", | ||
"chai": "^1.10.0", | ||
"jenkins-mocha": "^1.0.9", | ||
"mockery": "^1.4.0", | ||
"sinon": "^1.12.2" | ||
"chai": "^4.1.2", | ||
"cucumber": "^3.1.0", | ||
"eslint": "^4.11.0", | ||
"eslint-config-screwdriver": "^3.0.0", | ||
"jenkins-mocha": "^6.0.0", | ||
"request": "^2.83.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# cucumberjs-enhancement | ||
# cucumber-enhancement | ||
@@ -7,18 +7,20 @@ Enhances cucumber.js to make it easier to interact between steps. | ||
* Create a file `features/support/enhancement-hook.js` as follows | ||
* Create a file `features/step_definitions/world-enhance.js` as follows | ||
``` | ||
module.exports = require('cucumber-enhancement'); | ||
const {setWorldConstructor} = require('cucumber'); | ||
setWorldConstructor(require('cucumber-enhancement')); | ||
``` | ||
This does the following: | ||
This modifies the World object to do the following: | ||
* Adds an additional argument to the step callback that saves the values for use in other steps: | ||
* Write attributes for later retrieval: | ||
```javascript | ||
this.Given(/^I am on the "([^/"]*)\/([^"]*)" repository on GitHub$/, function (owner, repo, callback) { | ||
Given(/^I am on the "([^/"]*)\/([^"]*)" repository on GitHub$/, function (owner, repo) { | ||
// This saves owner and repo for use in other steps | ||
callback(null, { | ||
owner: owner, | ||
repo: repo | ||
this.setAttributes({ | ||
owner, | ||
repo | ||
}); | ||
@@ -28,12 +30,12 @@ }); | ||
* Adds a new optional argument to `when`, `then`, and `given` to require data saved from other steps: | ||
* Read attributes from previous steps: | ||
```javascript | ||
this.Then(/^I should see "([^"]*)" as one of the branches$/, { | ||
branches: this.joi.array() | ||
}, function (data, branch, callback) { | ||
Then(/^I should see "([^"]*)" as one of the branches$/, function (branch) { | ||
const data = this.getAttributes({ | ||
branches: this.joi.array() | ||
}); | ||
// data.branches contains data saved from another step | ||
expect(data.branches).to.contain(branch); | ||
setImmediate(callback); | ||
}); | ||
@@ -43,25 +45,1 @@ ``` | ||
* Exposes [`joi`](https://www.npmjs.com/package/joi) interface for quick access during step definition (see above). | ||
* Bakes [`request`](https://www.npmjs.com/package/request) module into the world for easy access and automatically sets the scope of the callback function: | ||
```javascript | ||
this.request({ | ||
method: 'GET', | ||
uri: 'http://foo.yahoo.com/' | ||
}, function (err, message, response) { | ||
callback(null, { | ||
'bar': Object.keys(response) | ||
}); | ||
}); | ||
``` | ||
* Allows you to default values for the request module (mostly used by cucumber @tags): | ||
```javascript | ||
this.Before("@github_api", function(callback) { | ||
this.default('headers.User-Agent', 'Cucumber Test'); | ||
this.default('json', true); | ||
setImmediate(callback); | ||
}); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
15
11518
6
210
43
1
+ Addedhoek@5.0.46.1.3(transitive)
+ Addedisemail@3.2.0(transitive)
+ Addedjoi@13.7.0(transitive)
+ Addedtopo@3.0.3(transitive)
- Removedrequest@^2.51.0
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhoek@2.16.3(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisemail@1.2.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjoi@5.1.0(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedmoment@2.30.1(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpsl@1.14.0(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtopo@1.1.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
Updatedhoek@^5.0.2
Updatedjoi@^13.0.2