Open diegocouto opened 8 years ago
@diegocouto Can you post sample_spec.js.jsx content to inspect.
Sure! This is the content:
describe('Hello from Jasmine', function(){
it('passes', function(){
expect(true).toBeTruthy();
});
});
@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 ?
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! :smile:
@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.
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.
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.
This breaks the example: https://github.com/jipiboily/react_testing/commit/1440a6ed400de7c9a2f6b3b4526ff576f0e1b3df /cc @calebwoods (in case you have any idea)....so it looks like this is not due to jasmine-rails at all at first.
And it's not babel-source
'fault btw (https://github.com/calebwoods/react_testing/compare/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
I've created this issue: https://github.com/reactjs/react-rails/issues/431
Forgot to mention it here, but downgrading to react-rails
1.4.1 fixed it for me.
This line is probably why it stopped working: https://github.com/reactjs/react-rails/compare/v1.4.1...v1.4.2#diff-db79e062f0a40ceaca64834c6738ee2bR7. Maybe the problem is in jasmine-rails
after all?
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.
I implemented this workaround in my fork so I can move on until this is resolved.
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 002efe803d214ce6627af627ac602fded9f5703a it may have been to ensure uniqueness between scripts amid differing '../..' patterns to prevent duplicate scripts to sneak in.
Scratch that, I had some gunked up tempfiles giving me a false positive. What I really see is apparently this:
Could someone kindly reproduce off that branch?
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
Opened here https://github.com/searls/jasmine-rails/pull/181
@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.
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!
@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
.
Any updates on this?
I updated to react-rails 1.8.0 today in conjunction with the following and am no longer facing this issue:
Prior to that, I was holding at react-rails 1.4; this no longer seems necessary.
Well, that's heartening.
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?
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...
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.
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.
Hi @jipiboily -- I probably can't help here without a minimal example project that reproduces the issue.
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 :)
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.
It looks like https://github.com/searls/jasmine-rails/pull/181 is actually fixing this for me.
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
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
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.
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.
I ended up tracking a similar issue to be caused by a Sprockets issuing react-rails https://github.com/reactjs/react-rails/issues/587
I'm using jasmine-rails 0.12.2v with Sprockets and local Phantomjs. This is my jasmine.yml (I'm relying on assets pipeline):
I've create a sample file (sample_spec.js.jsx) and when I try to run rake spec:javascripts it always throws:
On my environment, test will only run when using a .js extension for spec files.