Skip to content

Commit 616cdb4

Browse files
committed
Update readme
1 parent 42b941c commit 616cdb4

File tree

1 file changed

+62
-2
lines changed

1 file changed

+62
-2
lines changed

README.md

+62-2
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,11 @@ yarn add cypress-on-rails --dev
355355
### for VCR
356356

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

359-
#### setup
362+
#### basic setup
360363

361364
Add your VCR configuration to your `cypress_helper.rb`
362365

@@ -381,13 +384,16 @@ VCR.turn_off!
381384
WebMock.disable! if defined?(WebMock)
382385
```
383386

387+
#### insert/eject setup
388+
384389
Add to your `config/cypress_on_rails.rb`:
385390

386391
```ruby
387392
c.use_vcr_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
393+
# c.use_vcr_use_cassette_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
388394
```
389395

390-
#### usage
396+
#### insert/eject usage
391397

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

@@ -414,6 +420,60 @@ describe('My First Test', () => {
414420
})
415421
```
416422

423+
#### use_cassette setup
424+
425+
Add to your `config/cypress_on_rails.rb`:
426+
427+
```ruby
428+
# c.use_vcr_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
429+
c.use_vcr_use_cassette_middleware = !Rails.env.production? && ENV['CYPRESS'].present?
430+
```
431+
432+
Adjust record mode in `config/cypress_on_rails.rb` if needed:
433+
434+
```ruby
435+
c.vcr_record_mode = :once # Use to choose VCR record mode
436+
```
437+
438+
Add to your `cypress/support/command.js`:
439+
440+
```js
441+
// Add proxy-like mock to add operation name into query string
442+
Cypress.Commands.add('mockGraphQL', () => {
443+
cy.on('window:before:load', (win) => {
444+
const originalFetch = win.fetch;
445+
const fetch = (path, options, ...rest) => {
446+
if (options && options.body) {
447+
try {
448+
const body = JSON.parse(options.body);
449+
if (body.operationName) {
450+
return originalFetch(`${path}?operation=${body.operationName}`, options, ...rest);
451+
}
452+
} catch (e) {
453+
return originalFetch(path, options, ...rest);
454+
}
455+
}
456+
return originalFetch(path, options, ...rest);
457+
};
458+
cy.stub(win, 'fetch', fetch);
459+
});
460+
});
461+
```
462+
463+
Add to your `cypress/support/on-rails.js`, to `beforeEach`:
464+
465+
```js
466+
cy.mockGraphQL() // for GraphQL usage with use_cassette, see cypress/support/commands.rb
467+
```
468+
469+
#### use_cassette usage
470+
471+
There's nothing special to be called during Cypress scenario. Each request will be wrapped with `VCR.use_cassette`.
472+
Consider VCR configuration in `cypress_helper.rb` to ignore hosts.
473+
474+
All cassettes will be recorded and saved automatically, using the pattern `<vcs_cassettes_path>/graphql/<operation_name>`
475+
476+
417477
## `before_request` configuration
418478

419479
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)