Search code examples
ruby-on-railsphantomjsautomated-testspoltergeist

Rails/Poltergiest/PhantomJS not loading files in application.js during testing


I'm asking for help getting JS in .js script files to run under testing with rails.

If I put the code in a .js file and require it in the first line of application.js, then that script is not run. In development it executes the js! (though there is a syntax error or something). If I put the .js code into the .html.erb file in <script> tags, it works fine!

At time of writing, this is my app/assets/javascripts/application.js:

//  LIBRARIES
//= require this_webpage_is_toast
//= require jquery
//= require jquery_ujs
//= require jquery-ui/autocomplete
//= require bootstrap-sprockets
//= require angular
//= require angucomplete-alt
//  LOCAL FILES
//= require libfigure/util
//= require libfigure/move
//= require libfigure/chooser
//= require libfigure/param
//= require libfigure/figure
//= require libfigure/dance
//= require dances
//= require_tree .

Notice the first require, which is how I know JS doesn't run. That's because the file app/assets/javascripts/this_webpage_is_toast.js contains:

  document.body.innerHTML = "if you are reading this in a web page, then javascript is successfully executing from a file!"

It never executes that line.

It does /something/ with applications.js though, because if I write

//= require file_that_does_not_exist

it complains about a missing template.

It's doubtless some bone-headed misconfiguration error on my part.

Using phantomjs 2.1.1.0 Using poltergeist 1.10.0

Thoughts?

update: How are these .js files included anyway? It turns out generated html pages start out with:

"<!DOCTYPE html><html><head>\n  <title>Contra</title>\n  <link rel=\"stylesheet\" media=\"all\" href=\"/assets/application.css\">  <script src=\"/assets/application.js\"></script>...

Solution

  • I can think of two possible causes for this. The first would be a syntax error in a previous included JS file preventing the following files from being processed when they all get concatenated together. Since the file thats not working is the first file required it's probably not that. The second cause is that you have precompiled assets at some point, and have a manifest file in public/assets (probably something like public/assets/.sprockets-manifest-*.json) which can prevent the test environment from rebuilding and serving the latest version of the assets. Try deleting the file, or run rake assets:clobber