Webpacker: Can't find packs using Capybara/RSpec

Created on 6 Nov 2017  ·  29Comments  ·  Source: rails/webpacker

Ruby 2.3.1
Rails 5.1.3
Webpacker 3.0.2

I'm using Webkit as my JS driver. I can find the packs while in prod and dev but cannot get my React components to render while running tests.

I have this in my rails_helper.rb and it generates the correct files.
config.before(:suite) do # compile front-end Webpacker.compile end

Manifest file:
{ "application.js": "/assets/packs-test/application-1b884d2f94460a1a42f1.js" }

Output from save_and_open_page
<script src="/assets/packs-test/application-1b884d2f94460a1a42f1.js"></script>

My manifest file looks good as well and it pointing in the right files. Do I have to serve these files up with a different process to get them to render?

Most helpful comment

Thanks, everyone for keeping this thread updated. We have added an info task to show version info, thanks to @ytbryan :

bundle exec rails webpacker:info

Will make a new release today.

All 29 comments

What exactly is the error or issue? Can you share your webpacker.yml configuration? Is there not a file that compiles in public/assets/packs-test/application-1b884d2f94460a1a42f1.js?

Hi rossta,

The issue is that I can't access the pack files. The packs are in the correct place but it isn't being loaded ONLY in the test environment. Therefore, none of my components are being rendered.

webpacker.yml
```# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
source_path: app/javascript
source_entry_path: packs
public_output_path: assets/packs
cache_path: tmp/cache/webpacker

# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []

# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false

extensions:
- .jsx.erb
- .erb
- .js
- .jsx
- .ts
- .vue
- .sass
- .scss
- .css
- .png
- .svg
- .gif
- .jpeg
- .jpg

development:
<<: *default
compile: true

dev_server:
host: localhost
port: 3035
hmr: false
https: false

test:
<<: *default
compile: true

# Compile test packs to a separate directory
public_output_path: assets/packs-test

production:
<<: *default

# Production depends on precompilation of packs prior to booting for performance.
compile: false

# Cache manifest.json for performance
cache_manifest: true
```

Setup looks fine to me. Do you get any error (pack not found) or your react components aren't rendering?

BTW, you don't need this: config.before(:suite) do # compile front-end Webpacker.compile end, the on-demand compiler will take care of this in test environment.

As it turns out, Capybara is having issues with ES6 in my pack. I'll close this out because it isn't a Webpacker problem.

Thank you for the help!

BTW, you don't need this: config.before(:suite) do # compile front-end Webpacker.compile end, the on-demand compiler will take care of this in test environment.

Hi @gauravtiwari, I'm not finding that to be true. When running tests in our CI environment I'm seeing the following failure for all System tests:

Failure/Error: <%= javascript_pack_tag 'application' %>

ActionView::Template::Error:
 Webpacker can't find application.js in /home/circleci/app-name-here/public/packs-test/manifest.json. Possible causes:
 1. You want to set webpacker.yml value of compile to true for your environment
    unless you are using the `webpack -w` or the webpack-dev-server.
 2. Webpack has not yet re-run to reflect updates.
 3. You have misconfigured Webpacker's config/webpacker.yml file.
 4. Your Webpack configuration is not creating a manifest.
 Your manifest contains:
 {
 }

This is a vanilla config that I just generated via bin/rails webpacker:install. It works locally:

