Skip to content

Commit 17cd554

Browse files
cypress js plugin and vcr integration (#103)
1 parent a35dcd1 commit 17cd554

File tree

165 files changed

+454
-50
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

165 files changed

+454
-50
lines changed

.github/workflows/ruby.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
- run: gem uninstall -v '>= 2' -ax bundler || true
2323
- run: gem install bundler -v '< 2'
2424
- name: Run interaction tests
25-
run: ./spec/integrations/rails_3_2/test.sh
25+
run: ./specs_e2e/rails_3_2/test.sh
2626

2727
rails_4_2:
2828
runs-on: ubuntu-latest
@@ -39,7 +39,7 @@ jobs:
3939
- run: gem uninstall -v '>= 2' -ax bundler || true
4040
- run: gem install bundler -v '< 2'
4141
- name: Run interaction tests
42-
run: ./spec/integrations/rails_4_2/test.sh
42+
run: ./specs_e2e/rails_4_2/test.sh
4343

4444
rails_5_2:
4545
runs-on: ubuntu-latest
@@ -54,4 +54,4 @@ jobs:
5454
- name: Run tests
5555
run: bundle exec rake
5656
- name: Run interaction tests
57-
run: ./spec/integrations/rails_5_2/test.sh
57+
run: ./specs_e2e/rails_5_2/test.sh

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ spec/examples.txt
66
spec/test.log
77
pkg/*.gem
88
vendor/bundle
9-
.vscode
9+
.vscode
10+
node_modules
11+
package-lock.json
12+
yarn.lock

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## []
2+
3+
### Changed
4+
* Add support for matching npm package and VCR
5+
16
## [1.12.1]
27
[Compare]: https://github.com/shakacode/cypress-on-rails/compare/v1.12.0...v1.12.1
38

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,68 @@ describe('My First Test', function() {
271271
})
272272
```
273273

274+
## Expermintal Features (matching npm package)
275+
276+
Please test and give feedback
277+
278+
add the npm package:
279+
280+
```
281+
yarn add cypress-on-rails --dev
282+
```
283+
284+
### for VCR
285+
286+
#### setup
287+
288+
Add you VCR configuration to your `cypress_helper.rb`
289+
290+
```ruby
291+
require 'vcr'
292+
VCR.configure do |config|
293+
config.hook_into :webmock
294+
end
295+
```
296+
297+
Add to you `cypress/support/index.js`
298+
299+
```js
300+
import 'cypress-on-rails/support/index'
301+
```
302+
303+
Add to you `clean.rb`
304+
305+
```ruby
306+
VCR.eject_cassette # make sure we no cassettes inserted before the next test starts
307+
```
308+
309+
#### usage
310+
311+
You have `vcr_insert_cassette` and `vcr_eject_cassette` available. https://www.rubydoc.info/github/vcr/vcr/VCR:insert_cassette
312+
313+
314+
```js
315+
describe('My First Test', function() {
316+
beforeEach(() => { cy.app('load_seed') })
317+
318+
it('visit root', function() {
319+
cy.app('clean') // have a look at cypress/app_commands/clean.rb
320+
321+
cy.vcr_insert_cassette('cats', { record: "new_episodes" })
322+
cy.visit('/using_vcr/index')
323+
324+
cy.get('a').contains('Cats').click()
325+
cy.contains('Wikipedia has a recording of a cat meowing, because why not?')
326+
327+
cy.vcr_eject_cassette();
328+
329+
cy.vcr_insert_cassette('cats')
330+
cy.visit('/using_vcr/record_cats')
331+
cy.contains('Wikipedia has a recording of a cat meowing, because why not?')
332+
})
333+
})
334+
```
335+
274336
## Usage with other rack applications
275337

276338
Add CypressOnRails to your config.ru

cypress-on-rails.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
2020
s.add_development_dependency 'rspec'
2121
s.add_development_dependency 'railties', '>= 3.2'
2222
s.add_development_dependency 'factory_bot'
23+
s.add_development_dependency 'vcr'
2324
s.metadata = {
2425
"bug_tracker_uri" => "https://github.com/shakacode/cypress-on-rails/issues",
2526
"changelog_uri" => "https://github.com/shakacode/cypress-on-rails/blob/master/CHANGELOG.md",

lib/cypress_on_rails/middleware.rb

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
require 'json'
22
require 'rack'
3-
require 'cypress_on_rails/configuration'
3+
require 'cypress_on_rails/middleware_config'
44
require 'cypress_on_rails/command_executor'
55

66
module CypressOnRails
77
# Middleware to handle cypress commands and eval
88
class Middleware
9+
include MiddlewareConfig
10+
911
def initialize(app, command_executor = CommandExecutor, file = ::File)
1012
@app = app
1113
@command_executor = command_executor
@@ -23,14 +25,6 @@ def call(env)
2325

2426
private
2527

26-
def configuration
27-
CypressOnRails.configuration
28-
end
29-
30-
def logger
31-
configuration.logger
32-
end
33-
3428
Command = Struct.new(:name, :options, :cypress_folder) do
3529
# @return [Array<Cypress::Middleware::Command>]
3630
def self.from_body(body, configuration)
@@ -71,7 +65,8 @@ def handle_command(req)
7165
[500, {'Content-Type' => 'application/json'}, [output]]
7266
end
7367
else
74-
[404, {}, ["could not find command file: #{missing_command.file_path}"]]
68+
output = {"message" => "could not find command file: #{missing_command.file_path}"}.to_json
69+
[404, {'Content-Type' => 'application/json'}, [output]]
7570
end
7671
end
7772
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'json'
2+
require 'rack'
3+
require 'cypress_on_rails/configuration'
4+
5+
module CypressOnRails
6+
module MiddlewareConfig
7+
protected
8+
9+
def configuration
10+
CypressOnRails.configuration
11+
end
12+
13+
def logger
14+
configuration.logger
15+
end
16+
end
17+
end

lib/cypress_on_rails/railtie.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
require 'rails/railtie'
22
require 'cypress_on_rails/configuration'
3-
require 'cypress_on_rails/middleware'
43

54
module CypressOnRails
65
class Railtie < Rails::Railtie
76
initializer :setup_cypress_middleware, after: :load_config_initializers do |app|
87
if CypressOnRails.configuration.use_middleware?
8+
require 'cypress_on_rails/middleware'
99
app.middleware.use Middleware
10+
require 'cypress_on_rails/vcr_middleware'
11+
app.middleware.use VCRMiddleware
1012
end
1113
end
1214
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require 'json'
2+
require 'rack'
3+
require 'cypress_on_rails/middleware_config'
4+
5+
module CypressOnRails
6+
# Middleware to handle vcr
7+
class VCRMiddleware
8+
include MiddlewareConfig
9+
10+
def initialize(app, vcr = nil)
11+
@app = app
12+
@vcr = vcr
13+
end
14+
15+
def call(env)
16+
request = Rack::Request.new(env)
17+
if request.path.start_with?('/__cypress__/vcr/insert')
18+
configuration.tagged_logged { handle_insert(request) }
19+
elsif request.path.start_with?('/__cypress__/vcr/eject')
20+
configuration.tagged_logged { handle_eject }
21+
else
22+
@app.call(env)
23+
end
24+
end
25+
26+
private
27+
28+
def handle_insert(req)
29+
body = JSON.parse(req.body.read)
30+
logger.info "vcr insert cassette: #{body}"
31+
cassette_name = body[0]
32+
options = (body[1] || {}).symbolize_keys
33+
options[:record] = options[:record].to_sym if options[:record]
34+
options[:match_requests_on] = options[:match_requests_on].map(&:to_sym) if options[:match_requests_on]
35+
options[:serialize_with] = options[:serialize_with].to_sym if options[:serialize_with]
36+
options[:persist_with] = options[:persist_with].to_sym if options[:persist_with]
37+
vcr.insert_cassette(cassette_name, options)
38+
[201, {'Content-Type' => 'application/json'}, [{'message': 'OK'}.to_json]]
39+
rescue LoadError, ArgumentError => e
40+
[501, {'Content-Type' => 'application/json'}, [{'message': e.message}.to_json]]
41+
end
42+
43+
def handle_eject
44+
logger.info "vcr eject cassette"
45+
vcr.eject_cassette
46+
[201, {'Content-Type' => 'application/json'}, [{'message': 'OK'}.to_json]]
47+
rescue LoadError, ArgumentError => e
48+
[501, {'Content-Type' => 'application/json'}, [{'message': e.message}.to_json]]
49+
end
50+
51+
def vcr
52+
return @vcr if @vcr
53+
require 'vcr'
54+
VCR.configure do |config|
55+
config.cassette_library_dir = "#{configuration.cypress_folder}/fixtures/vcr_cassettes"
56+
end
57+
@vcr = VCR
58+
end
59+
end
60+
end

lib/generators/cypress_on_rails/install_generator.rb

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,37 @@ class InstallGenerator < Rails::Generators::Base
44
class_option :install_cypress, type: :boolean, default: true
55
class_option :install_cypress_with, type: :string, default: 'yarn'
66
class_option :install_cypress_examples, type: :boolean, default: false
7+
class_option :experimental, type: :boolean, default: false
78
source_root File.expand_path('../templates', __FILE__)
89

910
def install_cypress
10-
if !Dir.exists?(options.cypress_folder) || Dir["#{options.cypress_folder}/*"].empty?
11-
directories = options.cypress_folder.split('/')
12-
directories.pop
13-
install_dir = "#{Dir.pwd}/#{directories.join('/')}"
14-
command = nil
15-
if options.install_cypress
16-
if options.install_cypress_with == 'yarn'
17-
command = "yarn --cwd=#{install_dir} add cypress --dev"
18-
elsif options.install_cypress_with == 'npm'
19-
command = "cd #{install_dir}; npm install cypress --save-dev"
20-
end
21-
if command
22-
say command
23-
fail 'failed to install cypress' unless system(command)
24-
end
11+
directories = options.cypress_folder.split('/')
12+
directories.pop
13+
install_dir = "#{Dir.pwd}/#{directories.join('/')}"
14+
command = nil
15+
if options.install_cypress
16+
if options.install_cypress_with == 'yarn'
17+
command = "yarn --cwd=#{install_dir} add cypress --dev"
18+
elsif options.install_cypress_with == 'npm'
19+
command = "cd #{install_dir}; npm install cypress --save-dev"
2520
end
26-
if options.install_cypress_examples
27-
directory 'spec/cypress/integration/examples', "#{options.cypress_folder}/integration/examples"
28-
directory 'spec/cypress/fixtures', "#{options.cypress_folder}/fixtures"
21+
if command
22+
say command
23+
fail 'failed to install cypress' unless system(command)
2924
end
30-
copy_file "spec/cypress/support/index.js", "#{options.cypress_folder}/support/index.js"
31-
copy_file "spec/cypress/support/commands.js", "#{options.cypress_folder}/support/commands.js"
32-
copy_file "spec/cypress.json", "#{options.cypress_folder}/../cypress.json"
3325
end
26+
if options.install_cypress_examples
27+
directory 'spec/cypress/integration/examples', "#{options.cypress_folder}/integration/examples"
28+
directory 'spec/cypress/fixtures', "#{options.cypress_folder}/fixtures"
29+
end
30+
template "spec/cypress/support/index.js.erb", "#{options.cypress_folder}/support/index.js"
31+
copy_file "spec/cypress/support/commands.js", "#{options.cypress_folder}/support/commands.js"
32+
copy_file "spec/cypress.json", "#{options.cypress_folder}/../cypress.json"
3433
end
3534

3635
def add_initial_files
3736
template "config/initializers/cypress_on_rails.rb.erb", "config/initializers/cypress_on_rails.rb"
38-
copy_file "spec/cypress/cypress_helper.rb", "#{options.cypress_folder}/cypress_helper.rb"
37+
template "spec/cypress/cypress_helper.rb.erb", "#{options.cypress_folder}/cypress_helper.rb"
3938
copy_file "spec/cypress/support/on-rails.js", "#{options.cypress_folder}/support/on-rails.js"
4039
directory 'spec/cypress/app_commands', "#{options.cypress_folder}/app_commands"
4140
directory 'spec/cypress/integration/rails_examples', "#{options.cypress_folder}/integration/rails_examples"

0 commit comments

Comments
 (0)