Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
feat: insert before hooks at depth 1
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 22, 2020
1 parent e998719 commit 02c7e42
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions bin/export-fiddle.js
Expand Up @@ -25,6 +25,7 @@ debug('found tests %o', tests)
const source = writeUtils.generateSpec(tests, {
before: args['--before'],
beforeEach: args['--before-each'],
beforeHooksAtDepth: 1, // only add the hooks to 1 level deep
})
fs.writeFileSync(outputFilename, source + '\n', 'utf8')
console.log('saved %s', outputFilename)
45 changes: 31 additions & 14 deletions src/write-utils.js
Expand Up @@ -23,23 +23,27 @@ function generateTest (name, maybeTest) {
* Processes a tree of test definitions, each with HTML and JS
* and returns generated spec file source
*/
function generateSpec (maybeTest, options = {}) {
function generateSpecWorker (maybeTest, options) {
let start = ''
if (options.before) {
start = source`
before(() => {
cy.visit('${options.before}')
})
` + '\n\n'
} else if (options.beforeEach) {
start = source`
beforeEach(() => {
cy.visit('${options.before}')
})
` + '\n\n'

if (options.beforeHooksAtDepth === options.depth) {
if (options.before ) {
start = source`
before(() => {
cy.visit('${options.before}')
})
` + '\n\n'
} else if (options.beforeEach) {
start = source`
beforeEach(() => {
cy.visit('${options.before}')
})
` + '\n\n'
}
}

if (isTestObject(maybeTest)) {
debug('single test object')
return start + generateTest(maybeTest.name, maybeTest)
}

Expand All @@ -66,8 +70,13 @@ function generateSpec (maybeTest, options = {}) {
return generateTest(name, value)
}

const nextCallOptions = {
...options,
depth: options.depth + 1
}

// final choice - create nested suite of tests
const inner = generateSpec(value)
const inner = generateSpecWorker(value, nextCallOptions)
return source`
describe('${name}', () => {
${inner}
Expand All @@ -78,4 +87,12 @@ function generateSpec (maybeTest, options = {}) {
return start + sources.join('\n')
}

function generateSpec(maybeTest, options = {}) {
const opts = {
...options,
depth: 0
}
return generateSpecWorker(maybeTest, opts)
}

module.exports = { generateSpec }

0 comments on commit 02c7e42

Please sign in to comment.