Skip to content

Add a test helper #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,25 @@ To use Webpacker with [Elm](http://elm-lang.org), create a new app with `rails n
The Elm library and core packages will be added via Yarn and Elm itself. An example `Main.elm` app
is also added to your project in `app/javascript` so that you can experiment with Elm right away.

## Testing

Webpacker provides a `Webpacker::TestHelper`, which includes a setup to
pre-compile webpacker assets when running tests.

```rb
# Test the example react component message
require "webpacker/test_helper"
require "application_system_test_case"

class HomesTest < ApplicationSystemTestCase
include Webpacker::TestHelper
test "can see the hello message" do
visit root_url
assert_selector "h5", text: "Hello! David"
end
end
```

## Troubleshooting

* If you get this error `ENOENT: no such file or directory - node-sass` on Heroku
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/installers.rake
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
INSTALLERS = {
installers = {
"Angular": :angular,
"Elm": :elm,
"React": :react,
Expand All @@ -7,7 +7,7 @@ INSTALLERS = {

namespace :webpacker do
namespace :install do
INSTALLERS.each do |name, task_name|
installers.each do |name, task_name|
desc "Install everything needed for #{name}"
task task_name => ["webpacker:verify_install"] do
template = File.expand_path("../install/#{task_name}.rb", __dir__)
Expand Down
7 changes: 0 additions & 7 deletions lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
require "webpacker/env"
require "webpacker/configuration"
REGEX_MAP = /\A.*\.map\z/

namespace :webpacker do
desc "Compile javascript packs using webpack for production with digests"
Expand All @@ -25,12 +24,6 @@ namespace :webpacker do
end
end

# Compile packs prior to system and controller tests running
if Rake::Task.task_defined?("test:system")
Rake::Task["test:system"].enhance(["webpacker:compile_before_test"])
Rake::Task["test:controllers"].enhance(["webpacker:compile_before_test"])
end

# Compile packs after we've compiled all other assets during precompilation
if Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance do
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/webpacker/install.rake
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
WEBPACKER_APP_TEMPLATE_PATH = File.expand_path("../../install/template.rb", __dir__)
install_template_path = File.expand_path("../../install/template.rb", __dir__)

namespace :webpacker do
desc "Install webpacker in this application"
task install: [:check_node, :check_yarn] do
if Rails::VERSION::MAJOR >= 5
exec "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
exec "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{install_template_path}"
else
exec "#{RbConfig.ruby} ./bin/rake rails:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
exec "#{RbConfig.ruby} ./bin/rake rails:template LOCATION=#{install_template_path}"
end
end
end
25 changes: 25 additions & 0 deletions lib/webpacker/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "rake"
require "webpacker"

module Webpacker::TestHelper
extend ActiveSupport::Concern

included do
setup :compile_webpack_assets
end

def compile_webpack_assets
Rails.cache.fetch(["webpacker", "manifest", checksum]) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cache key will only be invalidated when your files change, right? What if, for example, you bump the version of a dependency in package.json? I think it'd be best to avoid caching unless we can use the same digests provided by webpack.

Copy link
Member Author

@gauravtiwari gauravtiwari May 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javan Yepp that's right. Good point 👍 Perhaps we should watch package.json and yarn.lock for changes too? The compile task slows down the tests so it's best to cache because we won't be able to get new digests from webpack until it's compiled (which is same thing as re-compiling) unless I am missing something??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added yarn.lock and package.json to watch list

@load_rakefile ||= Rake.load_rakefile(Rails.root.join("Rakefile"))
Rake::Task["webpacker:compile"].invoke
Rake::Task["webpacker:compile"].reenable
end
end

private

def checksum
files = Dir["#{Webpacker::Configuration.source}/**/*", "package.json", "yarn.lock"].reject { |f| File.directory?(f) }
files.map { |f| File.mtime(f).utc.to_i }.max.to_s
end
end