Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Add environment variable #11

Closed
bahmutov opened this issue Nov 14, 2019 · 1 comment · Fixed by #12
Closed

Add environment variable #11

bahmutov opened this issue Nov 14, 2019 · 1 comment · Fixed by #12
Assignees
Labels

Comments

@bahmutov
Copy link
Contributor

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.

@bahmutov
Copy link
Contributor Author

🎉 This issue has been resolved in version 2.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant