This repository was archived by the owner on Jan 31, 2023. It is now read-only.
This repository was archived by the owner on Jan 31, 2023. It is now read-only.
Add environment variable #11
Closed
Description
Sometimes users want to conditionally stub network calls, for example when running on local environment. They could do something like this
// stub-services.js
export default function() {
cy.server();
cy.route('/api/../me', 'fx:services/me.json').as('me_stub');
cy.route('/api/../permissions', 'fx:services/permissions.json')
.as('permissions_stub');
// Lots of fixtures ...
}
// spec.js
import stubServices from '../../support/stub-services';
const isLocalHost = () => Cypress.env('ENVIRONMENT') === "localhost";
it('works', () => {
if (isLocalHost()) {
stubServices();
}
...
})
We could make user's life a little easier. First, even now we can show an example like this
// spec.js
import stubServices from '../../support/stub-services'
import {onlyOn} from '@cypress/skip-test'
const isLocalHost = () => Cypress.env('ENVIRONMENT') === "localhost";
it('works', () => {
onlyOn(isLocalHost(), stubServices)
...
})
Second, we could use Cypress.env('ENVIRONMENT')
as another check, similar to platform. Thus we could say
import stubServices from '../../support/stub-services'
import {onlyOn} from '@cypress/skip-test'
it('works', () => {
onlyOn('localhost', stubServices)
...
})
and have onlyOn
and skipOnly
check Cypress.env('ENVIRONMENT')
variable and if it exists and matches given value do their function.