Only loads spec files with .js extension #178

Open
diegocouto opened this Issue Nov 20, 2015 · 37 comments

Projects

None yet

9 participants

@diegocouto

I'm using jasmine-rails 0.12.2v with Sprockets and local Phantomjs. This is my jasmine.yml (I'm relying on assets pipeline):

# minimalist jasmine.yml configuration when leveraging asset pipeline
spec_files:
  - "**/*[Ss]pec.{js.jsx, js, jsx}"

use_phantom_gem: false

I've create a sample file (sample_spec.js.jsx) and when I try to run rake spec:javascripts it always throws:

ActionView::Template::Error: couldn't find file 'sample_spec.js.jsx' with type 'application/javascript'

On my environment, test will only run when using a .js extension for spec files.

@pironim
Contributor
pironim commented Nov 20, 2015

@diegocouto Can you post sample_spec.js.jsx content to inspect.

@diegocouto

Sure! This is the content:

describe('Hello from Jasmine', function(){
    it('passes', function(){
        expect(true).toBeTruthy();
    });
});
@pironim
Contributor
pironim commented Nov 20, 2015

@diegocouto It looks like regular js file without any jsx content.
As far as I know jsx is for React Components .. and it create js from pseudo html.
Do you want to have a predecessor or just create js file with different extension.. What is the goal ?

@diegocouto

Actually, I'm planning to use the tests to build React components, but as it wasn't working, I tried to remove as much variables as possible to find the issue, this is why this code is so simple and plain JS.

Thanks for your help! 😄

@pironim
Contributor
pironim commented Nov 20, 2015

@diegocouto Not a PRO but I think you still can use regular js extension for tests and sprocket can compile jsx components to regular js file for you.

@jipiboily

I am facing the same problem right now. With a .js.jsx file, I get this error: couldn't find file 'components/select2_spec.js.jsx' with type 'application/javascript'. If I use it just .js, I have an error as this is not valid JS, but JSX.

You can see for yourself with this sample test which gives the same error I am getting:

var TestUtils = React.addons.TestUtils

describe('CheckboxWithLabel', function () {

  it('changes the text after click', function () {
    // Render a checkbox with label in the document
    var checkbox = TestUtils.renderIntoDocument(
      <CheckboxWithLabel labelOn="On" labelOff="Off" />
    );

    var checkboxNode = ReactDOM.findDOMNode(checkbox);

    // Verify that it's Off by default
    expect(checkboxNode.textContent).toEqual('Off');

    // Simulate a click and verify that it is now On
    TestUtils.Simulate.change(
      TestUtils.findRenderedDOMComponentWithTag(
        checkbox,
        'input'
      )
    );
    expect(checkboxNode.textContent).toEqual('On');
  });

});

The problem is that

    var checkbox = TestUtils.renderIntoDocument(
      <CheckboxWithLabel labelOn="On" labelOff="Off" />
    );

is not valid JS, but it is indeed the way to write this.

Any hints on how to make jasmine-rails work with jsx files would be awesome. Until then, I'll continue looking and try to remember to report back here when I find the solution.

@jipiboily

I found out this blog post which has a working example... http://www.calebwoods.com/2015/11/01/testing-react-components-rails/

Example is https://github.com/calebwoods/react_testing but if you do a bundle update, it breaks. Not sure which gem upgrade breaks it, possibly related to some Sprocket changes, or something else. Continuing my investigation.

@jipiboily

This breaks the example: jipiboily/react_testing@1440a6e /cc @calebwoods (in case you have any idea)....so it looks like this is not due to jasmine-rails at all at first.

@jipiboily

And it's not babel-source'fault btw (calebwoods/react_testing@master...jipiboily:figure-out-wtf-breaks-it), just tested to rule it out. I guess we can close this and open an issue on react-rails now? Or at least, for my case.

Cheers

@jipiboily

I've created this issue: reactjs/react-rails#431

@jipiboily

Forgot to mention it here, but downgrading to react-rails 1.4.1 fixed it for me.

@jipiboily

This line is probably why it stopped working: reactjs/react-rails@v1.4.1...v1.4.2#diff-db79e062f0a40ceaca64834c6738ee2bR7. Maybe the problem is in jasmine-rails after all?

@mikem
mikem commented Jan 11, 2016

I've run into this as well and documented my findings in rails/sprockets#209.

I discovered that if the path passed to require_asset (see jasmine-specs.js.erb) is absolute, then the issue does not occur.

Interestingly, in JasmineRails.spec_files the spec paths are absolute. However there's a call to filter_files for each spec file which strips the root part from the front of the path, leaving a relative path.

Is there a reason this is necessary? I realize it's not a solution to this issue but a workaround.

@mikem mikem added a commit to mikem/jasmine-rails that referenced this issue Jan 11, 2016
@mikem mikem Skip stripping root dir for spec files cdc25ad
@mikem
mikem commented Jan 11, 2016

I implemented this workaround in my fork so I can move on until this is resolved.

@searls searls added a commit to searls/jasmine-rails-exploratory-tests that referenced this issue Jan 16, 2016
@searls Reproduces searls/jasmine-rails#178
Should see a failing spec
fb66226
@searls
Owner
searls commented Jan 16, 2016

I've posted a reproduction to my exploratory test repo:

https://github.com/searls/jasmine-rails-exploratory-tests/tree/jasmine-rails-issue-178

And I can see this behavior too.

@mikem -- I don't know why it stripped absolute paths, but forced to guess I'd say that when @wireframe originally added it in 002efe8 it may have been to ensure uniqueness between scripts amid differing '../..' patterns to prevent duplicate scripts to sneak in.

@searls
Owner
searls commented Jan 16, 2016

Scratch that, I had some gunked up tempfiles giving me a false positive. What I really see is apparently this:

screen shot 2016-01-16 at 9 22 15 am

Could someone kindly reproduce off that branch?

@searls
Owner
searls commented Jan 16, 2016

In any case, not stripping the absolute paths seems to fix the immediate issue (who knows what else it breaks). I'll send a PR to jasmine-rails to see how CI likes it

@searls
Owner
searls commented Jan 16, 2016

Opened here #181

@mikem
mikem commented Jan 17, 2016

@searls thanks for looking into this. The screenshot of the error you posted is exactly what I've been encountering.

For what it's worth, I don't believe the root cause to be in jasmine-rails. I'm still thinking the issue is in sprockets because loading dependencies with a relative path works fine when using sprockets directives but breaks with require_asset in ERB, like jasmine-specs.js.erb is doing.

Unfortunately rails/sprockets#209 hasn't gotten any attention yet.

@justin808

Please consider moving from the gem react-rails to the gem react_on_rails

https://github.com/shakacode/react_on_rails/

Webpack and modern JS tooling FTW!

@mikem
mikem commented Jan 25, 2016

@searls Another way to approach this is by always requesting a .js file. (See this comment on reactjs/react-rails#431.)

Here's a patch that might work:

diff --git a/lib/jasmine-rails.rb b/lib/jasmine-rails.rb
index 9a257f4..8cb3ede 100644
--- a/lib/jasmine-rails.rb
+++ b/lib/jasmine-rails.rb
@@ -138,6 +138,7 @@ module JasmineRails
       end
       files = files.flatten
       files = files.collect {|f| f.gsub(root_dir.to_s + '/', '') }
+      files = files.map { |f| f.gsub(/\.js\..*/, '.js') }
       files || []
     end

Maybe there's a better way to strip the suffix? This feels neater to me than not stripping root_dir.

@mikemerritt

Any updates on this?

@colinrcummings

I updated to react-rails 1.8.0 today in conjunction with the following and am no longer facing this issue:

  • jasmine-core (2.4.1)
  • jasmine-rails (0.12.2)
  • sprockets (3.4.0)
  • sprockets-rails (2.3.3)

Prior to that, I was holding at react-rails 1.4; this no longer seems necessary.

@searls
Owner
searls commented Jul 11, 2016

Well, that's heartening.

@jipiboily

Still getting the same error here, with latest react-rails (1.8.1), jasmine-rails (0.12.6), sprockets (3.7.0), sprockets-rails (3.1.1) and jasmine-core (2.4.1).

:(

Only difference I see, is that it gives me the list of paths it looks at. And it should be found, as the correct path is there. I would guess that the "application/javascript" part is the problem here. in couldn't find file 'components/comparison_type_spec.js.jsx' with type 'application/javascript'.

Thoughts?

@jipiboily

hum, it might not be related to jasmine-rails or react-rails...but Sprockets. When removing the jsx files from the spec files, it crashes...saying that bourbon is not present. I'm using the bourbon gem. It might be a deeper issue! Not sure...

@jipiboily

For my bourbon issue, I had to put the gem earlier in the Gemfile...which seems weird. Contiuing to look at this on my end.

@jipiboily

moving

//= require react
//= require react_ujs

at the top of my application.js removed the error. No react stuff was loaded before them, so not sure why it would work...

Now, I am not getting any error, which is a great step...but it also stopped running any .jsx specs, even with the proper config in the jasmine.yml.

@searls
Owner
searls commented Aug 5, 2016

Hi @jipiboily -- I probably can't help here without a minimal example project that reproduces the issue.

@jipiboily

I was not looking for help but sharing what I found so far. Hard to identify or reproduce and I'm on vacation. I was just lookong at that for fun on the morning :)

@jipiboily

Just spent a little time on this...and immediately saw this, and I wanted to share.

It didn't stop running just jsx files...it runs the specs in only one directory, and doesn't seem to care about my jasmine.yml saying I want to run only coffee files.

I have specs in 4 different directories: actions (just js files), components (jsx files), helpers (js files) and reducers (js files). Only specs from the helpers directoy are running.

@jipiboily jipiboily added a commit to jipiboily/jasmine-rails that referenced this issue Aug 9, 2016
@searls @jipiboily + jipiboily Resolves #178. Breaks ??? other things. b6977e5
@jipiboily

It looks like #181 is actually fixing this for me.

@jipiboily
jipiboily commented Aug 9, 2016 edited

Well, it works on localhost/specs...but not with bundle exec rake spec:javascript. To be continued...

Starting...

Finished
-----------------
0 specs, 0 failures in 0.005s.

ConsoleReporter finished

Should be 273 specs, 0 failures, 10 pending specs

@jipiboily
jipiboily commented Aug 16, 2016 edited

I spent a bunch of time, didn't figure it out...it doesn't work for me. I had to move away from jasmine-rails after spending hours and hours without being able to figure it out or repro in a different project.

For anyone in that situation..I had to move to https://github.com/jasmine/jasmine-gem but do an awful hack to make it work with react-rails...in an initializer:

module Jasmine
  class Configuration
    def js_files
      (
        map(@jasmine_files, :jasmine) +
        map(@boot_files, :boot) +
        map(@runner_boot_files, :runner_boot) +
        map(@src_files, :src) +
        map(@spec_files, :spec)
      ).map {|f| f.gsub('.js.jsx', '.js') }
    end
  end
end
@searls
Owner
searls commented Aug 16, 2016

Hey @jipiboily -- really sorry to hear about your frustration. I've been there too. The murky relationship between Rails Engines, initializers, Sprockets, and other assets-aware gems has caused no end of very-hard-to-reproduce issues.

@jipiboily

Yeah, been there before too. jasmine-rails served me well for at least a year, maybe more...so that's great!

Now that my new setup works, I'll leave it as-is. Next step in a few months will be to move everything out of the asset pipeline. At least it works now.

@calebwoods

I ended up tracking a similar issue to be caused by a Sprockets issuing react-rails reactjs/react-rails#587

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment