Skip to content
This repository has been archived by the owner on Mar 5, 2022. It is now read-only.

write a wrapper to verify number of expected tests #357

Merged
merged 3 commits into from
Jul 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/react-scripts-folder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Handles component tests from cypress folder",
"private": true,
"scripts": {
"test": "../../node_modules/.bin/cypress run",
"test": "node ../../scripts/cypress-expect run --passing 5",
"cy:open": "../../node_modules/.bin/cypress open",
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js",
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js"
Expand Down
2 changes: 1 addition & 1 deletion examples/react-scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"start": "../../node_modules/.bin/react-scripts start",
"test": "../../node_modules/.bin/cypress run",
"test": "node ../../scripts/cypress-expect run --passing 10",
"cy:open": "../../node_modules/.bin/cypress open",
"check-coverage": "../../node_modules/.bin/check-coverage src/App.js src/calc.js src/Child.js cypress/fixtures/add.js",
"only-covered": "../../node_modules/.bin/only-covered src/App.js src/calc.js src/Child.js cypress/fixtures/add.js"
Expand Down
2 changes: 1 addition & 1 deletion examples/using-babel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Component testing for projects using Babel config",
"private": true,
"scripts": {
"test": "../../node_modules/.bin/cypress run",
"test": "node ../../scripts/cypress-expect run --passing 17",
"cy:open": "../../node_modules/.bin/cypress open"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"@types/node": "9.6.49",
"@types/react": "16.9.16",
"@types/react-dom": "16.9.4",
"arg": "4.1.3",
"autoprefixer": "9.7.6",
"axios": "0.18.1",
"babel-loader": "8.0.6",
Expand Down
61 changes: 61 additions & 0 deletions scripts/cypress-expect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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 args = arg({
'--passing': Number, // number of total passing tests to expect
})
if (typeof args['--passing'] !== 'number') {
console.error('expected a number of --passing tests', args['--passing'])
process.exit(1)
}
if (typeof args['--passing'] < 0) {
console.error('expected a number of --passing tests', args['--passing'])
process.exit(1)
}

const parseArguments = async () => {
// remove all our arguments to let Cypress only deal with its arguments
const cliArgs = args._
if (cliArgs[0] !== 'cypress') {
cliArgs.unshift('cypress')
}

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(runResults => {
// see https://on.cypress.io/module-api
if (runResults.failures) {
console.error(runResults.message)
process.exit(1)
}

if (runResults.totalFailed) {
console.error('%d test(s) failed', runResults.totalFailed)
process.exit(runResults.totalFailed)
}

// make sure the expected number of tests executed
if (runResults.totalPassed !== args['--passing']) {
console.error(
'expected %d passing tests, got %d',
args['--passing'],
runResults.totalPassed,
)
process.exit(1)
}
})
.catch(e => {
console.error(e)
process.exit(1)
})