ActionView::Template::Error: 785: unexpected token at ''
Asked Answered
D

2

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
Diametrically answered 30/10, 2019 at 8:31 Comment(8)
I have encountered such before, there is actually nothing wrong with your code. you could remove that test section.Aghast
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?Weatherboard
@Weatherboard it's this line: <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">Diametrically
@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.Diametrically
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.Weatherboard
This error is strange for me: i cannot reproduce it on my system. sometimes it fails and other times it suceedsPeplos
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 webpackAllamerican
This behavior is issued at rails/webpacker and the issue is still open: github.com/rails/webpacker/issues/2860Goodill
M
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.

Matamoros answered 14/8, 2020 at 20:53 Comment(2)
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.Harden
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. 🤷‍♂️Matamoros
K
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

Krys answered 26/11, 2019 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.