Skip to content

Commit

Permalink
feat(fixtures): add support for JSON fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte committed Jul 1, 2017
1 parent c6517b7 commit 169a75c
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Scenario: Running an invalid command

This module is composed of several extensions.

[state](#state-extension) | [http API](#http-api-extension) | [CLI](#cli-extension)
[state](#state-extension) | [fixtures](#fixtures-extension) | [http API](#http-api-extension) | [CLI](#cli-extension)

### state extension

Expand Down
2 changes: 1 addition & 1 deletion doc/README.tpl.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ Scenario: Running an invalid command

This module is composed of several extensions.

[state](#state-extension) | [http API](#http-api-extension) | [CLI](#cli-extension)
[state](#state-extension) | [fixtures](#fixtures-extension) | [http API](#http-api-extension) | [CLI](#cli-extension)

### state extension

Expand Down
3 changes: 3 additions & 0 deletions examples/features/http_api/fixtures.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ Feature: Using fixtures

Scenario: Loading .js fixture file
Given I set request json body from module_00

Scenario: Loading .json fixture file
Given I set request json body from json_00
5 changes: 5 additions & 0 deletions examples/features/http_api/fixtures/json_00.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"data": "fixture from JSON",
"testing": true,
"awesomeness": 100
}
49 changes: 48 additions & 1 deletion src/extensions/fixtures/fixtures_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,34 @@ const _ = require('lodash')
let fixturesDir = 'fixtures'
let featureUri = null

/**
* Configures the loader
*
* @param {string} _fixturesDir - The name of the fixtures directory relative to feature
*/
exports.configure = ({ fixturesDir: _fixturesDir = 'fixtures' } = {}) => {
fixturesDir = _fixturesDir
}

/**
* Sets feature uri, used to resolve fixtures files.
* When trying to load a fixture file the path will be comprised of:
* - feature uri
* - fixturesDir
* - fixture name
*
* @param {string} _featureUri - Feature uri
*/
exports.setFeatureUri = _featureUri => {
featureUri = _featureUri
}

/**
* Loads content from file.
*
* @param {string} file - File path
* @return {Promise.<string>} File content
*/
const loadText = file =>
new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
Expand All @@ -30,6 +50,12 @@ const loadText = file =>
})
})

/**
* Loads content from yaml file.
*
* @param {string} file - File path
* @return {Promise.<Object|Array>} Parsed yaml data
*/
const loadYaml = file =>
loadText(file).then(content => {
try {
Expand All @@ -44,8 +70,29 @@ const loadYaml = file =>
}
})

const loadJson = file => Promise.resolve()
/**
* Loads content from json file.
*
* @param {string} file - File path
* @return {Promise.<Object>} Json data
*/
const loadJson = file =>
loadText(file).then(content => {
try {
const data = JSON.parse(content)

return data
} catch (err) {
return Promise.reject(new Error(`Unable to parse json fixture file: ${file}.\nerror: ${err.message}`))
}
})

/**
* Loads content from javascript module.
*
* @param {string} file - File path
* @return {Promise.<*>} Data generated from the module
*/
const loadModule = file => {
try {
const relativePath = path.relative(__dirname, file)
Expand Down

0 comments on commit 169a75c

Please sign in to comment.