How to use cypress.cli.parseRunArguments method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

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

cypress_spec.js

Source: cypress_spec.js Github

copy
1require('../spec_helper')
2
3const os = require('os')
4const path = require('path')
5const _ = require('lodash')
6const snapshot = require('../support/snapshot')
7const Promise = require('bluebird')
8const tmp = Promise.promisifyAll(require('tmp'))
9const mockfs = require('mock-fs')
10
11const fs = require(`${lib}/fs`)
12const open = require(`${lib}/exec/open`)
13const run = require(`${lib}/exec/run`)
14const cypress = require(`${lib}/cypress`)
15
16describe('cypress', function () {
17  beforeEach(function () {
18    mockfs({})
19  })
20
21  afterEach(() => {
22    mockfs.restore()
23  })
24
25  context('.open', function () {
26    beforeEach(function () {
27      sinon.stub(open, 'start').resolves()
28    })
29
30    const getStartArgs = () => {
31      expect(open.start).to.be.called
32
33      return _.get(open.start, ['lastCall', 'args', 0])
34    }
35
36    it('calls open#start, passing in options', function () {
37      return cypress.open({ foo: 'foo' })
38      .then(getStartArgs)
39      .then((args) => {
40        expect(args.foo).to.equal('foo')
41      })
42    })
43
44    it('normalizes config object', () => {
45      const config = {
46        pageLoadTime: 10000,
47        watchForFileChanges: false,
48      }
49
50      return cypress.open({ config })
51      .then(getStartArgs)
52      .then((args) => {
53        expect(args).to.deep.eq({ config: JSON.stringify(config) })
54      })
55    })
56
57    it('passes configFile: false', () => {
58      const opts = {
59        configFile: false,
60      }
61
62      return cypress.open(opts)
63      .then(getStartArgs)
64      .then((args) => {
65        expect(args).to.deep.eq(opts)
66      })
67    })
68  })
69
70  context('.run fails to write results file', function () {
71    it('resolves with error object', function () {
72      const outputPath = path.join(os.tmpdir(), 'cypress/monorepo/cypress_spec/output.json')
73
74      sinon.stub(tmp, 'fileAsync').resolves(outputPath)
75      sinon.stub(run, 'start').resolves(2)
76      sinon.stub(fs, 'readJsonAsync').withArgs(outputPath).resolves()
77
78      return cypress.run().then((result) => {
79        expect(result).to.deep.equal({
80          status: 'failed',
81          failures: 2,
82          message: 'Could not find Cypress test run results',
83        })
84      })
85    })
86  })
87
88  context('.run', function () {
89    let outputPath
90
91    beforeEach(function () {
92      outputPath = path.join(os.tmpdir(), 'cypress/monorepo/cypress_spec/output.json')
93      sinon.stub(tmp, 'fileAsync').resolves(outputPath)
94      sinon.stub(run, 'start').resolves()
95
96      return fs.outputJsonAsync(outputPath, {
97        code: 0,
98        failingTests: [],
99      })
100    })
101
102    const normalizeCallArgs = (args) => {
103      expect(args.outputPath).to.equal(outputPath)
104      delete args.outputPath
105
106      return args
107    }
108    const getStartArgs = () => {
109      expect(run.start).to.be.called
110
111      return normalizeCallArgs(_.get(run.start, ['lastCall', 'args', 0]))
112    }
113
114    it('calls run#start, passing in options', () => {
115      return cypress.run({ spec: 'foo' })
116      .then(getStartArgs)
117      .then((args) => {
118        expect(args.spec).to.equal('foo')
119      })
120    })
121
122    it('normalizes config object', () => {
123      const config = {
124        pageLoadTime: 10000,
125        watchForFileChanges: false,
126      }
127
128      return cypress.run({ config })
129      .then(getStartArgs)
130      .then((args) => {
131        expect(args).to.deep.eq({ config: JSON.stringify(config) })
132      })
133    })
134
135    it('normalizes env option if passed an object', () => {
136      const env = { foo: 'bar', another: 'one' }
137
138      return cypress.run({ env })
139      .then(getStartArgs)
140      .then((args) => {
141        expect(args).to.deep.eq({ env: JSON.stringify(env) })
142      })
143    })
144
145    it('gets random tmp file and passes it to run#start', function () {
146      return cypress.run().then(() => {
147        expect(run.start.lastCall.args[0].outputPath).to.equal(outputPath)
148      })
149    })
150
151    it('resolves with contents of tmp file', () => {
152      return cypress.run().then(snapshot)
153    })
154
155    it('passes configFile: false', () => {
156      const opts = {
157        configFile: false,
158      }
159
160      return cypress.run(opts)
161      .then(getStartArgs)
162      .then((args) => {
163        expect(args).to.deep.eq(opts)
164      })
165    })
166
167    it('rejects if project is an empty string', () => {
168      return expect(cypress.run({ project: '' })).to.be.rejected
169    })
170
171    it('rejects if project is true', () => {
172      return expect(cypress.run({ project: true })).to.be.rejected
173    })
174
175    it('rejects if project is false', () => {
176      return expect(cypress.run({ project: false })).to.be.rejected
177    })
178
179    it('passes quiet: true', () => {
180      const opts = {
181        quiet: true,
182      }
183
184      return cypress.run(opts)
185      .then(getStartArgs)
186      .then((args) => {
187        expect(args).to.deep.eq(opts)
188      })
189    })
190  })
191
192  context('cli', function () {
193    describe('.parseRunArguments', function () {
194      it('parses CLI cypress run arguments', async () => {
195        const args = 'cypress run --browser chrome --spec my/test/spec.js'.split(' ')
196        const options = await cypress.cli.parseRunArguments(args)
197
198        expect(options).to.deep.equal({
199          browser: 'chrome',
200          spec: 'my/test/spec.js',
201        })
202      })
203
204      it('parses CLI cypress run shorthand arguments', async () => {
205        const args = 'cypress run -b firefox -p 5005 --headed --quiet'.split(' ')
206        const options = await cypress.cli.parseRunArguments(args)
207
208        expect(options).to.deep.equal({
209          browser: 'firefox',
210          port: 5005,
211          headed: true,
212          quiet: true,
213        })
214      })
215
216      it('coerces --record and --dev', async () => {
217        const args = 'cypress run --record false --dev true'.split(' ')
218        const options = await cypress.cli.parseRunArguments(args)
219
220        expect(options).to.deep.equal({
221          record: false,
222          dev: true,
223        })
224      })
225
226      it('coerces --config-file false to boolean', async () => {
227        const args = 'cypress run --config-file false'.split(' ')
228        const options = await cypress.cli.parseRunArguments(args)
229
230        expect(options).to.deep.equal({
231          configFile: false,
232        })
233      })
234
235      it('coerces --config-file cypress.json to string', async () => {
236        const args = 'cypress run --config-file cypress.json'.split(' ')
237        const options = await cypress.cli.parseRunArguments(args)
238
239        expect(options).to.deep.equal({
240          configFile: 'cypress.json',
241        })
242      })
243
244      it('parses config file false', async () => {
245        const args = 'cypress run --config-file false'.split(' ')
246        const options = await cypress.cli.parseRunArguments(args)
247
248        expect(options).to.deep.equal({
249          configFile: false,
250        })
251      })
252
253      it('parses config', async () => {
254        const args = 'cypress run --config baseUrl=localhost,video=true'.split(' ')
255        const options = await cypress.cli.parseRunArguments(args)
256
257        // we don't need to convert the config into an object
258        // since the logic inside cypress.run handles that
259        expect(options).to.deep.equal({
260          config: 'baseUrl=localhost,video=true',
261        })
262      })
263
264      it('parses env', async () => {
265        const args = 'cypress run --env MY_NUMBER=42,MY_FLAG=true'.split(' ')
266        const options = await cypress.cli.parseRunArguments(args)
267
268        // we don't need to convert the environment into an object
269        // since the logic inside cypress.run handles that
270        expect(options).to.deep.equal({
271          env: 'MY_NUMBER=42,MY_FLAG=true',
272        })
273      })
274    })
275  })
276})
277
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)