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

feat: use require to support config via cypress.js #15263

Merged
merged 6 commits into from
Mar 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/server/lib/util/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ module.exports = {

const file = this.pathToConfigFile(projectRoot, options)

return fs.readJsonAsync(file)
const requireAsync = (file) => {
return Promise.try(() => require(file))
}

return requireAsync(file)
.catch({ code: 'MODULE_NOT_FOUND' }, () => {
return this._write(file, {})
})
.catch({ code: 'ENOENT' }, () => {
return this._write(file, {})
}).then((json = {}) => {
Expand Down
5 changes: 5 additions & 0 deletions packages/server/test/e2e/6_visit_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const https = require('https')
const useragent = require('express-useragent')
const { allowDestroy } = require('@packages/network')
const e2e = require('../support/helpers/e2e').default
const { clearCypressJsonCache } = require('../specUtils')

// create an HTTPS server that forces TLSv1
const startTlsV1Server = (port) => {
Expand Down Expand Up @@ -116,6 +117,10 @@ foo\
}

describe('e2e visit', () => {
beforeEach(() => {
clearCypressJsonCache()
})

context('low response timeout', () => {
e2e.setup({
settings: {
Expand Down
5 changes: 5 additions & 0 deletions packages/server/test/e2e/6_web_security_spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const e2e = require('../support/helpers/e2e').default
const { clearCypressJsonCache } = require('../specUtils')

const onServer = function (app) {
app.get('/link', (req, res) => {
Expand Down Expand Up @@ -51,6 +52,10 @@ const onServer = function (app) {
}

describe('e2e web security', () => {
beforeEach(() => {
clearCypressJsonCache()
})

e2e.setup({
servers: [{
port: 4466,
Expand Down
5 changes: 5 additions & 0 deletions packages/server/test/e2e/7_record_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { expectRunsToHaveCorrectTimings } = require('../support/helpers/resultsUt
const postRunResponseWithWarnings = jsonSchemas.getExample('postRunResponse')('2.2.0')
const postRunResponse = _.assign({}, postRunResponseWithWarnings, { warnings: [] })
const postRunInstanceResponse = jsonSchemas.getExample('postRunInstanceResponse')('2.1.0')
const { clearCypressJsonCache } = require('../specUtils')

const e2ePath = Fixtures.projectPath('e2e')
const outputPath = path.join(e2ePath, 'output.json')
Expand Down Expand Up @@ -744,6 +745,10 @@ describe('e2e record', () => {
})

describe('create run 500', () => {
beforeEach(() => {
clearCypressJsonCache()
})

const routes = [{
method: 'post',
url: '/runs',
Expand Down
33 changes: 22 additions & 11 deletions packages/server/test/integration/cypress_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const system = require(`${root}lib/util/system`)
const appData = require(`${root}lib/util/app_data`)
const electronApp = require('../../lib/util/electron-app')
const savedState = require(`${root}lib/saved_state`)
const { clearCypressJsonCache } = require('../specUtils')

const TYPICAL_BROWSERS = [
{
Expand Down Expand Up @@ -427,6 +428,7 @@ describe('lib/cypress', () => {
sinon.stub(runMode, 'listenForProjectEnd').resolves({ stats: { failures: 0 } })
sinon.stub(browsers, 'open')
sinon.stub(commitInfo, 'getRemoteOrigin').resolves('remoteOrigin')
clearCypressJsonCache()
})

it('runs project headlessly and exits with exit code 0', function () {
Expand Down Expand Up @@ -1224,6 +1226,8 @@ describe('lib/cypress', () => {

describe('--port', () => {
beforeEach(() => {
clearCypressJsonCache()

return runMode.listenForProjectEnd.resolves({ stats: { failures: 0 } })
})

Expand Down Expand Up @@ -1258,6 +1262,7 @@ describe('lib/cypress', () => {
describe('--env', () => {
beforeEach(() => {
process.env = _.omit(process.env, 'CYPRESS_DEBUG')
clearCypressJsonCache()

return runMode.listenForProjectEnd.resolves({ stats: { failures: 0 } })
})
Expand Down Expand Up @@ -1300,6 +1305,10 @@ describe('lib/cypress', () => {
})

describe('--config-file', () => {
beforeEach(() => {
clearCypressJsonCache()
})

it('false does not require cypress.json to run', function () {
return fs.statAsync(path.join(this.pristinePath, 'cypress.json'))
.then(() => {
Expand Down Expand Up @@ -1919,21 +1928,21 @@ describe('lib/cypress', () => {

describe('--config-file', () => {
beforeEach(function () {
this.filename = 'foo.bar.baz.asdf.quux.json'
clearCypressJsonCache()
this.open = sinon.stub(ServerE2E.prototype, 'open').resolves([])
})

it('reads config from a custom config file', function () {
sinon.stub(fs, 'readJsonAsync')
fs.readJsonAsync.withArgs(path.join(this.pristinePath, this.filename)).resolves({
env: { foo: 'bar' },
port: 2020,
})
const filename = 'foo.bar.baz.asdf.quux.json'

fs.readJsonAsync.callThrough()
fs.writeFileAsync(
path.join(this.pristinePath, filename),
JSON.stringify({ env: { foo: 'bar' }, port: 2020 }),
'utf8',
)

return cypress.start([
`--config-file=${this.filename}`,
`--config-file=${filename}`,
])
.then(() => {
const options = Events.start.firstCall.args[0]
Expand All @@ -1951,11 +1960,13 @@ describe('lib/cypress', () => {
})

it('creates custom config file if it does not exist', function () {
const filename = 'foo.quux.test.json'

return cypress.start([
`--config-file=${this.filename}`,
`--config-file=${filename}`,
])
.then(() => {
debug('cypress started with config %s', this.filename)
debug('cypress started with config %s', filename)
const options = Events.start.firstCall.args[0]

debug('first call arguments %o', Events.start.firstCall.args)
Expand All @@ -1964,7 +1975,7 @@ describe('lib/cypress', () => {
}).then(() => {
expect(this.open, 'open was called').to.be.called

return fs.readJsonAsync(path.join(this.pristinePath, this.filename))
return fs.readJsonAsync(path.join(this.pristinePath, filename))
.then((json) => {
expect(json, 'json file is empty').to.deep.equal({})
})
Expand Down
8 changes: 8 additions & 0 deletions packages/server/test/specUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ export const getFs = () => {

return recurse({ root: mockfs.getMockRoot() }, -1).root
}

export const clearCypressJsonCache = () => {
Object.keys(require.cache).forEach((key) => {
if (key.includes('cypress.json')) {
delete require.cache[key]
}
})
}
5 changes: 5 additions & 0 deletions packages/server/test/unit/settings_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ require('../spec_helper')
const path = require('path')
const { fs } = require(`${root}lib/util/fs`)
const settings = require(`${root}lib/util/settings`)
const { clearCypressJsonCache } = require('../specUtils')

const projectRoot = process.cwd()

describe('lib/settings', () => {
beforeEach(function () {
clearCypressJsonCache()
})

context('with no configFile option', () => {
beforeEach(function () {
this.setup = (obj = {}) => {
Expand Down