-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fixtures): init fixtures support
- Loading branch information
Raphaël Benitte
authored and
Raphaël Benitte
committed
Jul 2, 2017
1 parent
3738972
commit db0c460
Showing
16 changed files
with
247 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@http_api @fixtures | ||
Feature: Using fixtures | ||
|
||
Scenario: Whatever 1 | ||
Given I set request json body from user |
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
'use strict' | ||
|
||
const { defineSupportCode } = require('cucumber') | ||
const { state, httpApi, cli } = require('../../src') | ||
const { state, fixtures, httpApi, cli } = require('../../src') | ||
|
||
defineSupportCode(({ setWorldConstructor }) => { | ||
setWorldConstructor(function() { | ||
state.extendWorld(this) | ||
fixtures.extendWorld(this) | ||
httpApi.extendWorld(this) | ||
cli.extendWorld(this) | ||
}) | ||
}) | ||
|
||
state.install(defineSupportCode) | ||
httpApi.install({})(defineSupportCode) | ||
fixtures.install(defineSupportCode) | ||
httpApi.install()(defineSupportCode) | ||
cli.install(defineSupportCode) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
'use strict' | ||
|
||
const Helper = require('../../helper') | ||
const Loader = require('./fixtures_loader') | ||
|
||
module.exports = (world, options) => { | ||
Loader.configure(options) | ||
world.fixtures = Loader | ||
Helper.registerExtension(world, 'fixtures') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module extensions/fixtures/FixturesLoader | ||
*/ | ||
|
||
const path = require('path') | ||
const glob = require('glob') | ||
const chalk = require('chalk') | ||
const _ = require('lodash') | ||
|
||
let fixturesDir = 'fixtures' | ||
let featureUri = null | ||
|
||
exports.configure = ({ fixturesDir: _fixturesDir = 'fixtures' } = {}) => { | ||
fixturesDir = _fixturesDir | ||
} | ||
|
||
exports.setFeatureUri = _featureUri => { | ||
featureUri = _featureUri | ||
} | ||
|
||
/** | ||
* Tries to load a fixture from current feature directory. | ||
* Will search for the following file extensions: | ||
* - yaml | ||
* - yml | ||
* - js | ||
* - json | ||
* - txt | ||
* | ||
* @param {string} fixture - Fixture file name without extension | ||
* @return {Promise.<Object|string>} Fixture content | ||
*/ | ||
exports.load = fixture => { | ||
if (featureUri === null) return Promise.reject() | ||
|
||
const relativePath = path.relative(process.cwd(), path.dirname(featureUri)) | ||
const pattern = `${relativePath}/${fixturesDir}/${fixture}.@(yaml|yml|js|json|txt)` | ||
|
||
return new Promise((resolve, reject) => { | ||
glob(pattern, (err, files) => { | ||
const fixturesCount = files.length | ||
|
||
if (fixturesCount === 0) return reject(`No fixtures found for: ${fixture} (${pattern})`) | ||
if (fixturesCount > 1) { | ||
return reject( | ||
[ | ||
`Found ${fixturesCount} matching fixture files, `, | ||
`you should have only one matching '${fixture}':\n `, | ||
`- ${files.join('\n - ')}` | ||
].join('') | ||
) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
/** | ||
* Resets fixtures loader. | ||
*/ | ||
exports.reset = () => { | ||
featureUri = null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
'use strict' | ||
|
||
/** | ||
* Registers hooks for the fixtures extension. | ||
* | ||
* @module extensions/fixtures/hooks | ||
*/ | ||
|
||
module.exports = ({ Before }) => { | ||
Before(function(scenarioResult) { | ||
this.fixtures.reset() | ||
this.fixtures.setFeatureUri(scenarioResult.scenario.feature.uri) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
|
||
/** | ||
* @module extensions/fixtures | ||
*/ | ||
|
||
const hooks = require('./hooks') | ||
|
||
/** | ||
* Extends cucumber world object. | ||
* Must be used inside customWorldConstructor. | ||
* | ||
* @example | ||
* // /support/world.js | ||
* | ||
* const { defineSupportCode } = require('cucumber') | ||
* const { fixtures } = require('@ekino/veggies') | ||
* | ||
* defineSupportCode(({ setWorldConstructor }) => { | ||
* setWorldConstructor(function() { | ||
* fixtures.extendWorld(this) | ||
* }) | ||
* }) | ||
* | ||
* @function | ||
* @param {Object} world - The cucumber world object | ||
*/ | ||
exports.extendWorld = require('./extend_world') | ||
|
||
/** | ||
* Installs the extension. | ||
* | ||
* @example | ||
* // /support/world.js | ||
* | ||
* const { defineSupportCode } = require('cucumber') | ||
* const { fixtures } = require('@ekino/veggies') | ||
* | ||
* defineSupportCode(({ setWorldConstructor }) => { | ||
* setWorldConstructor(function() { | ||
* fixtures.extendWorld(this) | ||
* }) | ||
* }) | ||
* | ||
* fixtures.install(defineSupportCode) | ||
* | ||
* @param {Function} define - The `defineSupportCode` helper from cucumber | ||
*/ | ||
exports.install = define => { | ||
define(hooks) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
'use strict' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
'use strict' | ||
|
||
exports.state = require('./extensions/state') | ||
exports.fixtures = require('./extensions/fixtures') | ||
exports.httpApi = require('./extensions/http_api') | ||
exports.cli = require('./extensions/cli') |