Files
cypress/cli/test/lib/cypress_spec.js
Tim Griesser 6a9f5a1a41 Merge branch 'develop' into unified-desktop-gui
* develop: (40 commits)
  fix(driver): Sticky elements within a fixed container will not prevent an element from being scrolled to (#18441)
  chore: make `create` function on server.ts obsolete (#18615)
  docs: Add instructions to squash commits to develop in Contributing (#18728)
  fix(@cypress/react): throw if using Next.js swc-loader without nodeVersion=system (#18686)
  refactor: remove Ramda (#18723)
  chore: Increase paralleled machines for desktop-gui tests (#18725)
  chore: Update Chrome (stable) to 95.0.4638.69 (#18696)
  chore: release @cypress/vue-v3.0.4
  chore: release @cypress/react-v5.10.2
  chore: release @cypress/schematic-v1.5.3
  fix: remove outdated registry link (#18710)
  chore: release @cypress/schematic-v1.5.2
  chore: release create-cypress-tests-v1.1.3
  chore: Update Chrome (beta) to 96.0.4664.27 (#18676)
  chore(tests): Remove flaky assertion that relies on png how compression (#18668)
  fix: make sure to go back to no-specs when delete spec file (#17760)
  fix: Next.JS 12 components testing failing with ` TypeError: Cannot read property 'traceChild' of undefined` (#18648)
  Backport .gitignore from unified-desktop-gui
  chore(docs): add 'Upgrading Electron' instructions (#18594)
  release 8.7.0 [skip ci]
  ...
2021-11-02 21:35:26 -04:00

277 lines
7.4 KiB
JavaScript

require('../spec_helper')
const os = require('os')
const path = require('path')
const _ = require('lodash')
const snapshot = require('../support/snapshot')
const Promise = require('bluebird')
const tmp = Promise.promisifyAll(require('tmp'))
const mockfs = require('mock-fs')
const fs = require(`${lib}/fs`)
const open = require(`${lib}/exec/open`)
const run = require(`${lib}/exec/run`)
const cypress = require(`${lib}/cypress`)
describe('cypress', function () {
beforeEach(function () {
mockfs({})
})
afterEach(() => {
mockfs.restore()
})
context('.open', function () {
beforeEach(function () {
sinon.stub(open, 'start').resolves()
})
const getStartArgs = () => {
expect(open.start).to.be.called
return _.get(open.start, ['lastCall', 'args', 0])
}
it('calls open#start, passing in options', function () {
return cypress.open({ foo: 'foo' })
.then(getStartArgs)
.then((args) => {
expect(args.foo).to.equal('foo')
})
})
it('normalizes config object', () => {
const config = {
pageLoadTime: 10000,
watchForFileChanges: false,
}
return cypress.open({ config })
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq({ config: JSON.stringify(config) })
})
})
it('passes configFile: false', () => {
const opts = {
configFile: false,
}
return cypress.open(opts)
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq(opts)
})
})
})
context('.run fails to write results file', function () {
it('resolves with error object', function () {
const outputPath = path.join(os.tmpdir(), 'cypress/monorepo/cypress_spec/output.json')
sinon.stub(tmp, 'fileAsync').resolves(outputPath)
sinon.stub(run, 'start').resolves(2)
sinon.stub(fs, 'readJsonAsync').withArgs(outputPath).resolves()
return cypress.run().then((result) => {
expect(result).to.deep.equal({
status: 'failed',
failures: 2,
message: 'Could not find Cypress test run results',
})
})
})
})
context('.run', function () {
let outputPath
beforeEach(function () {
outputPath = path.join(os.tmpdir(), 'cypress/monorepo/cypress_spec/output.json')
sinon.stub(tmp, 'fileAsync').resolves(outputPath)
sinon.stub(run, 'start').resolves()
return fs.outputJsonAsync(outputPath, {
code: 0,
failingTests: [],
})
})
const normalizeCallArgs = (args) => {
expect(args.outputPath).to.equal(outputPath)
delete args.outputPath
return args
}
const getStartArgs = () => {
expect(run.start).to.be.called
return normalizeCallArgs(_.get(run.start, ['lastCall', 'args', 0]))
}
it('calls run#start, passing in options', () => {
return cypress.run({ spec: 'foo' })
.then(getStartArgs)
.then((args) => {
expect(args.spec).to.equal('foo')
})
})
it('normalizes config object', () => {
const config = {
pageLoadTime: 10000,
watchForFileChanges: false,
}
return cypress.run({ config })
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq({ config: JSON.stringify(config) })
})
})
it('normalizes env option if passed an object', () => {
const env = { foo: 'bar', another: 'one' }
return cypress.run({ env })
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq({ env: JSON.stringify(env) })
})
})
it('gets random tmp file and passes it to run#start', function () {
return cypress.run().then(() => {
expect(run.start.lastCall.args[0].outputPath).to.equal(outputPath)
})
})
it('resolves with contents of tmp file', () => {
return cypress.run().then(snapshot)
})
it('passes configFile: false', () => {
const opts = {
configFile: false,
}
return cypress.run(opts)
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq(opts)
})
})
it('rejects if project is an empty string', () => {
return expect(cypress.run({ project: '' })).to.be.rejected
})
it('rejects if project is true', () => {
return expect(cypress.run({ project: true })).to.be.rejected
})
it('rejects if project is false', () => {
return expect(cypress.run({ project: false })).to.be.rejected
})
it('passes quiet: true', () => {
const opts = {
quiet: true,
}
return cypress.run(opts)
.then(getStartArgs)
.then((args) => {
expect(args).to.deep.eq(opts)
})
})
})
context('cli', function () {
describe('.parseRunArguments', function () {
it('parses CLI cypress run arguments', async () => {
const args = 'cypress run --browser chrome --spec my/test/spec.js'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
browser: 'chrome',
spec: 'my/test/spec.js',
})
})
it('parses CLI cypress run shorthand arguments', async () => {
const args = 'cypress run -b firefox -p 5005 --headed --quiet'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
browser: 'firefox',
port: 5005,
headed: true,
quiet: true,
})
})
it('coerces --record and --dev', async () => {
const args = 'cypress run --record false --dev true'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
record: false,
dev: true,
})
})
it('coerces --config-file false to boolean', async () => {
const args = 'cypress run --config-file false'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
configFile: false,
})
})
it('coerces --config-file cypress.config.js to string', async () => {
const args = 'cypress run --config-file cypress.config.js'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
configFile: 'cypress.config.js',
})
})
it('parses config file false', async () => {
const args = 'cypress run --config-file false'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
expect(options).to.deep.equal({
configFile: false,
})
})
it('parses config', async () => {
const args = 'cypress run --config baseUrl=localhost,video=true'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
// we don't need to convert the config into an object
// since the logic inside cypress.run handles that
expect(options).to.deep.equal({
config: 'baseUrl=localhost,video=true',
})
})
it('parses env', async () => {
const args = 'cypress run --env MY_NUMBER=42,MY_FLAG=true'.split(' ')
const options = await cypress.cli.parseRunArguments(args)
// we don't need to convert the environment into an object
// since the logic inside cypress.run handles that
expect(options).to.deep.equal({
env: 'MY_NUMBER=42,MY_FLAG=true',
})
})
})
})
})