Skip to content

add wrapping application promises example #1808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/_changelogs/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- Plugins can now return a `Promise` and modify the yielded `config` to set new configuration values and environment variables. Fixes {% issue 1042 %} and {% issue 909 %} and {% issue 576 %}.
- There is a new {% url "`userAgent`" configuration#Browser %} option in `cypress.json`. This enables you to change the `userAgent` of the browser for the entire browsing session. Fixes {% issue 364 %}.
- There is a new {% url "`blacklistHosts`" configuration#Browser %} option in `cypress.json` that allows you to block requests made to those hosts. Blocked requests will respond with a `503` status code. This is useful for blocking 3rd party domains like Google Analytics. We have added a {% url "new 'Stubbing Google Analytics' recipe" recipes#Stubbing-Spying %} demonstrating this. Fixes {% issue 442 %}.
- There is a new {% url "`blacklistHosts`" configuration#Browser %} option in `cypress.json` that allows you to block requests made to those hosts. Blocked requests will respond with a `503` status code. This is useful for blocking 3rd party domains like Google Analytics. We have added a {% url "new 'Stubbing Google Analytics' recipe" recipes#Stubbing-and-spying %} demonstrating this. Fixes {% issue 442 %}.
- Added chai assertions in TypeScript to generate autocompletion. Fixes {% issue 1073 %}.

**Bugfixes:**
Expand Down
4 changes: 2 additions & 2 deletions source/api/commands/clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cy.clock(Date.UTC(2018, 10, 30), ['Date'])
### `Using cy.clock()` with `cy.tick()`

{% note info %}
{% url 'Check out our example recipe testing spying, stubbing and time.' recipes#Stubbing-Spying %}
{% url 'Check out our example recipe testing spying, stubbing and time.' recipes#Stubbing-and-spying %}
{% endnote %}

# Notes
Expand Down Expand Up @@ -227,4 +227,4 @@ When clicking on the `clock` command within the command log, the console outputs
- {% url `cy.stub()` stub %}
- {% url `cy.tick()` tick %}
- {% url 'Guide: Stubs, Spies and Clocks' stubs-spies-and-clocks %}
- {% url 'Recipe: Stubbing, Spying' recipes#Stubbing-Spying %}
- {% url 'Recipe: Stubbing, Spying' recipes#Stubbing-and-spying %}
2 changes: 1 addition & 1 deletion source/api/commands/its.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ cy
### Use `.its()` to test `window.fetch`

{% note info %}
{% url "Check out our example recipe on testing `window.fetch` using `.its()`" recipes#Stubbing-Spying %}
{% url "Check out our example recipe on testing `window.fetch` using `.its()`" recipes#Stubbing-and-spying %}
{% endnote %}

## Nested Properties
Expand Down
4 changes: 2 additions & 2 deletions source/api/commands/spy.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const stub = cy.stub(obj, 'foo').log(false)
### More `cy.spy()` examples

{% note info %}
{% url "Check out our example recipe testing spying, stubbing and time" recipes#Stubbing-Spying %}
{% url "Check out our example recipe testing spying, stubbing and time" recipes#Stubbing-and-spying %}
{% endnote %}

## Aliases
Expand Down Expand Up @@ -154,5 +154,5 @@ When clicking on the `spy-1` event within the command log, the console outputs t
- {% url `.as()` as %}
- {% url `cy.clock()` clock %}
- {% url 'Guide: Stubs, Spies and Clocks' stubs-spies-and-clocks %}
- {% url "Recipe: Stubbing, Spying" recipes#Stubbing-Spying %}
- {% url "Recipe: Stubbing, Spying" recipes#Stubbing-and-spying %}
- {% url `cy.stub()` stub %}
4 changes: 2 additions & 2 deletions source/api/commands/stub.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const stub = cy.stub(obj, 'foo').log(false)
### More `cy.stub()` examples

{% note info %}
{% url "Check out our example recipe testing spying, stubbing and time" recipes#Stubbing-Spying %}
{% url "Check out our example recipe testing spying, stubbing and time" recipes#Stubbing-and-spying %}
{% endnote %}

## Aliases
Expand Down Expand Up @@ -210,6 +210,6 @@ When clicking on the `(stub-1)` event within the command log, the console output
- {% url `cy.clock()` clock %}
- {% url `cy.spy()` spy %}
- {% url 'Guide: Stubs, Spies and Clocks' stubs-spies-and-clocks %}
- {% url "Recipe: Stubbing, Spying" recipes#Stubbing-Spying %}
- {% url "Recipe: Stubbing, Spying" recipes#Stubbing-and-spying %}
- {% url "Recipe: Unit Test - Stubbing Dependencies" recipes %}
- {% url "Stub navigator API in end-to-end tests" https://glebbahmutov.com/blog/stub-navigator-api/ %}
4 changes: 2 additions & 2 deletions source/api/commands/tick.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ cy.get('#header').should('have.text', 'Hello, World')
### Using `cy.clock()` with `cy.tick()`

{% note info %}
{% url "Check out our example recipe testing spying, stubbing and time" recipes#Stubbing-Spying %}
{% url "Check out our example recipe testing spying, stubbing and time" recipes#Stubbing-and-spying %}
{% endnote %}

# Rules
Expand Down Expand Up @@ -112,4 +112,4 @@ When clicking on the `tick` command within the command log, the console outputs
- {% url `cy.spy()` spy %}
- {% url `cy.stub()` stub %}
- {% url 'Guide: Stubs, Spies and Clocks' stubs-spies-and-clocks %}
- {% url "Recipe: Stubbing, Spying" recipes#Stubbing-Spying %}
- {% url "Recipe: Stubbing, Spying" recipes#Stubbing-and-spying %}
4 changes: 2 additions & 2 deletions source/api/commands/visit.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cy.visit('http://localhost:3000/#dashboard', {
Check out our example recipes using `cy.visit()`'s `onBeforeLoad` option to:
- {% url 'Bootstraping your App' recipes#Server-Communication %}
- {% url 'Set a token to `localStorage` for login during Single Sign On' recipes#Logging-In %}
- {% url 'Stub `window.fetch`' recipes#Stubbing-Spying %}
- {% url 'Stub `window.fetch`' recipes#Stubbing-and-spying %}
{% endnote %}

### Provide an `onLoad` callback function
Expand Down Expand Up @@ -303,4 +303,4 @@ When clicking on `visit` within the command log, the console outputs the followi
- {% url `cy.request()` request %}
- {% url "Recipe: Bootstrapping your App" recipes#Server-Communication %}
- {% url "Recipe: Logging In - Single Sign on" recipes#Logging-In %}
- {% url "Recipe: Stubbing `window.fetch`" recipes#Stubbing-Spying %}
- {% url "Recipe: Stubbing `window.fetch`" recipes#Stubbing-and-spying %}
34 changes: 34 additions & 0 deletions source/api/commands/wrap.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ cy
})
```

## Promises

You can wrap promises returned by the application code. Cypress commands will automatically wait for the promise to resolve before continuing with the yielded value to the next command or assertion. See the {% url "Logging in using application code" recipes#Logging-In %} recipe for the full example.

```javascript
// import application code for logging in
import { userService } from '../../src/_services/user.service'

it('can assert against resolved object using .should', () => {
cy.log('user service login')
const username = Cypress.env('username')
const password = Cypress.env('password')

// wrap the promise returned by the application code
cy.wrap(userService.login(username, password))
// check the yielded object
.should('be.an', 'object')
.and('have.keys', ['firstName', 'lastName', 'username', 'id', 'token'])
.and('contain', {
username: 'test',
firstName: 'Test',
lastName: 'User'
})

// cy.visit command will wait for the promise returned from
// the "userService.login" to resolve. Then local storage item is set
// and the visit will immediately be authenticated and logged in
cy.visit('/')
// we should be logged in
cy.contains('Hi Test!').should('be.visible')
})
```

# Rules

## Requirements {% helper_icon requirements %}
Expand Down Expand Up @@ -125,3 +158,4 @@ When clicking on the `wrap` command within the command log, the console outputs
- {% url `.should()` should %}
- {% url `.spread()` spread %}
- {% url `.then()` then %}
- {% url "Logging in using application code" recipes#Logging-In %} recipe
57 changes: 36 additions & 21 deletions source/examples/examples/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,26 @@ containerClass: examples

Recipes show you how to test common scenarios in Cypress.

{% fa fa-github %} {% url https://github.com/cypress-io/cypress-example-recipes %}
{% fa fa-github %} {% url https://github.com/cypress-io/cypress-example-recipes %}

## Fundamentals

Recipe | Description
--- | ---
{% url 'Node Modules' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__node-modules %} | Import your own Node modules
{% url "Environment variables" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/server-communication__env-variables %} | Passing environment variables to tests
{% url "Dynamic tests" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__dynamic-tests %} | Create tests dynamically from data
{% url "Fixtures" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__fixtures %} | Loading single or multiple fixtures
{% url "Adding Chai Assertions" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/extending-cypress__chai-assertions %} | Add new or custom chai assertions
{% url "Cypress module API" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__module-api %} | Run Cypress via its module API

## Testing the DOM
Recipe | Description
--- | ---
{% url 'Tab Handling and Links' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__tab-handling-links %} | Links that open in a new tab
{% url 'Hover and Hidden Elements' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__hover-hidden-elements %} | Test hidden elements requiring hover
{% url 'Form Interactions' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__form-interactions %} | Test form elements like input type `range`
{% url 'Drag and Drop' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__drag-drop %} | Use `.trigger()` to test drag and drop

## Logging In
Recipe | Description
Expand All @@ -20,31 +34,34 @@ Recipe | Description
{% url 'XHR Web Forms' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/logging-in__xhr-web-forms %} | Log in using an XHR
{% url 'CSRF Tokens' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/logging-in__csrf-tokens %} | Log in with a required CSRF token
{% url 'Json Web Tokens' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/logging-in__jwt %} | Log in using JWT
{% url 'Using application code' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/logging-in__using-app-code %} | Log in by calling the application code

## Testing the DOM
Recipe | Description
--- | ---
{% url 'Tab Handling and Links' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__tab-handling-links %} | Links that open in a new tab
{% url 'Hover and Hidden Elements' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__hover-hidden-elements %} | Test hidden elements requiring hover
{% url 'Form Interactions' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__form-interactions %} | Test form elements like input type `range`
{% url 'Drag and Drop' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__drag-drop %} | Use `.trigger()` to test drag and drop
Also see {% url "Authentication plugins" plugins#authentication and watch {% url "Organizing Tests, Logging In, Controlling State" https://www.youtube.com/watch?v=5XQOK0v_YRE %}

## Preprocessors
Recipe | Description
--- | ---
--- | ---
{% url 'grep' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/preprocessors__grep %} | Filter tests by name using Mocha-like `grep` syntax
{% url 'TypeScript with Browserify' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/preprocessors__typescript-browserify %} | Add TypeScript support with Browserify
{% url 'TypeScript with webpack' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/preprocessors__typescript-webpack %} | Add TypeScript support with webpack

## Blogs

Demo recipes from the blog posts at {% url "Cypress blog" https://www.cypress.io/blog %}.

Recipe | Description
--- | ---
{% url 'Application Actions' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__application-actions %} | Application actions are a replacement for Page Objects
{% url 'Direct Control of AngularJS' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__direct-control-angular %} | Bypass the DOM and control AngularJS
{% url 'E2E API Testing' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__e2e-api-testing %} | Run your API Tests with a GUI
{% url "E2E Snapshots" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__e2e-snapshots %} | End-to-End Snapshot Testing
{% url "Element Coverage" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__element-coverage %} | Track elements covered by tests
{% url 'Codepen.io Testing' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__codepen-demo %} | Test a HyperApp Codepen demo
{% url 'Redux Testing' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__testing-redux-store %} | Test an application that uses central Redux data store
{% url 'Testing Redux Store' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__testing-redux-store %} | Test an application that uses central Redux data store
{% url 'Vue + Vuex + REST Testing' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__vue-vuex-rest %} | Test an application that uses central Vuex data store
{% url "A11y Testing" https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/blogs__a11y %} | Accessability testing with {% url "cypress-axe" https://github.com/avanslaars/cypress-axe %}

## Stubbing, Spying
## Stubbing and spying
Recipe | Description
--- | ---
{% url 'Stubbing Functions' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/stubbing-spying__functions %} | Use `cy.stub()` to test function calls
Expand All @@ -59,19 +76,17 @@ Recipe | Description
{% url 'React' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/unit-testing__react %} | Test your react components in isolation
{% url 'File Upload in React' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/file-upload-react %} | Test file upload in React application

## Extending Cypress
Recipe | Description
--- | ---
{% url 'Adding Chai Assertions' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/extending-cypress__chai-assertions %} | Add new or custom chai assertions

## Server Communication
Recipe | Description
--- | ---
{% url 'Bootstrapping your App' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/server-communication__bootstrapping-your-app %} | Seed your application with test data
{% url 'Seeding your Database in Node' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/server-communication__seeding-database-in-node %} | Seed your database with test data
{% url 'Environment Variables' https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/server-communication__env-variables %} | Pass environment variables to your tests

## Continuous Integration
Recipe | Description
--- | ---
{% url 'Cypress CircleCI Orb' https://github.com/cypress-io/cypress-example-circleci-orb %} | Install, cache and run Cypress tests on CircleCI with minimal configuration.
## Community Recipes

Recipe | Description
--- | ---
{% url "Visual Regression Testing" https://github.com/mjhea0/cypress-visual-regression %} | Adding visual regression testing to Cypress
{% url "Code coverage" https://github.com/paulfalgout/cypress-coverage-example %} | Cypress with Coverage reports
{% url "Cucumber" https://github.com/TheBrainFamily/cypress-cucumber-example %} | Example usage of Cypress with Cucumber
{% url "Jest" https://github.com/TheBrainFamily/jest-runner-cypress-example %} | Example for the jest-runner-cypress
4 changes: 2 additions & 2 deletions source/faq/questions/using-cypress-faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,13 @@ Yes. {% url "You can override this with `userAgent` in `cypress.json`." configur

Yes. {% url "You can set this with `blacklistHosts` in `cypress.json`." configuration#Browser %}

Also, check out our {% url 'Stubbing Google Analytics Recipe' recipes#Stubbing-Spying %}.
Also, check out our {% url 'Stubbing Google Analytics Recipe' recipes#Stubbing-and-spying %}.

## {% fa fa-angle-right %} How can I verify that calls to analytics like Google Analytics are being made correct?

You can stub their functions and then ensure they're being called.

Check out our {% url 'Stubbing Google Analytics Recipe' recipes#Stubbing-Spying %}.
Check out our {% url 'Stubbing Google Analytics Recipe' recipes#Stubbing-and-spying %}.

## {% fa fa-angle-right %} I'm trying to test a chat application. Can I run more than one browser at a time with Cypress?

Expand Down
2 changes: 1 addition & 1 deletion source/guides/guides/stubs-spies-and-clocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You can refer to each of these libraries' documentation for more examples and ex
# Common Scenarios

{% note info Example test! %}
{% url 'Check out our example recipe testing spying, stubbing and time' recipes#Stubbing-Spying %}
{% url 'Check out our example recipe testing spying, stubbing and time' recipes#Stubbing-and-spying %}
{% endnote %}

## Stubs
Expand Down
2 changes: 1 addition & 1 deletion source/guides/references/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ When you open a Cypress project, clicking on the *Settings* tab will display the

By passing a string or array of strings you can block requests made to one or more hosts.

To see a working example of this please check out our {% url 'Stubbing Google Analytics Recipe' recipes#Stubbing-Spying %}.
To see a working example of this please check out our {% url 'Stubbing Google Analytics Recipe' recipes#Stubbing-and-spying %}.

To blacklist a host:

Expand Down
2 changes: 1 addition & 1 deletion source/ja/_changelogs/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

- Plugins can now return a `Promise` and modify the yielded `config` to set new configuration values and environment variables. Fixes {% issue 1042 %} and {% issue 909 %} and {% issue 576 %}.
- There is a new {% url "`userAgent`" configuration#Browser %} option in `cypress.json`. This enables you to change the `userAgent` of the browser for the entire browsing session. Fixes {% issue 364 %}.
- There is a new {% url "`blacklistHosts`" configuration#Browser %} option in `cypress.json` that allows you to block requests made to those hosts. Blocked requests will respond with a `503` status code. This is useful for blocking 3rd party domains like Google Analytics. We have added a {% url "new 'Stubbing Google Analytics' recipe" recipes#Stubbing-Spying %} demonstrating this. Fixes {% issue 442 %}.
- There is a new {% url "`blacklistHosts`" configuration#Browser %} option in `cypress.json` that allows you to block requests made to those hosts. Blocked requests will respond with a `503` status code. This is useful for blocking 3rd party domains like Google Analytics. We have added a {% url "new 'Stubbing Google Analytics' recipe" recipes#Stubbing-and-spying %} demonstrating this. Fixes {% issue 442 %}.
- Added chai assertions in TypeScript to generate autocompletion. Fixes {% issue 1073 %}.

**Bugfixes:**
Expand Down
4 changes: 2 additions & 2 deletions source/ja/api/commands/clock.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ cy.clock(Date.UTC(2018, 10, 30), ['Date'])
### `Using cy.clock()` with `cy.tick()`

{% note info %}
{% url 'Check out our example recipe testing spying, stubbing and time.' recipes#Stubbing-Spying %}
{% url 'Check out our example recipe testing spying, stubbing and time.' recipes#Stubbing-and-spying %}
{% endnote %}

# Notes
Expand Down Expand Up @@ -227,4 +227,4 @@ When clicking on the `clock` command within the command log, the console outputs
- {% url `cy.stub()` stub %}
- {% url `cy.tick()` tick %}
- {% url 'Guide: Stubs, Spies and Clocks' stubs-spies-and-clocks %}
- {% url 'Recipe: Stubbing, Spying' recipes#Stubbing-Spying %}
- {% url 'Recipe: Stubbing, Spying' recipes#Stubbing-and-spying %}
2 changes: 1 addition & 1 deletion source/ja/api/commands/its.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ cy
### Use `.its()` to test `window.fetch`

{% note info %}
{% url "Check out our example recipe on testing `window.fetch` using `.its()`" recipes#Stubbing-Spying %}
{% url "Check out our example recipe on testing `window.fetch` using `.its()`" recipes#Stubbing-and-spying %}
{% endnote %}

## Nested Properties
Expand Down
Loading