Skip to content

Commit

Permalink
extend types, inline config logic [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
kuceb committed Feb 12, 2020
1 parent 648ef18 commit 224f1ec
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
67 changes: 67 additions & 0 deletions cli/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ declare namespace Cypress {
path: string
isHeaded: boolean
isHeadless: boolean
family: string
}

interface LocalStorage {
Expand Down Expand Up @@ -4662,3 +4663,69 @@ Cypress._ // => Lodash _
```
*/
declare const Cypress: Cypress.Cypress & EventEmitter

declare namespace Mocha {
interface TestFunction {
/**
* Describe a specification or test-case with the given `title` and callback `fn` acting
* as a thunk.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn?: Func): Test;

/**
* Describe a specification or test-case with the given `title` and callback `fn` acting
* as a thunk.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn?: AsyncFunc): Test;

}
interface ExclusiveTestFunction {
/**
* Describe a specification or test-case with the given `title` and callback `fn` acting
* as a thunk.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn?: Func): Test;

/**
* Describe a specification or test-case with the given `title` and callback `fn` acting
* as a thunk.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn?: AsyncFunc): Test;
}
interface PendingTestFunction {
/**
* Describe a specification or test-case with the given `title` and callback `fn` acting
* as a thunk.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn?: Func): Test;

/**
* Describe a specification or test-case with the given `title` and callback `fn` acting
* as a thunk.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn?: AsyncFunc): Test;
}

interface SuiteFunction {
/**
* [bdd, tdd] Describe a "suite" with the given `title` and callback `fn` containing
* nested suites.
*
* - _Only available when invoked via the mocha CLI._
*/
(title: string, config: Partial<Cypress.ConfigOptions>, fn: (this: Suite) => void): Suite;
}

}
118 changes: 118 additions & 0 deletions packages/driver/test/cypress/integration/e2e/test-config.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/// <reference path="../../../../../../cli/types/index.d.ts" />
// @ts-check
/* eslint-disable @cypress/dev/skip-comment,mocha/no-exclusive-tests */

const { _ } = Cypress
const _it = it

function overrideIt (fn) {
it = fn()
it['only'] = fn('only')
it['skip'] = fn('skip')
}

overrideIt(function (subFn) {
return function (...args) {

const origIt = subFn ? _it[subFn] : _it

if (args.length > 2 && _.isObject(args[1])) {
const opts = _.defaults({}, args[1], {
browsers: '*',
})

const mochaArgs = [args[0], args[2]]

// return origIt.apply(this, mochaArgs)

if (!shouldRunBrowser(opts.browsers, Cypress.browser.family)) {
mochaArgs[0] = `[browser skip (${opts.browsers})]${mochaArgs[0]}`

if (subFn === 'only') {
mochaArgs[1] = function () {
// @ts-ignore
this.skip()
}

return origIt.apply(this, mochaArgs)
}

return _it['skip'].apply(this, mochaArgs)

}

return origIt.apply(this, mochaArgs)
}

return origIt.apply(this, args)

}
})

const shouldRunBrowser = (browserlist, browser) => {

// return true
let allEnabled = false
const exclude = []
const include = []

browserlist.split(/\s+,\s+/).forEach((v) => {

if (v === '*') {
allEnabled = true

return
}

if (v.includes('!')) {
allEnabled = true
exclude.push(v.slice(1))

return
}

include.push(v)
})

if (!allEnabled) {
return include.includes(browser)
}

return !exclude.includes(browser)

}

describe('foo', () => {
it('bar', {
defaultCommandTimeout: 200,
}, () => {
// test body
})
})

describe('foo', () => {
it.only('bar', {
defaultCommandTimeout: 200,
}, () => {
// test body

})
})

describe('foo', () => {
it.skip('bar', {
defaultCommandTimeout: 200,
}, () => {
// test body
})
})

describe('foo', {
defaultCommandTimeout: 200,
}, () => {
it('bar', {
defaultCommandTimeout: 200,
}, () => {
// test body
})
})

0 comments on commit 224f1ec

Please sign in to comment.