Skip to content

Commit

Permalink
test(fixtures): add tests for FixturesLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Jul 2, 2017
1 parent f84ca4b commit 56b5d32
Show file tree
Hide file tree
Showing 5 changed files with 316 additions and 22 deletions.
44 changes: 23 additions & 21 deletions src/extensions/fixtures/fixtures_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const yaml = require('js-yaml')
const _ = require('lodash')

let fixturesDir = 'fixtures'
let featureUri = null
let featureUri

/**
* Configures the loader
Expand Down Expand Up @@ -42,7 +42,7 @@ exports.setFeatureUri = _featureUri => {
* @param {string} file - File path
* @return {Promise.<string>} File content
*/
const loadText = file =>
exports.loadText = file =>
new Promise((resolve, reject) => {
fs.readFile(file, (err, data) => {
if (err) return reject(err)
Expand All @@ -56,17 +56,17 @@ const loadText = file =>
* @param {string} file - File path
* @return {Promise.<Object|Array>} Parsed yaml data
*/
const loadYaml = file =>
loadText(file).then(content => {
exports.loadYaml = file =>
exports.loadText(file).then(content => {
try {
const data = yaml.safeLoad(content)
if (data === undefined) {
return Promise.reject(new Error(`yaml parsing resulted in undefined data (${file})`))
return Promise.reject(new Error(`Fixture file is invalid, yaml parsing resulted in undefined data for file: ${file}`))
}

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

Expand All @@ -76,8 +76,8 @@ const loadYaml = file =>
* @param {string} file - File path
* @return {Promise.<Object>} Json data
*/
const loadJson = file =>
loadText(file).then(content => {
exports.loadJson = file =>
exports.loadText(file).then(content => {
try {
const data = JSON.parse(content)

Expand All @@ -93,7 +93,7 @@ const loadJson = file =>
* @param {string} file - File path
* @return {Promise.<*>} Data generated from the module
*/
const loadModule = file => {
exports.loadModule = file => {
try {
const relativePath = path.relative(__dirname, file)
const mod = require(relativePath)
Expand Down Expand Up @@ -128,7 +128,7 @@ const loadModule = file => {
* @return {Promise.<Object|string>} Fixture content
*/
exports.load = fixture => {
if (featureUri === null) return Promise.reject()
if (featureUri === undefined) return Promise.reject(new Error(`Cannot load fixture: ${fixture}, no feature uri defined`))

const relativePath = path.relative(process.cwd(), path.dirname(featureUri))
const pattern = `${relativePath}/${fixturesDir}/${fixture}.@(yaml|yml|js|json|txt)`
Expand All @@ -137,14 +137,16 @@ exports.load = fixture => {
glob(pattern, (err, files) => {
const fixturesCount = files.length

if (fixturesCount === 0) return reject(`No fixtures found for: ${fixture} (${pattern})`)
if (fixturesCount === 0) return reject(new Error(`No fixture 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('')
new Error(
[
`Found ${fixturesCount} matching fixture files, `,
`you should have only one matching '${fixture}':\n `,
`- ${files.join('\n - ')}`
].join('')
)
)
}

Expand All @@ -154,16 +156,16 @@ exports.load = fixture => {
switch (ext) {
case 'yml':
case 'yaml':
return resolve(loadYaml(fixtureFile))
return resolve(exports.loadYaml(fixtureFile))

case 'js':
return resolve(loadModule(fixtureFile))
return resolve(exports.loadModule(fixtureFile))

case 'json':
return resolve(loadJson(fixtureFile))
return resolve(exports.loadJson(fixtureFile))

default:
return resolve(loadText(fixtureFile))
return resolve(exports.loadText(fixtureFile))
}
})
})
Expand All @@ -173,5 +175,5 @@ exports.load = fixture => {
* Resets fixtures loader.
*/
exports.reset = () => {
featureUri = null
featureUri = undefined
}
1 change: 0 additions & 1 deletion src/fixtures_loader.js

This file was deleted.

22 changes: 22 additions & 0 deletions tests/__mocks__/fs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const path = require('path')

const fs = jest.genMockFromModule('fs')

let mockFiles = {}

const __setMockFiles = _mockFiles => {
mockFiles = _mockFiles
}

const readFile = (file, cb) => {
const mockedContent = mockFiles[file]
if (mockedContent !== undefined) return cb(null, mockedContent)
cb(new Error(`File does not exist (${file})`))
}

fs.__setMockFiles = __setMockFiles
fs.readFile = readFile

module.exports = fs
16 changes: 16 additions & 0 deletions tests/__mocks__/glob.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict'

const glob = jest.genMockFromModule('glob')

let mockedResults = {}

module.exports = (pattern, cb) => {
if (pattern === '__defineMocks') {
mockedResults = cb
return
}

const mockedResult = mockedResults[pattern]
if (mockedResult === undefined) return cb(null, [])
cb(null, mockedResult)
}

0 comments on commit 56b5d32

Please sign in to comment.