Skip to content

Commit a71b949

Browse files
committed
Update readme
1 parent 47af81d commit a71b949

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,11 @@ yarn add cypress-on-rails --dev
382382
### for VCR
383383

384384
This only works when you start the Rails server with a single worker and single thread
385+
It can be used in two modes:
386+
- with separate insert/eject calls (more general, recommended way)
387+
- with use_cassette wrapper (supports only GraphQL integration)
385388

386-
#### setup
389+
#### basic setup
387390

388391
Add your VCR configuration to your `cypress_helper.rb`
389392

@@ -408,13 +411,16 @@ VCR.turn_off!
408411
WebMock.disable! if defined?(WebMock)
409412
```
410413

414+
#### insert/eject setup
415+
411416
Add to your `config/cypress_on_rails.rb`:
412417

413418
```ruby
414419
c.use_vcr_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
420+
# c.use_vcr_use_cassette_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
415421
```
416422

417-
#### usage
423+
#### insert/eject usage
418424

419425
You have `vcr_insert_cassette` and `vcr_eject_cassette` available. https://www.rubydoc.info/github/vcr/vcr/VCR:insert_cassette
420426

@@ -441,6 +447,60 @@ describe('My First Test', () => {
441447
})
442448
```
443449

450+
#### use_cassette setup
451+
452+
Add to your `config/cypress_on_rails.rb`:
453+
454+
```ruby
455+
# c.use_vcr_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
456+
c.use_vcr_use_cassette_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
457+
```
458+
459+
Adjust record mode in `config/cypress_on_rails.rb` if needed:
460+
461+
```ruby
462+
c.vcr_record_mode = :once # Use to choose VCR record mode
463+
```
464+
465+
Add to your `cypress/support/command.js`:
466+
467+
```js
468+
// Add proxy-like mock to add operation name into query string
469+
Cypress.Commands.add('mockGraphQL', () => {
470+
cy.on('window:before:load', (win) => {
471+
const originalFetch = win.fetch;
472+
const fetch = (path, options, ...rest) => {
473+
if (options && options.body) {
474+
try {
475+
const body = JSON.parse(options.body);
476+
if (body.operationName) {
477+
return originalFetch(`${path}?operation=${body.operationName}`, options, ...rest);
478+
}
479+
} catch (e) {
480+
return originalFetch(path, options, ...rest);
481+
}
482+
}
483+
return originalFetch(path, options, ...rest);
484+
};
485+
cy.stub(win, 'fetch', fetch);
486+
});
487+
});
488+
```
489+
490+
Add to your `cypress/support/on-rails.js`, to `beforeEach`:
491+
492+
```js
493+
cy.mockGraphQL() // for GraphQL usage with use_cassette, see cypress/support/commands.rb
494+
```
495+
496+
#### use_cassette usage
497+
498+
There's nothing special to be called during Cypress scenario. Each request will be wrapped with `VCR.use_cassette`.
499+
Consider VCR configuration in `cypress_helper.rb` to ignore hosts.
500+
501+
All cassettes will be recorded and saved automatically, using the pattern `<vcs_cassettes_path>/graphql/<operation_name>`
502+
503+
444504
## `before_request` configuration
445505

446506
You may perform any custom action before running a CypressOnRails command, such as authentication, or sending metrics. Please set `before_request` as part of the CypressOnRails configuration.

0 commit comments

Comments
 (0)