|
| 1 | +// @ts-check |
| 2 | +const ecstatic = require('ecstatic') |
| 3 | +const http = require('http') |
| 4 | +const debug = require('debug')('netlify-plugin-cypress') |
| 5 | +const debugVerbose = require('debug')('netlify-plugin-cypress:verbose') |
| 6 | + |
| 7 | +function serveFolder (folder, port) { |
| 8 | + const server = ecstatic({ |
| 9 | + root: folder |
| 10 | + }) |
| 11 | + return http.createServer(server).listen(port) |
| 12 | +} |
| 13 | + |
| 14 | +async function runCypressTests (baseUrl, record) { |
| 15 | + // we will use Cypress via its NPM module API |
| 16 | + // https://on.cypress.io/module-api |
| 17 | + const cypress = require('cypress') |
| 18 | + |
| 19 | + console.log('running Cypress against url %s recording?', baseUrl, record) |
| 20 | + return await cypress.run({ |
| 21 | + config: { |
| 22 | + baseUrl |
| 23 | + }, |
| 24 | + record |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +module.exports = function cypressPlugin (pluginConfig) { |
| 29 | + debugVerbose('cypressPlugin config %o', pluginConfig) |
| 30 | + |
| 31 | + return { |
| 32 | + name: 'cypress netlify plugin', |
| 33 | + postBuild: async (arg) => { |
| 34 | + debugVerbose('postBuild arg %o', arg) |
| 35 | + |
| 36 | + const fullPublishFolder = arg.netlifyConfig.build.publish |
| 37 | + debug('folder to publish is "%s"', fullPublishFolder) |
| 38 | + |
| 39 | + const port = 8080 |
| 40 | + const server = serveFolder(fullPublishFolder, port) |
| 41 | + debug('local server listening on port %d', port) |
| 42 | + |
| 43 | + // only if the user wants to record the tests and has set the record key |
| 44 | + // then we should attempt recording |
| 45 | + const record = |
| 46 | + typeof process.env.CYPRESS_RECORD_KEY === 'string' && |
| 47 | + Boolean(pluginConfig.record) |
| 48 | + const baseUrl = `http://localhost:${port}` |
| 49 | + const results = await runCypressTests(baseUrl, record) |
| 50 | + |
| 51 | + await new Promise((resolve, reject) => { |
| 52 | + server.close(err => { |
| 53 | + if (err) { |
| 54 | + return reject(err) |
| 55 | + } |
| 56 | + debug('closed local server on port %d', port) |
| 57 | + resolve() |
| 58 | + }) |
| 59 | + }) |
| 60 | + |
| 61 | + const exitWithError = (message, info) => { |
| 62 | + console.error('Exit with error: %s', message) |
| 63 | + throw info.error |
| 64 | + } |
| 65 | + const failBuild = arg.utils && arg.utils.build && arg.utils.build.failBuild || exitWithError |
| 66 | + |
| 67 | + // seems Cypress TS definition does not have "failures" and "message" properties |
| 68 | + if (results.failures) { |
| 69 | + // Cypress failed without even running the tests |
| 70 | + console.error('Problem running Cypress') |
| 71 | + console.error(results.message) |
| 72 | + |
| 73 | + return failBuild('Problem running Cypress', { |
| 74 | + error: new Error(results.message) |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + debug('Cypress run results') |
| 79 | + Object.keys(results).forEach(key => { |
| 80 | + if (key.startsWith('total')) { |
| 81 | + debug('%s:', key, results[key]) |
| 82 | + } |
| 83 | + }) |
| 84 | + |
| 85 | + // results.totalFailed gives total number of failed tests |
| 86 | + if (results.totalFailed) { |
| 87 | + return failBuild('Failed Cypress tests', { |
| 88 | + error: new Error(`${results.totalFailed} test(s) failed`) |
| 89 | + }) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments