12

This error happens randomly during testing (standard Rails testing toolkit) after running rails test. Failed test can be any test in any file.

This error happens usually in testing environment during CI/CD process. Locally it occurs very rarely.

Here is a full stack trace:

Error:
SitePositionsControllerTest#test_should_create_position_link:
ActionView::Template::Error: 785: unexpected token at ''
    app/views/layouts/site/application.html.erb:10
    test/controllers/site_positions_controller_test.rb:28:in `block (2 levels) in <class:SitePositionsControllerTest>'
    test/controllers/site_positions_controller_test.rb:27:in `block in <class:SitePositionsControllerTest>'
    test/test_helper.rb:64:in `block (3 levels) in run'
    test/test_helper.rb:63:in `block (2 levels) in run'
    test/test_helper.rb:62:in `block in run'
    test/test_helper.rb:54:in `run'

rails test test/controllers/site_positions_controller_test.rb:18
Todd
  • 3,438
  • 1
  • 27
  • 36
Dmitry Matveyev
  • 307
  • 1
  • 10
  • I have encountered such before, there is actually nothing wrong with your code. you could remove that test section. – Nsikan Sylvester Oct 30 '19 at 10:44
  • Most of the times I got that error it was related to json parsing or malformed javascript. What's on your application layout at line 10? – arieljuod Oct 30 '19 at 13:12
  • @arieljuod it's this line: `` – Dmitry Matveyev Oct 31 '19 at 09:20
  • @NsikanSylvester removing test section is not going to help, it occurs randomly for any test. My problem is that it fails CI/CD process so I have to restart it. – Dmitry Matveyev Oct 31 '19 at 09:25
  • I guess the error is pointing to the wrong line. I would try removing most of the code from the template and start adding it back again until I found what's triggering that. I guess it's some javascript error or json parsing error. – arieljuod Oct 31 '19 at 12:50
  • This error is strange for me: i cannot reproduce it on my system. sometimes it fails and other times it suceeds – BenKoshy Apr 23 '20 at 03:43
  • 4
    I'm also getting this randomly in CI/CD. That line `app/views/layouts/site/application.html.erb:10` generally points to the `javascript_pack_tag` statement, which leads me to believe that there's some sort of race condition between the test and webpack – danini Jun 08 '20 at 05:52
  • 3
    This behavior is issued at rails/webpacker and the issue is still open: https://github.com/rails/webpacker/issues/2860 – nekketsuuu Aug 14 '21 at 05:15

2 Answers2

2

Like @jellymann I was having this issue on calls to javascript_pack_tag.

I added bin/rails webpacker:compile before rails test and it seems to have resolved the issue.

This was happening pretty regularly. In the two days since I made that change, at least a dozen or so builds have gone off without this error.

Todd
  • 3,438
  • 1
  • 27
  • 36
  • Also seeing the same issue in CI. I already have `rake webpacker:compile` in the workflow before tests are run. Seeing failure only on some runs. While I think the compile is finished before tests start, @jellymann is right that the occasional failure feels like some sort of race condition between those actions. – RedBassett Dec 30 '20 at 03:35
  • Yeah. That sounds like a race condition to me too. FWIW, to others, I have not see this again since I made the above change. ‍♂️ – Todd Jan 05 '21 at 16:22
0

I had the same problem when I used with webmock and VCR.

Solved adding this initializer (some hints)

# config/initializers/webmock.rb
if Rails.env.test?
  require 'webmock'
  WebMock.disable_net_connect!(allow_localhost: true)
end

duleorlovic
  • 432
  • 4
  • 15