Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor utils into own module #36

Merged
merged 1 commit into from May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Expand Up @@ -12,28 +12,28 @@ jobs:
with:
# run Cypress via this script
# and verify that exactly 3 tests are passing
command: node . run --passing 3
command: node ./bin run --passing 3

- name: Check --min-passing 🧪
uses: cypress-io/github-action@v2
with:
# we have already installed all dependencies above
install: false
command: node . run --min-passing 1
command: node ./bin run --min-passing 1

- name: Check other flags are parsed 🧪
uses: cypress-io/github-action@v2
with:
# we have already installed all dependencies above
install: false
command: node . run --passing 3 --spec '**/spec.js'
command: node ./bin run --passing 3 --spec '**/spec.js'

- name: Check --failing 🧪
uses: cypress-io/github-action@v2
with:
# we have already installed all dependencies above
install: false
command: node . run --passing 1 --failing 1 --config integrationFolder=cypress/failing
command: node ./bin run --passing 1 --failing 1 --config integrationFolder=cypress/failing

- name: --expect example 1 💎
run: npm run test:expect:1
Expand Down
152 changes: 152 additions & 0 deletions bin/index.js
@@ -0,0 +1,152 @@
#!/usr/bin/env node

// @ts-check

// runs Cypress with "normal" CLI parameters
// but after the run verifies the expected number of passing tests
const cypress = require('cypress')
const debug = require('debug')('cypress-expect')
const arg = require('arg')
const fs = require('fs')
const { expectTestResults } = require('../src/utils')

const CLI_HELP_URL = 'https://github.com/bahmutov/cypress-expect#options'
const cliHelpMessage = `see ${CLI_HELP_URL}`

const isValidPassing = (x) => typeof x === 'number' && x > 0

// remove all our arguments to let Cypress only deal with its arguments
const args = arg(
{
'--passing': Number, // number of total passing tests to expect
'--min-passing': Number, // at least this number of passing tests
'--failing': Number, // number of failing tests to expect
'--pending': Number, // number of pending tests to expect
'--expect': String, // filename of JSON file with test names and statuses
},
{
// allow other flags to be present - to be sent to Cypress CLI
permissive: true,
},
)
debug('args %o', args)

const isPassingSpecified = '--passing' in args
const isMinPassingSpecified = '--min-passing' in args
const isFailingSpecified = '--failing' in args
const isPendingSpecified = '--pending' in args
const isExpectSpecified = '--expect' in args
const noOptionsSpecified =
!isPassingSpecified &&
!isMinPassingSpecified &&
!isPendingSpecified &&
!isFailingSpecified &&
!isExpectSpecified

debug('specified options %o', {
isPassingSpecified,
isMinPassingSpecified,
isFailingSpecified,
isPendingSpecified,
isExpectSpecified,
noOptionsSpecified,
})

if (noOptionsSpecified) {
console.error('Need to specify at least one parameter:')
console.error(
'--passing or --min-passing or --failing or --pending or --expect',
)
console.error(cliHelpMessage)
process.exit(1)
}

const isNumberOptionSpecified =
isPassingSpecified ||
isMinPassingSpecified ||
isFailingSpecified ||
isPendingSpecified
if (isExpectSpecified && isNumberOptionSpecified) {
console.error('You used --expect with some other option')
console.error('--expect <filename> can be the only option by itself')
console.error(cliHelpMessage)
process.exit(1)
}

if (isPassingSpecified) {
if (!isValidPassing(args['--passing'])) {
console.error('expected a number of --passing tests', args['--passing'])
console.error(cliHelpMessage)
process.exit(1)
}
}

if (isMinPassingSpecified) {
if (!isValidPassing(args['--min-passing'])) {
console.error(
'expected a number of --min-passing tests',
args['--min-passing'],
)
console.error(cliHelpMessage)
process.exit(1)
}
}

if (isFailingSpecified) {
if (!isValidPassing(args['--failing'])) {
console.error('expected a number of --failing tests', args['--failing'])
console.error(cliHelpMessage)
process.exit(1)
}
}

if (isPassingSpecified && isMinPassingSpecified) {
console.error('Cannot specify both --passing and --min-passing options')
console.error(cliHelpMessage)
process.exit(1)
}

if (isExpectSpecified) {
const filename = args['--expect']
if (!fs.existsSync(filename)) {
console.error('Cannot find file specified using --expect option')
console.error('filename: "%s"', filename)
console.error(cliHelpMessage)
process.exit(1)
}
}

debug('params %o', {
passing: args['--passing'],
minPassing: args['--min-passing'],
failing: args['--failing'],
pending: args['--pending'],
expect: args['--expect'],
})

const parseArguments = async () => {
const cliArgs = args._
if (cliArgs[0] !== 'cypress') {
cliArgs.unshift('cypress')
}

if (cliArgs[1] !== 'run') {
cliArgs.splice(1, 0, 'run')
}

debug('parsing Cypress CLI %o', cliArgs)
return await cypress.cli.parseRunArguments(cliArgs)
}

parseArguments()
.then((options) => {
debug('parsed CLI options %o', options)

return cypress.run(options)
})
.then(expectTestResults(args))
.catch((e) => {
console.log('error: %s', e.message)
console.error(e)
process.exit(1)
})