Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Enable require.js mode. #34

Closed
wants to merge 2 commits into from

10 participants

Brendan Erwin Adam Williams Juliusz Gonera Rajan Agaskar malandrew Nikita Fedyashev Julian Maicher tatemae flip Davis W. Frank
Brendan Erwin

If the yml file contains a require_js element with the url fragment to require.js, then the server will switch to CommonJS amd module mode.

It will no longer directly load any source files, but will expect that the specs are declared as modules taking dependencies on the appropriate source files.

Anyone interested in playing with this, I have a gem you can try out:

https://github.com/downloads/brendanjerwin/jasmine-gem/jasmine-1.0.3.0pre.gem

Brendan Erwin brendanjerwin Enable require.js mode.
If the yml file contains a `require_js` element with the url fragment to require.js, then the server will switch to CommonJS amd module mode.

It will no longer directly load any source files, but will expect that the specs are declared as modules taking dependencies on the appropriate source files.
eaa4e58
Brendan Erwin

I did not add an example section in the yml file. I think I saw more than one and wasn't sure which to modify.

Brendan Erwin

Be sure to add a single item to the yml file, not a list:

require_js: lib/require.js

not

require_js: 
    - lib/require.js
Adam Williams

Unless I'm mistaken, it looks like this will have the helpers loading before the src files that are required. That's not how it worked before, with the src files before the helpers. Helpers may use code from the src files.

Brendan Erwin

True enough. I should remove the loading of helpers all together and let require.js handle the loading. Then any dependencies /load order issues can be taken care of correctly.

Brendan Erwin brendanjerwin Ignores helpers if in require.js mode.
Modified the erb to expect that the require.js configuration be an array. Now any require.js configuration can be done prior to using require.js.
2566c4b
Brendan Erwin

There, now it ignores helpers. It works fine to simply require the helpers needed, and if they need access to the code under test, they can just require those modules.

Also, now the require_js configuration option is expected to be an array. If something must load before the specs, it can be put there.

Juliusz Gonera

Will this get merged to the official gem at some point?

Brendan Erwin

Probably not as is. This works for us but I think it's best thought of as a proof if concept.

Rajan Agaskar
Owner

Yeah, I feel like it's best for us to put off addressing this issue until the next release, where we can take on all the new JS challenges that Rails 3.1 and other frameworks are now presenting. Apologies for making it difficult for those of you that use require_js, but we want to get 1.1 (mostly bugfixes and a minor gem refactor) out within the week. We'll probably have a substantial refactoring of Jasmine coming soon, as it's getting pretty creaky.

Rajan Agaskar ragaskar closed this
Brendan Erwin

Another thing to keep in mind when enabling require_js support for real: https://github.com/JamesMaroney/requirejs-isolate

This plugin helps to isolate the code under test.

malandrew

Now that Jasmine is at 1.1, can we get this issue reopened? jwhitley's requirejs-rails gem is now at 0.5.1 and one of the items slated for the 1.0 build is jasmine support. From what I've grokked so far, the only way to get require.js support working in his gem would be to get support for require.js into the jasmine gem.

https://github.com/jwhitley/requirejs-rails/

Nikita Fedyashev

@malandrew
+1 for reopening

Meanwhile:

https://github.com/millermedeiros/amd-utils/tree/master/tests perhaps this may help someone

Julian Maicher

I'd love to see some official support, too!
Meanwhile this could also help: https://github.com/scottburch/jasmine-require

tatemae

+1 for require.js support. We're in the process of trying to hack a solution together, but it would be nice to have official support.

flip

+1 for reopening

Davis W. Frank
Owner

We still plan on better async loading support in a future release. In the meantime: https://github.com/mattfysh/testr.js is your best bet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 13, 2011
  1. Brendan Erwin

    Enable require.js mode.

    brendanjerwin authored
    If the yml file contains a `require_js` element with the url fragment to require.js, then the server will switch to CommonJS amd module mode.
    
    It will no longer directly load any source files, but will expect that the specs are declared as modules taking dependencies on the appropriate source files.
Commits on May 14, 2011
  1. Brendan Erwin

    Ignores helpers if in require.js mode.

    brendanjerwin authored
    Modified the erb to expect that the require.js configuration be an array. Now any require.js configuration can be done prior to using require.js.
This page is out of date. Refresh to see the latest.
14 lib/jasmine/config.rb
View
@@ -92,7 +92,9 @@ def root_path
def js_files(spec_filter = nil)
spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter])
- src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } + spec_files_to_include.collect {|f| File.join(spec_path, f) }
+ files = []
+ files = src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } unless require_js
+ files + spec_files_to_include.collect {|f| File.join(spec_path, f) }
end
def css_files
@@ -111,6 +113,14 @@ def simple_config_file
File.join(project_root, 'spec/javascripts/support/jasmine.yml')
end
+ def require_js
+ if simple_config['require_js']
+ simple_config['require_js']
+ else
+ nil
+ end
+ end
+
def src_dir
if simple_config['src_dir']
File.join(project_root, simple_config['src_dir'])
@@ -159,4 +169,4 @@ def stylesheets
end
end
end
-end
+end
46 lib/jasmine/run.html.erb
View
@@ -11,31 +11,47 @@
<% jasmine_files.each do |jasmine_file| %>
<script src="<%= jasmine_file %>" type="text/javascript"></script>
<% end %>
+ <% require_js.each do |require_file| %>
+ <script src="<%= require_file %>" type="text/javascript"></script>
+ <% end %>
<script type="text/javascript">
var jsApiReporter;
(function() {
- var jasmineEnv = jasmine.getEnv();
-
- jsApiReporter = new jasmine.JsApiReporter();
- var trivialReporter = new jasmine.TrivialReporter();
-
- jasmineEnv.addReporter(jsApiReporter);
- jasmineEnv.addReporter(trivialReporter);
+ function startJasmine(){
+ var jasmineEnv = jasmine.getEnv();
+
+ jsApiReporter = new jasmine.JsApiReporter();
+ var trivialReporter = new jasmine.TrivialReporter();
+
+ jasmineEnv.addReporter(jsApiReporter);
+ jasmineEnv.addReporter(trivialReporter);
+
+ jasmineEnv.specFilter = function(spec) {
+ return trivialReporter.specFilter(spec);
+ };
+
+ jasmineEnv.execute();
+ }
+
+<% if require_js %>
+ require(['<%= js_files.join("','") %>'],function(){
+ require.ready(function(){
+ startJasmine();
+ })
+ });
+<% else %>
+ window.onload = function() {
+ startJasmine();
+ };
+<% end %>
- jasmineEnv.specFilter = function(spec) {
- return trivialReporter.specFilter(spec);
- };
-
- window.onload = function() {
- jasmineEnv.execute();
- };
})();
</script>
<% js_files.each do |js_file| %>
<script src="<%= js_file %>" type="text/javascript"></script>
- <% end %>
+ <% end unless require_js %>
</head>
<body>
1  lib/jasmine/server.rb
View
@@ -30,6 +30,7 @@ def run(focused_suite = nil)
jasmine_files = @jasmine_files
css_files = @jasmine_stylesheets + (@config.css_files || [])
js_files = @config.js_files(focused_suite)
+ require_js = @config.require_js
body = ERB.new(File.read(File.join(File.dirname(__FILE__), "run.html.erb"))).result(binding)
[
200,
Something went wrong with that request. Please try again.