Skip to content
This repository

Enable require.js mode. #34

Closed
wants to merge 2 commits into from

10 participants

Brendan Erwin Adam Williams Juliusz Gonera Rajan Agaskar Andrew de Andrade 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 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 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
Collaborator

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 August 27, 2011
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.

Andrew de Andrade

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

Showing 2 unique commits by 1 author.

May 12, 2011
Brendan Erwin 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
May 13, 2011
Brendan Erwin 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
This page is out of date. Refresh to see the latest.
14  lib/jasmine/config.rb
@@ -92,7 +92,9 @@ def root_path
92 92
 
93 93
     def js_files(spec_filter = nil)
94 94
       spec_files_to_include = spec_filter.nil? ? spec_files : match_files(spec_dir, [spec_filter])
95  
-      src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } + spec_files_to_include.collect {|f| File.join(spec_path, f) }
  95
+      files = []
  96
+      files = src_files.collect {|f| "/" + f } + helpers.collect {|f| File.join(spec_path, f) } unless require_js
  97
+      files + spec_files_to_include.collect {|f| File.join(spec_path, f) }
96 98
     end
97 99
 
98 100
     def css_files
@@ -111,6 +113,14 @@ def simple_config_file
111 113
       File.join(project_root, 'spec/javascripts/support/jasmine.yml')
112 114
     end
113 115
 
  116
+    def require_js
  117
+        if simple_config['require_js']
  118
+            simple_config['require_js']
  119
+        else
  120
+            nil
  121
+        end
  122
+    end
  123
+
114 124
     def src_dir
115 125
       if simple_config['src_dir']
116 126
         File.join(project_root, simple_config['src_dir'])
@@ -159,4 +169,4 @@ def stylesheets
159 169
       end
160 170
     end
161 171
   end
162  
-end
  172
+end
46  lib/jasmine/run.html.erb
@@ -11,31 +11,47 @@
11 11
   <% jasmine_files.each do |jasmine_file| %>
12 12
   <script src="<%= jasmine_file %>" type="text/javascript"></script>
13 13
   <% end %>
  14
+  <% require_js.each do |require_file| %>
  15
+  <script src="<%= require_file %>" type="text/javascript"></script>
  16
+  <% end %>
14 17
 
15 18
   <script type="text/javascript">
16 19
     var jsApiReporter;
17 20
     (function() {
18  
-      var jasmineEnv = jasmine.getEnv();
19  
-
20  
-      jsApiReporter = new jasmine.JsApiReporter();
21  
-      var trivialReporter = new jasmine.TrivialReporter();
22  
-
23  
-      jasmineEnv.addReporter(jsApiReporter);
24  
-      jasmineEnv.addReporter(trivialReporter);
  21
+        function startJasmine(){
  22
+          var jasmineEnv = jasmine.getEnv();
  23
+
  24
+          jsApiReporter = new jasmine.JsApiReporter();
  25
+          var trivialReporter = new jasmine.TrivialReporter();
  26
+
  27
+          jasmineEnv.addReporter(jsApiReporter);
  28
+          jasmineEnv.addReporter(trivialReporter);
  29
+
  30
+          jasmineEnv.specFilter = function(spec) {
  31
+            return trivialReporter.specFilter(spec);
  32
+          };
  33
+
  34
+          jasmineEnv.execute();
  35
+        }
  36
+
  37
+<% if require_js %>
  38
+        require(['<%= js_files.join("','") %>'],function(){
  39
+            require.ready(function(){
  40
+                startJasmine();
  41
+            })
  42
+        });
  43
+<% else %>
  44
+        window.onload = function() {
  45
+            startJasmine();
  46
+        };
  47
+<% end %>
25 48
 
26  
-      jasmineEnv.specFilter = function(spec) {
27  
-        return trivialReporter.specFilter(spec);
28  
-      };
29  
-
30  
-      window.onload = function() {
31  
-        jasmineEnv.execute();
32  
-      };
33 49
     })();
34 50
   </script>
35 51
 
36 52
   <% js_files.each do |js_file| %>
37 53
   <script src="<%= js_file %>" type="text/javascript"></script>
38  
-  <% end %>
  54
+  <% end unless require_js %>
39 55
 
40 56
 </head>
41 57
 <body>
1  lib/jasmine/server.rb
@@ -30,6 +30,7 @@ def run(focused_suite = nil)
30 30
       jasmine_files = @jasmine_files
31 31
       css_files = @jasmine_stylesheets + (@config.css_files || [])
32 32
       js_files = @config.js_files(focused_suite)
  33
+      require_js = @config.require_js
33 34
       body = ERB.new(File.read(File.join(File.dirname(__FILE__), "run.html.erb"))).result(binding)
34 35
       [
35 36
         200,
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.