How to use state.getBinaryPkgAsync method in Cypress

Best JavaScript code snippet using cypress

cli_spec.js

Source:cli_spec.js Github

copy

Full Screen

1require('../spec_helper')2const os = require('os')3const cli = require(`${lib}/cli`)4const util = require(`${lib}/util`)5const logger = require(`${lib}/logger`)6const info = require(`${lib}/exec/info`)7const run = require(`${lib}/exec/run`)8const open = require(`${lib}/exec/open`)9const cache = require(`${lib}/tasks/cache`)10const state = require(`${lib}/tasks/state`)11const verify = require(`${lib}/tasks/verify`)12const install = require(`${lib}/tasks/install`)13const spawn = require(`${lib}/exec/spawn`)14const snapshot = require('../support/snapshot')15const debug = require('debug')('test')16const execa = require('execa-wrap')17const mockedEnv = require('mocked-env')18const { expect } = require('chai')19describe('cli', () => {20 require('mocha-banner').register()21 beforeEach(() => {22 logger.reset()23 sinon.stub(process, 'exit')24 os.platform.returns('darwin')25 // sinon.stub(util, 'exit')26 sinon.stub(util, 'logErrorExit1')27 this.exec = (args) => {28 const cliArgs = `node test ${args}`.split(' ')29 debug('calling cli.init with: %o', cliArgs)30 return cli.init(cliArgs)31 }32 })33 context('unknown option', () => {34 // note it shows help for that specific command35 it('shows help', () => {36 return execa('bin/cypress', ['open', '--foo']).then((result) => {37 snapshot('shows help for open --foo 1', result)38 })39 })40 it('shows help for run command', () => {41 return execa('bin/cypress', ['run', '--foo']).then((result) => {42 snapshot('shows help for run --foo 1', result)43 })44 })45 it('shows help for cache command - unknown option --foo', () => {46 return execa('bin/cypress', ['cache', '--foo']).then(snapshot)47 })48 it('shows help for cache command - unknown sub-command foo', () => {49 return execa('bin/cypress', ['cache', 'foo']).then(snapshot)50 })51 it('shows help for cache command - no sub-command', () => {52 return execa('bin/cypress', ['cache']).then(snapshot)53 })54 })55 context('help command', () => {56 it('shows help', () => {57 return execa('bin/cypress', ['help']).then(snapshot)58 })59 it('shows help for -h', () => {60 return execa('bin/cypress', ['-h']).then(snapshot)61 })62 it('shows help for --help', () => {63 return execa('bin/cypress', ['--help']).then(snapshot)64 })65 })66 context('unknown command', () => {67 it('shows usage and exits', () => {68 return execa('bin/cypress', ['foo']).then(snapshot)69 })70 })71 context('CYPRESS_INTERNAL_ENV', () => {72 /**73 * Replaces line "Platform: ..." with "Platform: xxx"74 * @param {string} s75 */76 const replacePlatform = (s) => {77 return s.replace(/Platform: .+/, 'Platform: xxx')78 }79 /**80 * Replaces line "Cypress Version: ..." with "Cypress Version: 1.2.3"81 * @param {string} s82 */83 const replaceCypressVersion = (s) => {84 return s.replace(/Cypress Version: .+/, 'Cypress Version: 1.2.3')85 }86 const sanitizePlatform = (text) => {87 return text88 .split(os.eol)89 .map(replacePlatform)90 .map(replaceCypressVersion)91 .join(os.eol)92 }93 it('allows and warns when staging environment', () => {94 const options = {95 env: {96 CYPRESS_INTERNAL_ENV: 'staging',97 },98 filter: ['code', 'stderr', 'stdout'],99 }100 return execa('bin/cypress', ['help'], options).then(snapshot)101 })102 it('catches environment "foo"', () => {103 const options = {104 env: {105 CYPRESS_INTERNAL_ENV: 'foo',106 },107 // we are only interested in the exit code108 filter: ['code', 'stderr'],109 }110 return execa('bin/cypress', ['help'], options)111 .then(sanitizePlatform)112 .then(snapshot)113 })114 })115 context('cypress version', () => {116 let restoreEnv117 afterEach(() => {118 if (restoreEnv) {119 restoreEnv()120 restoreEnv = null121 }122 })123 const binaryDir = '/binary/dir'124 beforeEach(() => {125 sinon.stub(state, 'getBinaryDir').returns(binaryDir)126 })127 describe('individual package versions', () => {128 beforeEach(() => {129 sinon.stub(util, 'pkgVersion').returns('1.2.3')130 sinon131 .stub(state, 'getBinaryPkgAsync')132 .withArgs(binaryDir)133 .resolves({134 version: 'X.Y.Z',135 electronVersion: '10.9.8',136 electronNodeVersion: '7.7.7',137 })138 })139 it('reports just the package version', (done) => {140 this.exec('version --component package')141 process.exit.callsFake(() => {142 expect(logger.print()).to.equal('1.2.3')143 done()144 })145 })146 it('reports just the binary version', (done) => {147 this.exec('version --component binary')148 process.exit.callsFake(() => {149 expect(logger.print()).to.equal('X.Y.Z')150 done()151 })152 })153 it('reports just the electron version', (done) => {154 this.exec('version --component electron')155 process.exit.callsFake(() => {156 expect(logger.print()).to.equal('10.9.8')157 done()158 })159 })160 it('reports just the bundled Node version', (done) => {161 this.exec('version --component node')162 process.exit.callsFake(() => {163 expect(logger.print()).to.equal('7.7.7')164 done()165 })166 })167 it('handles not found bundled Node version', (done) => {168 state.getBinaryPkgAsync169 .withArgs(binaryDir)170 .resolves({171 version: 'X.Y.Z',172 electronVersion: '10.9.8',173 })174 this.exec('version --component node')175 process.exit.callsFake(() => {176 expect(logger.print()).to.equal('not found')177 done()178 })179 })180 })181 it('reports package version', (done) => {182 sinon.stub(util, 'pkgVersion').returns('1.2.3')183 sinon184 .stub(state, 'getBinaryPkgAsync')185 .withArgs(binaryDir)186 .resolves({187 version: 'X.Y.Z',188 })189 this.exec('version')190 process.exit.callsFake(() => {191 snapshot('cli version and binary version 1', logger.print())192 done()193 })194 })195 it('reports package and binary message', (done) => {196 sinon.stub(util, 'pkgVersion').returns('1.2.3')197 sinon.stub(state, 'getBinaryPkgAsync').resolves({ version: 'X.Y.Z' })198 this.exec('version')199 process.exit.callsFake(() => {200 snapshot('cli version and binary version 2', logger.print())201 done()202 })203 })204 it('reports electron and node message', (done) => {205 sinon.stub(util, 'pkgVersion').returns('1.2.3')206 sinon.stub(state, 'getBinaryPkgAsync').resolves({207 version: 'X.Y.Z',208 electronVersion: '10.10.88',209 electronNodeVersion: '11.10.3',210 })211 this.exec('version')212 process.exit.callsFake(() => {213 snapshot('cli version with electron and node 1', logger.print())214 done()215 })216 })217 it('reports package and binary message with npm log silent', (done) => {218 restoreEnv = mockedEnv({219 npm_config_loglevel: 'silent',220 })221 sinon.stub(util, 'pkgVersion').returns('1.2.3')222 sinon.stub(state, 'getBinaryPkgAsync').resolves({ version: 'X.Y.Z' })223 this.exec('version')224 process.exit.callsFake(() => {225 // should not be empty!226 snapshot('cli version and binary version with npm log silent', logger.print())227 done()228 })229 })230 it('reports package and binary message with npm log warn', (done) => {231 restoreEnv = mockedEnv({232 npm_config_loglevel: 'warn',233 })234 sinon.stub(util, 'pkgVersion').returns('1.2.3')235 sinon.stub(state, 'getBinaryPkgAsync').resolves({236 version: 'X.Y.Z',237 })238 this.exec('version')239 process.exit.callsFake(() => {240 // should not be empty!241 snapshot('cli version and binary version with npm log warn', logger.print())242 done()243 })244 })245 it('handles non-existent binary version', (done) => {246 sinon.stub(util, 'pkgVersion').returns('1.2.3')247 sinon.stub(state, 'getBinaryPkgAsync').resolves(null)248 this.exec('version')249 process.exit.callsFake(() => {250 snapshot('cli version no binary version 1', logger.print())251 done()252 })253 })254 it('handles non-existent binary --version', (done) => {255 sinon.stub(util, 'pkgVersion').returns('1.2.3')256 sinon.stub(state, 'getBinaryPkgAsync').resolves(null)257 this.exec('--version')258 process.exit.callsFake(() => {259 snapshot('cli --version no binary version 1', logger.print())260 done()261 })262 })263 it('handles non-existent binary -v', (done) => {264 sinon.stub(util, 'pkgVersion').returns('1.2.3')265 sinon.stub(state, 'getBinaryPkgAsync').resolves(null)266 this.exec('-v')267 process.exit.callsFake(() => {268 snapshot('cli -v no binary version 1', logger.print())269 done()270 })271 })272 })273 context('cypress run', () => {274 beforeEach(() => {275 sinon.stub(run, 'start').resolves(0)276 sinon.stub(util, 'exit').withArgs(0)277 })278 it('calls run.start with options + exits with code', (done) => {279 run.start.resolves(10)280 this.exec('run')281 util.exit.callsFake((code) => {282 expect(code).to.eq(10)283 done()284 })285 })286 it('run.start with options + catches errors', (done) => {287 const err = new Error('foo')288 run.start.rejects(err)289 this.exec('run')290 util.logErrorExit1.callsFake((e) => {291 expect(e).to.eq(err)292 done()293 })294 })295 it('calls run with port', () => {296 this.exec('run --port 7878')297 expect(run.start).to.be.calledWith({ port: '7878' })298 })299 it('calls run with port with -p arg', () => {300 this.exec('run -p 8989')301 expect(run.start).to.be.calledWith({ port: '8989' })302 })303 it('calls run with env variables', () => {304 this.exec('run --env foo=bar,host=http://localhost:8888')305 expect(run.start).to.be.calledWith({306 env: 'foo=bar,host=http://localhost:8888',307 })308 })309 it('calls run with config', () => {310 this.exec('run --config watchForFileChanges=false,baseUrl=localhost')311 expect(run.start).to.be.calledWith({312 config: 'watchForFileChanges=false,baseUrl=localhost',313 })314 })315 it('calls run with key', () => {316 this.exec('run --key asdf')317 expect(run.start).to.be.calledWith({ key: 'asdf' })318 })319 it('calls run with --record', () => {320 this.exec('run --record')321 expect(run.start).to.be.calledWith({ record: true })322 })323 it('calls run with --record false', () => {324 this.exec('run --record false')325 expect(run.start).to.be.calledWith({ record: false })326 })327 it('calls run with relative --project folder', () => {328 this.exec('run --project foo/bar')329 expect(run.start).to.be.calledWith({ project: 'foo/bar' })330 })331 it('calls run with absolute --project folder', () => {332 this.exec('run --project /tmp/foo/bar')333 expect(run.start).to.be.calledWith({ project: '/tmp/foo/bar' })334 })335 it('calls run with headed', () => {336 this.exec('run --headed')337 expect(run.start).to.be.calledWith({ headed: true })338 })339 it('calls run with --no-exit', () => {340 this.exec('run --no-exit')341 expect(run.start).to.be.calledWith({ exit: false })342 })343 it('calls run with --parallel', () => {344 this.exec('run --parallel')345 expect(run.start).to.be.calledWith({ parallel: true })346 })347 it('calls run with --ci-build-id', () => {348 this.exec('run --ci-build-id 123')349 expect(run.start).to.be.calledWith({ ciBuildId: '123' })350 })351 it('calls run with --group', () => {352 this.exec('run --group staging')353 expect(run.start).to.be.calledWith({ group: 'staging' })354 })355 it('calls run with spec', () => {356 this.exec('run --spec cypress/integration/foo_spec.js')357 expect(run.start).to.be.calledWith({358 spec: 'cypress/integration/foo_spec.js',359 })360 })361 it('calls run with space-separated --spec', () => {362 this.exec('run --spec a b c d e f g')363 expect(run.start).to.be.calledWith({ spec: 'a,b,c,d,e,f,g' })364 this.exec('run --dev bang --spec foo bar baz -P ./')365 expect(run.start).to.be.calledWithMatch({ spec: 'foo,bar,baz' })366 })367 it('warns with space-separated --spec', (done) => {368 sinon.spy(logger, 'warn')369 this.exec('run --spec a b c d e f g --dev')370 snapshot(logger.warn.getCall(0).args[0])371 done()372 })373 it('calls run with --tag', () => {374 this.exec('run --tag nightly')375 expect(run.start).to.be.calledWith({ tag: 'nightly' })376 })377 it('calls run comma-separated --tag', () => {378 this.exec('run --tag nightly,staging')379 expect(run.start).to.be.calledWith({ tag: 'nightly,staging' })380 })381 it('does not remove double quotes from --tag', () => {382 // I think it is a good idea to lock down this behavior383 // to make sure we either preserve it or change it in the future384 this.exec('run --tag "nightly"')385 expect(run.start).to.be.calledWith({ tag: '"nightly"' })386 })387 it('calls run comma-separated --spec', () => {388 this.exec('run --spec main_spec.js,view_spec.js')389 expect(run.start).to.be.calledWith({ spec: 'main_spec.js,view_spec.js' })390 })391 it('calls run with space-separated --tag', () => {392 this.exec('run --tag a b c d e f g')393 expect(run.start).to.be.calledWith({ tag: 'a,b,c,d,e,f,g' })394 this.exec('run --dev bang --tag foo bar baz -P ./')395 expect(run.start).to.be.calledWithMatch({ tag: 'foo,bar,baz' })396 })397 it('warns with space-separated --tag', (done) => {398 sinon.spy(logger, 'warn')399 this.exec('run --tag a b c d e f g --dev')400 snapshot(logger.warn.getCall(0).args[0])401 done()402 })403 it('calls run with space-separated --tag and --spec', () => {404 this.exec('run --tag a b c d e f g --spec h i j k l')405 expect(run.start).to.be.calledWith({ tag: 'a,b,c,d,e,f,g', spec: 'h,i,j,k,l' })406 this.exec('run --dev bang --tag foo bar baz -P ./ --spec fizz buzz --headed false')407 expect(run.start).to.be.calledWithMatch({ tag: 'foo,bar,baz', spec: 'fizz,buzz' })408 })409 it('removes stray double quotes from --ci-build-id and --group', () => {410 this.exec('run --ci-build-id "123" --group "staging"')411 expect(run.start).to.be.calledWith({ ciBuildId: '123', group: 'staging' })412 })413 })414 context('cypress open', () => {415 beforeEach(() => {416 sinon.stub(open, 'start').resolves(0)417 })418 it('calls open.start with relative --project folder', () => {419 this.exec('open --project foo/bar')420 expect(open.start).to.be.calledWith({ project: 'foo/bar' })421 })422 it('calls open.start with absolute --project folder', () => {423 this.exec('open --project /tmp/foo/bar')424 expect(open.start).to.be.calledWith({ project: '/tmp/foo/bar' })425 })426 it('calls open.start with options', () => {427 // sinon.stub(open, 'start').resolves()428 this.exec('open --port 7878')429 expect(open.start).to.be.calledWith({ port: '7878' })430 })431 it('calls open.start with global', () => {432 // sinon.stub(open, 'start').resolves()433 this.exec('open --port 7878 --global')434 expect(open.start).to.be.calledWith({ port: '7878', global: true })435 })436 it('calls open.start + catches errors', (done) => {437 const err = new Error('foo')438 open.start.rejects(err)439 this.exec('open --port 7878')440 util.logErrorExit1.callsFake((e) => {441 expect(e).to.eq(err)442 done()443 })444 })445 })446 context('cypress install', () => {447 it('calls install.start without forcing', () => {448 sinon.stub(install, 'start').resolves()449 this.exec('install')450 expect(install.start).not.to.be.calledWith({ force: true })451 })452 it('calls install.start with force: true when passed', () => {453 sinon.stub(install, 'start').resolves()454 this.exec('install --force')455 expect(install.start).to.be.calledWith({ force: true })456 })457 it('install calls install.start + catches errors', (done) => {458 const err = new Error('foo')459 sinon.stub(install, 'start').rejects(err)460 this.exec('install')461 util.logErrorExit1.callsFake((e) => {462 expect(e).to.eq(err)463 done()464 })465 })466 })467 context('cypress verify', () => {468 it('verify calls verify.start with force: true', () => {469 sinon.stub(verify, 'start').resolves()470 this.exec('verify')471 expect(verify.start).to.be.calledWith({472 force: true,473 welcomeMessage: false,474 })475 })476 it('verify calls verify.start + catches errors', (done) => {477 const err = new Error('foo')478 sinon.stub(verify, 'start').rejects(err)479 this.exec('verify')480 util.logErrorExit1.callsFake((e) => {481 expect(e).to.eq(err)482 done()483 })484 })485 })486 context('cypress info', () => {487 beforeEach(() => {488 sinon.stub(info, 'start').resolves(0)489 sinon.stub(util, 'exit').withArgs(0)490 })491 it('calls info start', () => {492 this.exec('info')493 expect(info.start).to.have.been.calledWith()494 })495 })496 context('cypress cache list', () => {497 it('prints explanation when no cache', (done) => {498 const err = new Error()499 err.code = 'ENOENT'500 sinon.stub(cache, 'list').rejects(err)501 this.exec('cache list')502 process.exit.callsFake(() => {503 snapshot('prints explanation when no cache', logger.print())504 done()505 })506 })507 it('catches rejection and exits', (done) => {508 const err = new Error('cache list failed badly')509 sinon.stub(cache, 'list').rejects(err)510 this.exec('cache list')511 util.logErrorExit1.callsFake((e) => {512 expect(e).to.eq(err)513 done()514 })515 })516 })517 context('component-testing', () => {518 beforeEach(() => {519 sinon.stub(spawn, 'start').resolves()520 })521 it('spawns server with correct args for component-testing', () => {522 this.exec('open-ct --dev')523 expect(spawn.start.firstCall.args[0]).to.include('--testing-type')524 expect(spawn.start.firstCall.args[0]).to.include('component')525 })526 it('runs server with correct args for component-testing', () => {527 this.exec('run-ct --dev')528 expect(spawn.start.firstCall.args[0]).to.include('--testing-type')529 expect(spawn.start.firstCall.args[0]).to.include('component')530 })531 it('does display open-ct command in the help', () => {532 return execa('bin/cypress', ['help']).then((result) => {533 expect(result).to.include('open-ct')534 })535 })536 it('does display run-ct command in the help', () => {537 return execa('bin/cypress', ['help']).then((result) => {538 expect(result).to.include('run-ct')539 })540 })541 })...

Full Screen

Full Screen

install_spec.js

Source:install_spec.js Github

copy

Full Screen

1require('../../spec_helper')2const _ = require('lodash')3const os = require('os')4const path = require('path')5const chalk = require('chalk')6const Promise = require('bluebird')7const mockfs = require('mock-fs')8const mockedEnv = require('mocked-env')9const snapshot = require('../../support/snapshot')10const stdout = require('../../support/stdout')11const fs = require(`${lib}/fs`)12const download = require(`${lib}/tasks/download`)13const install = require(`${lib}/tasks/install`)14const state = require(`${lib}/tasks/state`)15const unzip = require(`${lib}/tasks/unzip`)16const logger = require(`${lib}/logger`)17const util = require(`${lib}/util`)18const normalize = require('../../support/normalize')19const packageVersion = '1.2.3'20const downloadDestination = path.join(os.tmpdir(), `cypress-${process.pid}.zip`)21const installDir = '/cache/Cypress/1.2.3'22describe('/lib/tasks/install', function () {23 require('mocha-banner').register()24 beforeEach(function () {25 this.stdout = stdout.capture()26 // allow simpler log message comparison without27 // chalk's terminal control strings28 chalk.level = 029 })30 afterEach(() => {31 stdout.restore()32 chalk.level = 333 })34 context('.start', function () {35 beforeEach(function () {36 logger.reset()37 // sinon.stub(os, 'tmpdir').returns('/tmp')38 sinon.stub(util, 'isCi').returns(false)39 sinon.stub(util, 'isPostInstall').returns(false)40 sinon.stub(util, 'pkgVersion').returns(packageVersion)41 sinon.stub(download, 'start').resolves(packageVersion)42 sinon.stub(unzip, 'start').resolves()43 sinon.stub(Promise, 'delay').resolves()44 sinon.stub(fs, 'removeAsync').resolves()45 sinon.stub(state, 'getVersionDir').returns('/cache/Cypress/1.2.3')46 sinon.stub(state, 'getBinaryDir').returns('/cache/Cypress/1.2.3/Cypress.app')47 sinon.stub(state, 'getBinaryPkgAsync').resolves()48 sinon.stub(fs, 'ensureDirAsync').resolves(undefined)49 os.platform.returns('darwin')50 })51 describe('skips install', function () {52 it('when environment variable is set', function () {53 process.env.CYPRESS_INSTALL_BINARY = '0'54 return install.start()55 .then(() => {56 expect(download.start).not.to.be.called57 snapshot(58 'skip installation 1',59 normalize(this.stdout.toString()),60 )61 })62 })63 })64 describe('override version', function () {65 it('warns when specifying cypress version in env', function () {66 const version = '0.12.1'67 process.env.CYPRESS_INSTALL_BINARY = version68 return install.start()69 .then(() => {70 expect(download.start).to.be.calledWithMatch({71 version,72 })73 expect(unzip.start).to.be.calledWithMatch({74 zipFilePath: downloadDestination,75 })76 snapshot(77 'specify version in env vars 1',78 normalize(this.stdout.toString()),79 )80 })81 })82 it('trims environment variable before installing', function () {83 // note how the version has extra spaces around it on purpose84 const filename = '/tmp/local/file.zip'85 const version = ` ${filename} `86 process.env.CYPRESS_INSTALL_BINARY = version87 // internally, the variable should be trimmed and just filename checked88 sinon.stub(fs, 'pathExistsAsync').withArgs(filename).resolves(true)89 const installDir = state.getVersionDir()90 return install.start()91 .then(() => {92 expect(unzip.start).to.be.calledWithMatch({93 zipFilePath: filename,94 installDir,95 })96 })97 })98 it('removes double quotes around the environment variable before installing', function () {99 // note how the version has extra spaces around it on purpose100 // and there are double quotes101 const filename = '/tmp/local/file.zip'102 const version = ` "${filename}" `103 process.env.CYPRESS_INSTALL_BINARY = version104 // internally, the variable should be trimmed, double quotes removed105 // and just filename checked against the file system106 sinon.stub(fs, 'pathExistsAsync').withArgs(filename).resolves(true)107 const installDir = state.getVersionDir()108 return install.start()109 .then(() => {110 expect(unzip.start).to.be.calledWithMatch({111 zipFilePath: filename,112 installDir,113 })114 })115 })116 it('can install local binary zip file without download from absolute path', function () {117 const version = '/tmp/local/file.zip'118 process.env.CYPRESS_INSTALL_BINARY = version119 sinon.stub(fs, 'pathExistsAsync').withArgs(version).resolves(true)120 const installDir = state.getVersionDir()121 return install.start()122 .then(() => {123 expect(unzip.start).to.be.calledWithMatch({124 zipFilePath: version,125 installDir,126 })127 })128 })129 it('can install local binary zip file from relative path', function () {130 const version = './cypress-resources/file.zip'131 mockfs({132 [version]: 'asdf',133 })134 process.env.CYPRESS_INSTALL_BINARY = version135 const installDir = state.getVersionDir()136 return install.start()137 .then(() => {138 expect(download.start).not.to.be.called139 expect(unzip.start).to.be.calledWithMatch({140 zipFilePath: path.resolve(version),141 installDir,142 })143 })144 })145 describe('when version is already installed', function () {146 beforeEach(function () {147 state.getBinaryPkgAsync.resolves({ version: packageVersion })148 })149 it('doesn\'t attempt to download', function () {150 return install.start()151 .then(() => {152 expect(download.start).not.to.be.called153 expect(state.getBinaryPkgAsync).to.be.calledWith('/cache/Cypress/1.2.3/Cypress.app')154 })155 })156 it('logs \'skipping install\' when explicit cypress install', function () {157 return install.start()158 .then(() => {159 return snapshot(160 'version already installed - cypress install 1',161 normalize(this.stdout.toString()),162 )163 })164 })165 it('logs when already installed when run from postInstall', function () {166 util.isPostInstall.returns(true)167 return install.start()168 .then(() => {169 snapshot(170 'version already installed - postInstall 1',171 normalize(this.stdout.toString()),172 )173 })174 })175 })176 describe('when getting installed version fails', function () {177 beforeEach(function () {178 state.getBinaryPkgAsync.resolves(null)179 return install.start()180 })181 it('logs message and starts download', function () {182 expect(download.start).to.be.calledWithMatch({183 version: packageVersion,184 })185 expect(unzip.start).to.be.calledWithMatch({186 installDir,187 })188 snapshot(189 'continues installing on failure 1',190 normalize(this.stdout.toString()),191 )192 })193 })194 describe('when there is no install version', function () {195 beforeEach(function () {196 state.getBinaryPkgAsync.resolves(null)197 return install.start()198 })199 it('logs message and starts download', function () {200 expect(download.start).to.be.calledWithMatch({201 version: packageVersion,202 })203 expect(unzip.start).to.be.calledWithMatch({204 installDir,205 })206 // cleans up the zip file207 expect(fs.removeAsync).to.be.calledWith(208 downloadDestination,209 )210 snapshot(211 'installs without existing installation 1',212 normalize(this.stdout.toString()),213 )214 })215 })216 describe('when getting installed version does not match needed version', function () {217 beforeEach(function () {218 state.getBinaryPkgAsync.resolves({ version: 'x.x.x' })219 return install.start()220 })221 it('logs message and starts download', function () {222 expect(download.start).to.be.calledWithMatch({223 version: packageVersion,224 })225 expect(unzip.start).to.be.calledWithMatch({226 installDir,227 })228 snapshot(229 'installed version does not match needed version 1',230 normalize(this.stdout.toString()),231 )232 })233 })234 describe('with force: true', function () {235 beforeEach(function () {236 state.getBinaryPkgAsync.resolves({ version: packageVersion })237 return install.start({ force: true })238 })239 it('logs message and starts download', function () {240 expect(download.start).to.be.calledWithMatch({241 version: packageVersion,242 })243 expect(unzip.start).to.be.calledWithMatch({244 installDir,245 })246 snapshot(247 'forcing true always installs 1',248 normalize(this.stdout.toString()),249 )250 })251 })252 describe('as a global install', function () {253 beforeEach(function () {254 sinon.stub(util, 'isInstalledGlobally').returns(true)255 state.getBinaryPkgAsync.resolves({ version: 'x.x.x' })256 return install.start()257 })258 it('logs global warning and download', function () {259 expect(download.start).to.be.calledWithMatch({260 version: packageVersion,261 })262 expect(unzip.start).to.be.calledWithMatch({263 installDir,264 })265 snapshot(266 'warning installing as global 1',267 normalize(this.stdout.toString()),268 )269 })270 })271 describe('when running in CI', function () {272 beforeEach(function () {273 util.isCi.returns(true)274 state.getBinaryPkgAsync.resolves({ version: 'x.x.x' })275 return install.start()276 })277 it('uses verbose renderer', function () {278 snapshot(279 'installing in ci 1',280 normalize(this.stdout.toString()),281 )282 })283 })284 describe('failed write access to cache directory', function () {285 it('logs error on failure', function () {286 os.platform.returns('darwin')287 sinon.stub(state, 'getCacheDir').returns('/invalid/cache/dir')288 const err = new Error('EACCES: permission denied, mkdir \'/invalid\'')289 err.code = 'EACCES'290 fs.ensureDirAsync.rejects(err)291 return install.start()292 .then(() => {293 throw new Error('should have caught error')294 })295 .catch((err) => {296 logger.error(err)297 snapshot(298 'invalid cache directory 1',299 normalize(this.stdout.toString()),300 )301 })302 })303 })304 describe('CYPRESS_INSTALL_BINARY is URL or Zip', function () {305 it('uses cache when correct version installed given URL', function () {306 state.getBinaryPkgAsync.resolves({ version: '1.2.3' })307 util.pkgVersion.returns('1.2.3')308 process.env.CYPRESS_INSTALL_BINARY = 'www.cypress.io/cannot-download/2.4.5'309 return install.start()310 .then(() => {311 expect(download.start).to.not.be.called312 })313 })314 it('uses cache when mismatch version given URL ', function () {315 state.getBinaryPkgAsync.resolves({ version: '1.2.3' })316 util.pkgVersion.returns('4.0.0')317 process.env.CYPRESS_INSTALL_BINARY = 'www.cypress.io/cannot-download/2.4.5'318 return install.start()319 .then(() => {320 expect(download.start).to.not.be.called321 })322 })323 it('uses cache when correct version installed given Zip', function () {324 sinon.stub(fs, 'pathExistsAsync').withArgs('/path/to/zip.zip').resolves(true)325 state.getBinaryPkgAsync.resolves({ version: '1.2.3' })326 util.pkgVersion.returns('1.2.3')327 process.env.CYPRESS_INSTALL_BINARY = '/path/to/zip.zip'328 return install.start()329 .then(() => {330 expect(unzip.start).to.not.be.called331 })332 })333 it('uses cache when mismatch version given Zip ', function () {334 sinon.stub(fs, 'pathExistsAsync').withArgs('/path/to/zip.zip').resolves(true)335 state.getBinaryPkgAsync.resolves({ version: '1.2.3' })336 util.pkgVersion.returns('4.0.0')337 process.env.CYPRESS_INSTALL_BINARY = '/path/to/zip.zip'338 return install.start()339 .then(() => {340 expect(unzip.start).to.not.be.called341 })342 })343 })344 })345 it('is silent when log level is silent', function () {346 process.env.npm_config_loglevel = 'silent'347 return install.start()348 .then(() => {349 return snapshot(350 'silent install 1',351 normalize(`[no output]${this.stdout.toString()}`),352 )353 })354 })355 })356 context('._getBinaryUrlFromPrereleaseNpmUrl', function () {357 beforeEach(() => {358 os.platform.returns('linux')359 sinon.stub(os, 'arch').returns('x64')360 })361 it('returns binary url for prerelease npm url', function () {362 expect(install._getBinaryUrlFromPrereleaseNpmUrl('https://cdn.cypress.io/beta/npm/5.1.1/ciprovider-branchname-sha/cypress.tgz'))363 .to.eq('https://cdn.cypress.io/beta/binary/5.1.1/linux-x64/ciprovider-branchname-sha/cypress.zip')364 expect(install._getBinaryUrlFromPrereleaseNpmUrl('https://cdn.cypress.io/beta/npm/5.1.1/circle-develop-3fdfc3b453eb38ad3c0b079531e4dde6668e3dd0-436710/cypress.tgz'))365 .to.eq('https://cdn.cypress.io/beta/binary/5.1.1/linux-x64/circle-develop-3fdfc3b453eb38ad3c0b079531e4dde6668e3dd0-436710/cypress.zip')366 })367 it('returns nothing for an invalid url', function () {368 expect(install._getBinaryUrlFromPrereleaseNpmUrl('1.2.3')).to.be.undefined369 expect(install._getBinaryUrlFromPrereleaseNpmUrl(null)).to.be.undefined370 })371 })372 context('._getVersionSpecifier', function () {373 let restoreEnv374 beforeEach(function () {375 sinon.stub(fs, 'readJSON').rejects()376 restoreEnv && restoreEnv()377 })378 it('resolves undefined if no versionSpecifier found', async function () {379 expect(await install._getVersionSpecifier('/foo/bar/baz')).to.be.undefined380 })381 it('resolves with cypress.tgz URL if specified in npm argv', async function () {382 restoreEnv = mockedEnv({383 npm_config_argv: JSON.stringify({384 original: ['npm', 'i', 'https://foo.com/cypress.tgz'],385 }),386 })387 expect(await install._getVersionSpecifier('/foo/bar/baz')).to.eq('https://foo.com/cypress.tgz')388 })389 it('resolves with versionSpecifier from parent pkg.json', async function () {390 fs.readJSON.withArgs('/foo/bar/baz/package.json').resolves({391 dependencies: {392 'cypress': '1.2.3',393 },394 })395 fs.readJSON.withArgs('/foo/bar/package.json').resolves({396 dependencies: {397 'cypress': 'wrong',398 },399 })400 expect(await install._getVersionSpecifier('/foo/bar/baz')).to.eq('1.2.3')401 })402 it('resolves with devDependencies too', async function () {403 fs.readJSON.withArgs('/foo/bar/baz/package.json').resolves({404 devDependencies: {405 'cypress': '4.5.6',406 },407 })408 expect(await install._getVersionSpecifier('/foo/bar/baz')).to.eq('4.5.6')409 })410 it('resolves with optionalDependencies too', async function () {411 fs.readJSON.withArgs('/foo/bar/baz/package.json').resolves({412 optionalDependencies: {413 'cypress': '6.7.8',414 },415 })416 expect(await install._getVersionSpecifier('/foo/bar/baz')).to.eq('6.7.8')417 })418 context('with win32 path functions and paths', async function () {419 const oldPath = _.clone(path)420 beforeEach(() => {421 _.assign(path, path.win32)422 })423 afterEach(() => {424 _.assign(path, oldPath)425 })426 it('resolves undefined if no versionSpecifier found', async function () {427 expect(await install._getVersionSpecifier('C:\\foo\\bar\\baz')).to.be.undefined428 })429 it('resolves with versionSpecifier from parent pkg.json', async function () {430 fs.readJSON.withArgs('C:\\foo\\bar\\baz\\package.json').resolves({431 dependencies: {432 'cypress': '1.2.3',433 },434 })435 fs.readJSON.withArgs('C:\\foo\\bar\\package.json').resolves({436 dependencies: {437 'cypress': 'wrong',438 },439 })440 expect(await install._getVersionSpecifier('C:\\foo\\bar\\baz')).to.eq('1.2.3')441 })442 it('resolves with devDependencies too', async function () {443 fs.readJSON.withArgs('C:\\foo\\bar\\baz\\package.json').resolves({444 devDependencies: {445 'cypress': '4.5.6',446 },447 })448 expect(await install._getVersionSpecifier('C:\\foo\\bar\\baz')).to.eq('4.5.6')449 })450 it('resolves with optionalDependencies too', async function () {451 fs.readJSON.withArgs('C:\\foo\\bar\\baz\\package.json').resolves({452 optionalDependencies: {453 'cypress': '6.7.8',454 },455 })456 expect(await install._getVersionSpecifier('C:\\foo\\bar\\baz')).to.eq('6.7.8')457 })458 })459 })...

Full Screen

Full Screen

state_spec.js

Source:state_spec.js Github

copy

Full Screen

...45 sinon46 .stub(fs, 'readJsonAsync')47 .withArgs(binaryPkgPath)48 .resolves({ version: '2.0.48' })49 return state.getBinaryPkgAsync(binaryDir).then((result) => {50 expect(result).to.deep.equal({ version: '2.0.48' })51 })52 })53 it('returns null if no version found', function () {54 sinon.stub(fs, 'pathExistsAsync').resolves(false)55 return state56 .getBinaryPkgAsync(binaryDir)57 .then((result) => {58 return expect(result).to.equal(null)59 })60 })61 it('returns correct version if passed binaryDir', function () {62 const customBinaryDir = '/custom/binary/dir'63 const customBinaryPackageDir =...

Full Screen

Full Screen

versions_spec.js

Source:versions_spec.js Github

copy

Full Screen

1const { expect } = require('chai')2require('../../spec_helper')3const util = require(`${lib}/util`)4const state = require(`${lib}/tasks/state`)5const versions = require(`${lib}/exec/versions`)6describe('lib/exec/versions', function () {7 const binaryDir = '/cache/1.2.3/Cypress.app'8 beforeEach(function () {9 sinon.stub(state, 'getBinaryDir').returns(binaryDir)10 sinon.stub(state, 'getBinaryPkgAsync').withArgs(binaryDir).resolves({11 version: '1.2.3',12 electronVersion: '10.1.2',13 electronNodeVersion: '12.16.3',14 })15 sinon.stub(util, 'pkgVersion').returns('4.5.6')16 })17 describe('.getVersions', function () {18 it('gets the correct binary and package version', function () {19 return versions.getVersions().then(({ package, binary }) => {20 expect(package, 'package version').to.eql('4.5.6')21 expect(binary, 'binary version').to.eql('1.2.3')22 })23 })24 it('gets the correct Electron and bundled Node version', function () {25 return versions.getVersions().then(({ electronVersion, electronNodeVersion }) => {26 expect(electronVersion, 'electron version').to.eql('10.1.2')27 expect(electronNodeVersion, 'node version').to.eql('12.16.3')28 })29 })30 it('gets correct binary version if CYPRESS_RUN_BINARY', function () {31 sinon.stub(state, 'parseRealPlatformBinaryFolderAsync').resolves('/my/cypress/path')32 process.env.CYPRESS_RUN_BINARY = '/my/cypress/path'33 state.getBinaryPkgAsync34 .withArgs('/my/cypress/path')35 .resolves({36 version: '7.8.9',37 })38 return versions.getVersions().then(({ package, binary }) => {39 expect(package).to.eql('4.5.6')40 expect(binary).to.eql('7.8.9')41 })42 })43 it('reports default versions if not found', function () {44 // imagine package.json only has version there45 state.getBinaryPkgAsync.withArgs(binaryDir).resolves({46 version: '90.9.9',47 })48 return versions.getVersions().then((versions) => {49 expect(versions).to.deep.equal({50 'package': '4.5.6',51 'binary': '90.9.9',52 'electronVersion': 'not found',53 'electronNodeVersion': 'not found',54 })55 })56 })57 })...

Full Screen

Full Screen

versions.js

Source:versions.js Github

copy

Full Screen

1"use strict";2const Promise = require('bluebird');3const debug = require('debug')('cypress:cli');4const path = require('path');5const util = require('../util');6const state = require('../tasks/state');7const {8 throwFormErrorText,9 errors10} = require('../errors');11const getVersions = () => {12 return Promise.try(() => {13 if (util.getEnv('CYPRESS_RUN_BINARY')) {14 let envBinaryPath = path.resolve(util.getEnv('CYPRESS_RUN_BINARY'));15 return state.parseRealPlatformBinaryFolderAsync(envBinaryPath).then(envBinaryDir => {16 if (!envBinaryDir) {17 return throwFormErrorText(errors.CYPRESS_RUN_BINARY.notValid(envBinaryPath))();18 }19 debug('CYPRESS_RUN_BINARY has binaryDir:', envBinaryDir);20 return envBinaryDir;21 }).catch({22 code: 'ENOENT'23 }, err => {24 return throwFormErrorText(errors.CYPRESS_RUN_BINARY.notValid(envBinaryPath))(err.message);25 });26 }27 return state.getBinaryDir();28 }).then(state.getBinaryPkgAsync).then(pkg => {29 const versions = {30 binary: state.getBinaryPkgVersion(pkg),31 electronVersion: state.getBinaryElectronVersion(pkg),32 electronNodeVersion: state.getBinaryElectronNodeVersion(pkg)33 };34 debug('binary versions %o', versions);35 return versions;36 }).then(binaryVersions => {37 const versions = {38 package: util.pkgVersion(),39 binary: binaryVersions.binary || 'not installed',40 electronVersion: binaryVersions.electronVersion || 'not found',41 electronNodeVersion: binaryVersions.electronNodeVersion || 'not found'42 };43 debug('combined versions %o', versions);44 return versions;45 });46};47module.exports = {48 getVersions...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const state = require('cypress/lib/state')2const fs = require('fs')3state.getBinaryPkgAsync().then((binary) => {4 fs.writeFileSync('binary.json', JSON.stringify(binary, null, 2))5})6{7 "bin": {8 },9 "scripts": {10 },11 "repository": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var state = require('cypress/lib/tasks/state')2state.getBinaryPkgAsync().then(function (pkg) {3 console.log('pkg', pkg)4})5{6 "dependencies": {7 }8}

Full Screen

Using AI Code Generation

copy

Full Screen

1const cypress = require('cypress');2const path = require('path');3const binPath = path.join(__dirname, 'cypress', 'bin');4cypress.run({5 config: {6 },7 env: {8 },9 config: {10 },11}).then((results) => {12 console.log(results);13 process.exit(results.totalFailed);14}).catch((err) => {15 console.error(err);16 process.exit(1);17});18{19 "scripts": {20 },21 "devDependencies": {22 }23}24{25}26describe('My First Test', function() {27 it('Does not do much!', function() {28 })29})30{

Full Screen

Using AI Code Generation

copy

Full Screen

1const state = require('cypress/lib/tasks/state');2const pkg = state.getBinaryPkgAsync();3console.log(pkg);4{ version: '2.1.0',5 { test: 'cypress run',6 'test:run': 'cypress run' },7 repository: { type: 'git', url: '

Full Screen

Using AI Code Generation

copy

Full Screen

1const state = require('cypress/lib/state')2const binaryPkg = await state.getBinaryPkgAsync()3const cypress = require('cypress')4const install = require('cypress/lib/tasks/install')5const path = require('path')6const cypressPath = path.join(__dirname, 'node_modules', 'cypress')7const binaryDir = path.join(cypressPath, 'dist')8await install.install(cypressPath, binaryDir, binaryPkg)9cypress.run({10})11cypress.open({12})13cypress.run({14})15cypress.open({16})17cypress.run({18})19cypress.open({20})21cypress.run({22})23cypress.open({24})25cypress.run({26})27cypress.open({28})29cypress.run({30})31cypress.open({32})

Full Screen

Cypress Tutorial

Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.

Chapters:

  1. What is Cypress? -
  2. Why Cypress? - Learn why Cypress might be a good choice for testing your web applications.
  3. Features of Cypress Testing - Learn about features that make Cypress a powerful and flexible tool for testing web applications.
  4. Cypress Drawbacks - Although Cypress has many strengths, it has a few limitations that you should be aware of.
  5. Cypress Architecture - Learn more about Cypress architecture and how it is designed to be run directly in the browser, i.e., it does not have any additional servers.
  6. Browsers Supported by Cypress - Cypress is built on top of the Electron browser, supporting all modern web browsers. Learn browsers that support Cypress.
  7. Selenium vs Cypress: A Detailed Comparison - Compare and explore some key differences in terms of their design and features.
  8. Cypress Learning: Best Practices - Take a deep dive into some of the best practices you should use to avoid anti-patterns in your automation tests.
  9. How To Run Cypress Tests on LambdaTest? - Set up a LambdaTest account, and now you are all set to learn how to run Cypress tests.

Certification

You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.

YouTube

Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful