Skip to content

Commit 2653c36

Browse files
extra Logging on cypress fail (#9)
1 parent 795e654 commit 2653c36

File tree

11 files changed

+55
-15
lines changed

11 files changed

+55
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### Tasks
2+
* adding additional log failure logging
3+
14
## 1.1.1
25
### Fixed
36
* smart factory wrapper can handle when factory files get deleted

lib/generators/cypress_dev/templates/spec/cypress/app_commands/clean.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
logger.warn "add database_cleaner or update clean_db"
77
Post.delete_all if defined?(Post)
88
end
9+
10+
Rails.logger.info "APPCLEANED" # used by log_fail.rb
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This file is called when a cypress spec fails and allows for extra logging to be captured
2+
filename = command_options.fetch('runnable_full_title', 'no title').gsub(/[^[:print:]]/, '')
3+
4+
# grab last lines until "APPCLEANED" (Make sure in clean.rb to log the text "APPCLEANED")
5+
system "tail -n 10000 -r log/#{Rails.env}.log | sed \"/APPCLEANED/ q\" | sed 'x;1!H;$!d;x' > 'log/#{filename}.log'"
6+
7+
# create a json debug file for server debugging
8+
json_result = {}
9+
json_result['error'] = command_options.fetch('error_message', 'no error message')
10+
11+
if defined?(ActiveRecord::Base)
12+
json_result['records'] =
13+
ActiveRecord::Base.descendants.each_with_object({}) do |record_class, records|
14+
begin
15+
records[record_class.to_s] = record_class.limit(100).map(&:attributes)
16+
rescue
17+
end
18+
end
19+
end
20+
21+
filename = command_options.fetch('runnable_full_title', 'no title').gsub(/[^[:print:]]/, '')
22+
File.open("#{Rails.root}/log/#{filename}.json", "w+") do |file|
23+
file << JSON.pretty_generate(json_result)
24+
end

lib/generators/cypress_dev/templates/spec/cypress/integration/rails_examples/using_scenarios_spec.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ describe('Rails using scenarios examples', function() {
1515
})
1616
})
1717

18-
// uncomment these if you want to see what happens
19-
// it('example of missing scenario failure', function() {
20-
// cy.appScenario('missing')
21-
// })
22-
//
23-
// it('example of missing app failure', function() {
24-
// cy.app('run_me')
25-
// })
18+
19+
it('example of missing scenario failure', function() {
20+
cy.visit('/')
21+
cy.appScenario('basic')
22+
// cy.appScenario('missing') // uncomment these if you want to see what happens
23+
})
24+
25+
it('example of missing app failure', function() {
26+
cy.visit('/')
27+
cy.appScenario('basic')
28+
// cy.app('run_me') // uncomment these if you want to see what happens
29+
})
2630
})

lib/generators/cypress_dev/templates/spec/cypress/support/on-rails.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,17 @@ Cypress.Commands.add('appFixtures', function (options) {
3434
// The next is optional
3535
// beforeEach(() => {
3636
// cy.app('clean') // have a look at cypress/app_commands/clean.rb
37-
// });
37+
// });
38+
39+
// comment this out if you do not want to attempt to log additional info on test fail
40+
Cypress.on('fail', (err, runnable) => {
41+
// allow app to generate additional logging data
42+
Cypress.$.ajax({
43+
url: '/__cypress__/command',
44+
data: JSON.stringify({name: 'log_fail', options: {error_message: err.message, runnable_full_title: runnable.fullTitle() }}),
45+
async: false,
46+
method: 'POST'
47+
});
48+
49+
throw err;
50+
});

spec/integrations/rails_3_2/config/application.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,5 @@ class Application < Rails::Application
6464

6565
# Version of your assets, change this if you want to expire all your assets
6666
config.assets.version = '1.0'
67-
config.logger = Logger.new(STDOUT)
6867
end
6968
end

spec/integrations/rails_3_2/log/.keep

Whitespace-only changes.

spec/integrations/rails_4_2/config/application.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,5 @@ class Application < Rails::Application
2828
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
2929
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
3030
# config.i18n.default_locale = :de
31-
config.logger = Logger.new(STDOUT)
3231
end
3332
end

spec/integrations/rails_4_2/log/.keep

Whitespace-only changes.

spec/integrations/rails_5_2/config/application.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,5 @@ class Application < Rails::Application
2929

3030
# Don't generate system test files.
3131
config.generators.system_tests = nil
32-
33-
logger = ActiveSupport::Logger.new(STDOUT)
34-
logger.formatter = config.log_formatter
35-
config.logger = ActiveSupport::TaggedLogging.new(logger)
3632
end
3733
end

0 commit comments

Comments
 (0)