rm -rf public/packs-test
bin/rspec spec/system/*
# output omitted
ls public/packs-test
-> application-8529921d13676c5f97d4.js manifest.json

But on CI there is no public/packs-test directory. If I ssh into the build and bin/webpack && bin/rspec it works as expected, and there is then a public/packs-test directory with the manifest and compiled asset.

Any ideas?

@stevenharman maybe public/packs-test is being ignored by your .gitignore file since _webpacker_ ignores it by default.

@mitramejia Maybe... but why would that result in it not being generated on demand during a test run?

@stevenharman that's right, forget what I said.

Could you please try out master branch? Merged a PR that might have fixed this.

@gauravtiwari Nope, same error when running the latest master.

WELP... this turned out to be a problem with Rails + Bundler + RSpec, maybe? Specifically on CI everything worked as expected if ran bundle exec rspec. But bin/rspec didn't.

TL;DR: Rails' binstubs are different from those generated by Bundler, and in some cases incompatible?

Specifically:

  • After running bundle exec rspec both the public/packs-test/manifest.json and public/packs-test/application-*.js existed.
  • bin/rspec resulted in neither the manifest nor the compiled javascript being generated.

On a lark, I ran bundle binstubs rspec-core, which resulted in slightly updated bin/rspec; it added the following

bundle_binstub = File.expand_path("../bundle", __FILE__)
load(bundle_binstub) if File.file?(bundle_binstub)

Which then broken b/c my bin/bundle had been generated by Rails, and was incompatible with the Bundler generated binstubs. So, I bundle binstubs bundler, which generated a new bin/bundle. After that, bin/rspec worked just like bundle exec rspec.

This feels like a hot mess. Just me? ¯_(ツ)_/¯

@stevenharman can you paste a gist with the old content of your old rspec and bundle binstubs and the new versions? Did you generate old rspec binstub with an old version of bundler?

I think the problem that you might be experienced can be related to changes in the last version of Bundler (1.16).

@guilleiguaran Sure thing. Rails' binstubs vs. Bundler binstubs: https://gist.github.com/stevenharman/8fca435526917a70ec57ec457ecd55c9

@guilleiguaran Does that help? Can you provide any further insight into the changes, and how/if Rails will be changed to work with the change? Also, is there an issue at rails/rails I can track?

@stevenharman thank you very much, that info is very useful.

We don't have any issue in rails/rails yet but I think we must have one instead of continuing the discussion in here, can you post/copy the info that you've found in a new issue under Rails repo?

Aaand this error is back with Bundler v 1.16.1. I've not diagnosed it yet, but so long as I've got a bundler-generated bin/bundle binstub (as was the recommended workaround in https://github.com/rails/rails/issues/31193#issuecomment-347784561) and use Bundler 1.16.1 on CI this same error happens. That is, the manifests aren't built.

If I revert CI to Bundler 1.16.0, everything works again! 🤷‍♀️.

__UPDATE__: When using Bundler 1.16.1, bundle exec rspec also fails to generate the manifests. This is different than what we saw before. I suspect this is a different breakage than the original binstub issue. But I've not had time to look into it to confirm.

@stevenharman It's not bundler problem. Rubygems 2.7.3 and Travis environment is. I try to fix it and prepare to release 2.7.4 https://github.com/rubygems/rubygems/pull/2124

rails/rails with Rubygems 2.6.x and Bundler 1.16.1 is completely working. https://travis-ci.org/rails/rails/builds/320235027

@hsbt To clarify, it's a Rubygems 2.7.3 issue then, and doesn't really have anything to do with TravisCI, CircleCI, etc... (except they might happen to have Rubygems 2.7.3 installed). And the ultimate fix will be to upgrade all environments to Rubygems 2.7.4. Yes?

Maybe yes(Because I didn't use Circle CI). It happens with this scenario: https://github.com/rubygems/rubygems/pull/2124#issuecomment-353595931 and deeply related Travis and RVM environment.

I pointed only "I revert CI to Bundler 1.16.0, everything works again!"

the binstub issue did not resolve yet in Bundler-1.16.1. It only shows warning message.

I solved this issue in Rails 5.2 rc1 by setting this variable in test.rb:

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true

Perhaps we can document this in README.

I have the same problem locally after deleting the public/packs-test folder.

I use

➜ gem --version
2.7.6

➜ bundle --version
Bundler version 1.16.1

➜ rails -v
Rails 5.1.5

➜ bin/webpack
/Users/holger/.rbenv/versions/2.4.3/lib/ruby/site_ruby/2.4.0/bundler/rubygems_integration.rb:458:in `block in replace_bin_path': can't find executable webpack for gem webpacker (Gem::Exception)
    from /Users/holger/.rbenv/versions/2.4.3/lib/ruby/site_ruby/2.4.0/bundler/rubygems_integration.rb:489:in `block in replace_bin_path'
    from bin/webpack:17:in `<main>'

Error in test

    # Webpacker::Manifest::MissingEntryError:
     #   Webpacker can't find app.js in /Users/holger/projects/clients/edeka/edk-old/public/packs-test/manifest.json. Possible causes:
     #   1. You want to set webpacker.yml value of compile to true for your environment
     #      unless you are using the `webpack -w` or the webpack-dev-server.
     #   2. webpack has not yet re-run to reflect updates.
     #   3. You have misconfigured Webpacker's config/webpacker.yml file.
     #   4. Your webpack configuration is not creating a manifest.
     #   Your manifest contains:
     #   {
     #   }

I added config.public_file_server.enabled = true in config/environments/test.rb but it did not change a thing.
How could I proceed here?

edit
Also happens with Rails Version 5.2.0.rc1 from branch 5-2-stable.

edit 2
Perhaps this is old news, but if I downgrade webpacker to 3.2.2 it works again. It did not work with version 3.3.1 or 3.3.0.

edit 3 OR RTFM
=> https://github.com/rails/webpacker/blob/master/CHANGELOG.md#breaking-changes
bundle exec rails webpacker:binstubs

Now everything works for me again. Sorry again if this was old news for you.

I can confirm I'm having a similar problem with my tests as @5minpause

I added the prescribed line to config/environments/test.rb and I'm still receiving the error message.

webpacker 3.4.3 (gem version)
rails 5.2.0

however my package.json had a pessimistic lock in the dependency, so I relaxed that constraint and ran yarn upgrade @rails/webpacker

So a good troubleshooting step would be to verify that your webpacker gem version and npm package match. Hope that's helpful

Thanks, everyone for keeping this thread updated. We have added an info task to show version info, thanks to @ytbryan :

bundle exec rails webpacker:info

Will make a new release today.

I had the same error mentioned in https://github.com/rails/webpacker/issues/998#issuecomment-375597197 but it was because I needed to add the following to Codeship setup commands:

nvm install 8.4.0
yarn install

I solved this issue in Rails 5.2 rc1 by setting this variable in test.rb:

  # Configure public file server for tests with Cache-Control for performance.
  config.public_file_server.enabled = true

this instantly fixed my issue with React components included via react-rails gem not displaying in RSpec tests (js: true) using Capybara/Selenium.

None of the above fixes worked for me. In my case, it was caused by a lazy-load nvm setup http://broken-by.me/lazy-load-nvm/ If I run "bundle exec rspec" in a new terminal, the error occurs. If I run "yarn" and then "bundle exec rspec", the error goes away.

I'm not surprised it broke, since this lazy-load nvm setup tends to break most anything that uses node and which I don't execute myself. But, what DOES surprise me: it seems to fail silently. I got no error to the extent of "node command couldn't be found".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

suhomozgy-andrey picture suhomozgy-andrey  ·  3Comments

vtno picture vtno  ·  3Comments

iChip picture iChip  ·  3Comments

johan-smits picture johan-smits  ·  3Comments

pioz picture pioz  ·  3Comments