Skip to content

Commit

Permalink
fix: run an empty spec if there are no specs for this machine, closes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jan 18, 2023
1 parent 2f80cfd commit 3000173
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cypress/e2e/chunks.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ describe('getChunk', () => {
const specs = [1]
let chunk = getChunk(specs, 2, 0)
expect(chunk, 'chunk 0').to.deep.equal([1])
// nothing to do in the second machine
chunk = getChunk(specs, 2, 1)
expect(chunk, 'chunk 1').to.deep.equal(undefined)
expect(chunk, 'chunk 1').to.deep.equal([])
})
})

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"scripts": {
"test": "cypress run",
"badges": "npx -p dependency-version-badge update-badge cypress",
"semantic-release": "semantic-release"
"semantic-release": "semantic-release",
"empty": "DEBUG=cypress-split SPLIT=10 SPLIT_INDEX=9 cypress run"
},
"repository": {
"type": "git",
Expand Down
7 changes: 6 additions & 1 deletion src/chunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function chunkify(a, n, balanced) {
return out
}

/**
* Splits the given array across N "buckets"
* and returns the bucket with the index k.
* Note: a bucket can be empty!
*/
function getChunk(values, totalChunks, chunkIndex) {
// split all items into N chunks and take just a single chunk
if (totalChunks < 0) {
Expand All @@ -45,7 +50,7 @@ function getChunk(values, totalChunks, chunkIndex) {
}

const chunks = chunkify(values, totalChunks, true)
return chunks[chunkIndex]
return chunks[chunkIndex] || []
}

module.exports = { getChunk, chunkify }
4 changes: 4 additions & 0 deletions src/empty-spec.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// this file is empty on purpose
// if there are not specs to run on the current machine
// then we set the specPattern to this file
// so Cypress does not exit with an error "no specs found"
13 changes: 10 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { getSpecs } = require('find-cypress-specs')
const ghCore = require('@actions/core')
const cTable = require('console.table')
const { getChunk } = require('./chunk')
const path = require('path')

const label = 'cypress-split:'

Expand Down Expand Up @@ -119,9 +120,15 @@ function cypressSplit(on, config) {
.write()
}

debug('setting the spec pattern to')
debug(splitSpecs)
config.specPattern = splitSpecs
if (splitSpecs.length) {
debug('setting the spec pattern to')
debug(splitSpecs)
config.specPattern = splitSpecs
} else {
console.log('%s no specs to run, running an empty spec file', label)
config.specPattern = path.resolve(__dirname, './empty-spec.cy.js')
}

return config
}
}
Expand Down

0 comments on commit 3000173

Please sign in to comment.