jasmine 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,60 @@
1
+ module Jasmine
2
+ class RunnerConfig
3
+ def initialize(config = Jasmine::Config.new)
4
+ @config = config
5
+ end
6
+
7
+ def css_files
8
+ @config.jasmine_stylesheets + @config.user_stylesheets
9
+ end
10
+
11
+ def jasmine_files
12
+ @config.jasmine_javascripts
13
+ end
14
+
15
+ def js_files
16
+ @config.js_files
17
+ end
18
+
19
+ def spec_files
20
+ @config.spec_files
21
+ end
22
+
23
+ def spec_files_full_paths
24
+ @config.spec_files_full_paths
25
+ end
26
+
27
+ def spec_path
28
+ @config.spec_path
29
+ end
30
+
31
+ def spec_dir
32
+ @config.spec_dir
33
+ end
34
+
35
+ def src_dir
36
+ @config.src_dir
37
+ end
38
+
39
+ def project_root
40
+ @config.project_root
41
+ end
42
+
43
+ def root_path
44
+ @config.root_path
45
+ end
46
+
47
+ def browser
48
+ ENV["JASMINE_BROWSER"] || 'firefox'
49
+ end
50
+
51
+ def jasmine_host
52
+ ENV["JASMINE_HOST"] || 'http://localhost'
53
+ end
54
+
55
+ def port
56
+ @port ||= ENV["JASMINE_PORT"] || Jasmine.find_unused_port
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,70 @@
1
+ module Jasmine
2
+ module Runners
3
+ class HTTP
4
+ attr_accessor :suites
5
+
6
+ def initialize(client, results_processor)
7
+ @client = client
8
+ @results_processor = results_processor
9
+ end
10
+
11
+ def run
12
+ @client.connect
13
+ load_suite_info
14
+ wait_for_suites_to_finish_running
15
+ results = @results_processor.process(results_hash, suites)
16
+ @client.disconnect
17
+ results
18
+ end
19
+
20
+ private
21
+
22
+ def load_suite_info
23
+ started = Time.now
24
+ while !eval_js('return jsApiReporter && jsApiReporter.started') do
25
+ raise "couldn't connect to Jasmine after 60 seconds" if (started + 60 < Time.now)
26
+ sleep 0.1
27
+ end
28
+
29
+ @suites = eval_js("var result = jsApiReporter.suites(); if (window.Prototype && Object.toJSON) { return Object.toJSON(result) } else { return JSON.stringify(result) }")
30
+ end
31
+
32
+ def results_hash
33
+ spec_results = {}
34
+ spec_ids.each_slice(50) do |slice|
35
+ spec_results.merge!(eval_js("var result = jsApiReporter.resultsForSpecs(#{json_generate(slice)}); if (window.Prototype && Object.toJSON) { return Object.toJSON(result) } else { return JSON.stringify(result) }"))
36
+ end
37
+ spec_results
38
+ end
39
+
40
+ def spec_ids
41
+ map_spec_ids = lambda do |suites|
42
+ suites.map do |suite_or_spec|
43
+ if suite_or_spec['type'] == 'spec'
44
+ suite_or_spec['id']
45
+ else
46
+ map_spec_ids.call(suite_or_spec['children'])
47
+ end
48
+ end
49
+ end
50
+ map_spec_ids.call(@suites).compact.flatten
51
+ end
52
+
53
+ def wait_for_suites_to_finish_running
54
+ puts "Waiting for suite to finish in browser ..."
55
+ while !eval_js('return jsApiReporter.finished') do
56
+ sleep 0.1
57
+ end
58
+ end
59
+
60
+ def eval_js(script)
61
+ @client.eval_js(script)
62
+ end
63
+
64
+ def json_generate(obj)
65
+ @client.json_generate(obj)
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -21,10 +21,6 @@ module Jasmine
21
21
  @http_address = http_address
22
22
  end
23
23
 
24
- def tests_have_finished?
25
- @driver.execute_script("return window.jasmine.getEnv().currentRunner.finished") == "true"
26
- end
27
-
28
24
  def connect
29
25
  @driver.navigate.to @http_address
30
26
  end
@@ -33,16 +29,6 @@ module Jasmine
33
29
  @driver.quit
34
30
  end
35
31
 
36
- def run
37
- until tests_have_finished? do
38
- sleep 0.1
39
- end
40
-
41
- puts @driver.execute_script("return window.results()")
42
- failed_count = @driver.execute_script("return window.jasmine.getEnv().currentRunner.results().failedCount").to_i
43
- failed_count == 0
44
- end
45
-
46
32
  def eval_js(script)
47
33
  result = @driver.execute_script(script)
48
34
  JSON.parse("{\"result\":#{result}}", :max_nesting => false)["result"]
@@ -1,40 +1,19 @@
1
- require 'rack'
2
- require 'rack/utils'
3
- require 'jasmine-core'
4
- require 'rack/jasmine/runner'
5
- require 'rack/jasmine/focused_suite'
6
- require 'rack/jasmine/redirect'
7
- require 'rack/jasmine/cache_control'
8
- require 'ostruct'
9
-
10
1
  module Jasmine
11
- def self.app(config)
12
- jasmine_stylesheets = ::Jasmine::Core.css_files.map {|f| "/__JASMINE_ROOT__/#{f}"}
13
- config_shim = OpenStruct.new({:jasmine_files => ::Jasmine::Core.js_files.map {|f| "/__JASMINE_ROOT__/#{f}"},
14
- :js_files => config.js_files,
15
- :css_files => jasmine_stylesheets + (config.css_files || [])})
16
- page = Jasmine::Page.new(config_shim.instance_eval { binding })
17
- Rack::Builder.app do
18
- use Rack::Head
19
- use Rack::Jasmine::CacheControl
20
- if Jasmine::Dependencies.rails_3_asset_pipeline?
21
- map('/assets') do
22
- run Rails.application.assets
23
- end
24
- end
25
-
26
- map('/run.html') { run Rack::Jasmine::Redirect.new('/') }
27
- map('/__suite__') { run Rack::Jasmine::FocusedSuite.new(config) }
28
-
29
- map('/__JASMINE_ROOT__') { run Rack::File.new(Jasmine::Core.path) }
30
- map(config.spec_path) { run Rack::File.new(config.spec_dir) }
31
- map(config.root_path) { run Rack::File.new(config.project_root) }
2
+ class Server
3
+ def initialize(port = 8888, application = Jasmine::Application.app)
4
+ @port = port
5
+ @application = application
6
+ end
32
7
 
33
- map('/') do
34
- run Rack::Cascade.new([
35
- Rack::URLMap.new('/' => Rack::File.new(config.src_dir)),
36
- Rack::Jasmine::Runner.new(page)
37
- ])
8
+ def start
9
+ if Jasmine::Dependencies.legacy_rack?
10
+ handler = Rack::Handler.get('webrick')
11
+ handler.run(@application, :Port => @port, :AccessLog => [])
12
+ else
13
+ server = Rack::Server.new(:Port => @port, :AccessLog => [])
14
+ # workaround for Rack bug, when Rack > 1.2.1 is released Rack::Server.start(:app => Jasmine.app(self)) will work
15
+ server.instance_variable_set(:@app, @application)
16
+ server.start
38
17
  end
39
18
  end
40
19
  end
@@ -27,7 +27,7 @@ namespace :jasmine do
27
27
  t.rspec_opts = ["--colour", "--format", ENV['JASMINE_SPEC_FORMAT'] || "progress"]
28
28
  t.verbose = true
29
29
  if Jasmine::Dependencies.rails_3_asset_pipeline?
30
- t.ruby_opts = ["-r #{File.expand_path(File.join(::Rails.root, 'config', 'environment'))}"]
30
+ t.rspec_opts += ["-r #{File.expand_path(File.join(::Rails.root, 'config', 'environment'))}"]
31
31
  end
32
32
  t.pattern = [Jasmine.runner_filepath]
33
33
  end
@@ -48,7 +48,7 @@ namespace :jasmine do
48
48
  port = ENV['JASMINE_PORT'] || 8888
49
49
  puts "your tests are here:"
50
50
  puts " http://localhost:#{port}/"
51
- Jasmine::Config.new.start_server(port)
51
+ Jasmine::Server.new(port).start
52
52
  end
53
53
  end
54
54
 
@@ -1,3 +1,3 @@
1
1
  module Jasmine
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -2,12 +2,12 @@ module Rack
2
2
  module Jasmine
3
3
 
4
4
  class FocusedSuite
5
- def initialize(config)
6
- @config = config
5
+ def initialize(runner_config)
6
+ @runner_config = runner_config
7
7
  end
8
8
 
9
9
  def call(env)
10
- run_adapter = Rack::Jasmine::RunAdapter.new(@config)
10
+ run_adapter = Rack::Jasmine::RunAdapter.new(@runner_config)
11
11
  run_adapter.run(env["PATH_INFO"])
12
12
  end
13
13
  end
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+ require 'rack/test'
3
+
4
+ describe "Jasmine::Application" do
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ @root = File.join(File.dirname(__FILE__))
9
+ runner_config = double("config",
10
+ :project_root => @root,
11
+ :spec_dir => File.join(@root, "fixture", "spec"),
12
+ :spec_path => "/__spec__",
13
+ :root_path => "/__root__",
14
+ :css_files => [],
15
+ :jasmine_files => [],
16
+ :js_files => ["path/file1.js", "path/file2.js"],
17
+ :src_dir => File.join(@root, "fixture", "src"),
18
+ :src_files => ["file1.js"],
19
+ :spec_files => ["example_spec.js"])
20
+ Jasmine::Application.app(runner_config)
21
+ end
22
+
23
+ it "includes no-cache headers for specs" do
24
+ get "/__spec__/example_spec.js"
25
+ last_response.headers.should have_key("Cache-Control")
26
+ last_response.headers["Cache-Control"].should == "max-age=0, private, must-revalidate"
27
+ end
28
+
29
+ it "should serve static files from spec dir under __spec__" do
30
+ get "/__spec__/example_spec.js"
31
+ last_response.status.should == 200
32
+ last_response.content_type.should == "application/javascript"
33
+ last_response.body.should == File.read(File.join(@root, "fixture/spec/example_spec.js"))
34
+ end
35
+
36
+ it "should serve static files from root dir under __root__" do
37
+ get "/__root__/fixture/src/example.js"
38
+ last_response.status.should == 200
39
+ last_response.content_type.should == "application/javascript"
40
+ last_response.body.should == File.read(File.join(@root, "fixture/src/example.js"))
41
+ end
42
+
43
+ it "should serve static files from src dir under /" do
44
+ get "/example.js"
45
+ last_response.status.should == 200
46
+ last_response.content_type.should == "application/javascript"
47
+ last_response.body.should == File.read(File.join(@root, "fixture/src/example.js"))
48
+ end
49
+
50
+ it "should serve Jasmine static files under /__JASMINE_ROOT__/" do
51
+ get "/__JASMINE_ROOT__/jasmine.css"
52
+ last_response.status.should == 200
53
+ last_response.content_type.should == "text/css"
54
+ last_response.body.should == File.read(File.join(Jasmine::Core.path, "jasmine.css"))
55
+ end
56
+
57
+ it "should serve focused suites when prefixing spec files with /__suite__/" do
58
+ pending "Temporarily removing this feature (maybe permanent)"
59
+ Dir.stub!(:glob).and_return { |glob_string| [glob_string] }
60
+ get "/__suite__/file2.js"
61
+ last_response.status.should == 200
62
+ last_response.content_type.should == "text/html"
63
+ last_response.body.should include("\"/__spec__/file2.js")
64
+ end
65
+
66
+ it "should redirect /run.html to /" do
67
+ get "/run.html"
68
+ last_response.status.should == 302
69
+ last_response.location.should == "/"
70
+ end
71
+
72
+ it "should 404 non-existent files" do
73
+ get "/some-non-existent-file"
74
+ last_response.should be_not_found
75
+ end
76
+
77
+ describe "/ page" do
78
+ it "should load each js file in order" do
79
+ get "/"
80
+ last_response.status.should == 200
81
+ last_response.body.should include("path/file1.js")
82
+ last_response.body.should include("path/file2.js")
83
+ end
84
+
85
+ it "should return an empty 200 for HEAD requests to /" do
86
+ head "/"
87
+ last_response.status.should == 200
88
+ last_response.headers['Content-Type'].should == 'text/html'
89
+ last_response.body.should == ''
90
+ end
91
+
92
+ it "should tell the browser not to cache any assets" do
93
+ head "/"
94
+ ['Pragma'].each do |key|
95
+ last_response.headers[key].should == 'no-cache'
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jasmine::AssetPipelineMapper do
4
+ describe "mapping files" do
5
+ it "should retrieve asset paths from the asset pipeline for passed files" do
6
+ #TODO: this expects all src files to be asset pipeline files
7
+ src_files = ["assets/application.js", "assets/other_manifest.js"]
8
+ asset_context = double("asset context")
9
+ asset_context.stub_chain(:asset_paths, :asset_for).with("application", "js").and_return(['asset1.js', 'asset2.js'])
10
+ asset_context.stub_chain(:asset_paths, :asset_for).with("other_manifest", "js").and_return(['asset1.js', 'asset3.js'])
11
+ asset_context.stub(:asset_path) do |asset|
12
+ "/some_location/#{asset}"
13
+ end
14
+ mapper = Jasmine::AssetPipelineMapper.new(src_files, asset_context)
15
+ mapper.files.should == ['some_location/asset1.js?body=true', 'some_location/asset2.js?body=true', 'some_location/asset3.js?body=true']
16
+ end
17
+ end
18
+ end
@@ -4,6 +4,8 @@ require 'selenium-webdriver'
4
4
  describe Jasmine::Config do
5
5
  describe "configuration" do
6
6
  before :each do
7
+ Jasmine::Dependencies.stub(:rails_3_asset_pipeline?) { false }
8
+
7
9
  temp_dir_before
8
10
 
9
11
  Dir::chdir @tmp
@@ -239,83 +241,46 @@ describe Jasmine::Config do
239
241
  end
240
242
  end
241
243
 
242
- describe "environment variables" do
243
- def stub_env_hash(hash)
244
- ENV.stub!(:[]) do |arg|
245
- hash[arg]
246
- end
244
+
245
+ describe "jasmine_stylesheets" do
246
+ it "should return the relative web server path to the core Jasmine css stylesheets" do
247
+ #TODO: wrap Jasmine::Core with a class that knows about the core path and the relative mapping.
248
+ Jasmine::Core.stub(:css_files).and_return(["my_css_file1.css", "my_css_file2.css"])
249
+ Jasmine::Config.new.jasmine_stylesheets.should == ["/__JASMINE_ROOT__/my_css_file1.css", "/__JASMINE_ROOT__/my_css_file2.css"]
247
250
  end
248
- describe "browser configuration" do
249
- it "should use firefox by default" do
250
- stub_env_hash({"JASMINE_BROWSER" => nil})
251
- config = Jasmine::Config.new
252
- config.stub!(:start_jasmine_server)
253
- Jasmine::SeleniumDriver.should_receive(:new).
254
- with("firefox", anything).
255
- and_return(mock(Jasmine::SeleniumDriver, :connect => true))
256
- config.start
257
- end
251
+ end
258
252
 
259
- it "should use ENV['JASMINE_BROWSER'] if set" do
260
- stub_env_hash({"JASMINE_BROWSER" => "mosaic"})
261
- config = Jasmine::Config.new
262
- config.stub!(:start_jasmine_server)
263
- Jasmine::SeleniumDriver.should_receive(:new).
264
- with("mosaic", anything).
265
- and_return(mock(Jasmine::SeleniumDriver, :connect => true))
266
- config.start
267
- end
253
+ describe "jasmine_javascripts" do
254
+ it "should return the relative web server path to the core Jasmine css javascripts" do
255
+ Jasmine::Core.stub(:js_files).and_return(["my_js_file1.js", "my_js_file2.js"])
256
+ Jasmine::Config.new.jasmine_javascripts.should == ["/__JASMINE_ROOT__/my_js_file1.js", "/__JASMINE_ROOT__/my_js_file2.js"]
268
257
  end
258
+ end
269
259
 
270
- describe "jasmine host" do
271
- it "should use http://localhost by default" do
272
- stub_env_hash({})
273
- config = Jasmine::Config.new
274
- config.instance_variable_set(:@jasmine_server_port, '1234')
275
- config.stub!(:start_jasmine_server)
276
-
277
- Jasmine::SeleniumDriver.should_receive(:new).
278
- with(anything, "http://localhost:1234/").
279
- and_return(mock(Jasmine::SeleniumDriver, :connect => true))
280
- config.start
281
- end
260
+ describe "when the asset pipeline is active" do
261
+ before do
262
+ Jasmine::Dependencies.stub(:rails_3_asset_pipeline?) { true }
263
+ end
282
264
 
283
- it "should use ENV['JASMINE_HOST'] if set" do
284
- stub_env_hash({"JASMINE_HOST" => "http://some_host"})
285
- config = Jasmine::Config.new
286
- config.instance_variable_set(:@jasmine_server_port, '1234')
287
- config.stub!(:start_jasmine_server)
265
+ let(:src_files) { ["assets/some.js", "assets/files.js"] }
288
266
 
289
- Jasmine::SeleniumDriver.should_receive(:new).
290
- with(anything, "http://some_host:1234/").
291
- and_return(mock(Jasmine::SeleniumDriver, :connect => true))
292
- config.start
267
+ let(:config) do
268
+ Jasmine::Config.new.tap do |config|
269
+ #TODO: simple_config should be a passed in hash
270
+ config.stub(:simple_config) { { 'src_files' => src_files} }
293
271
  end
272
+ end
294
273
 
295
- it "should use ENV['JASMINE_PORT'] if set" do
296
- stub_env_hash({"JASMINE_PORT" => "4321"})
297
- config = Jasmine::Config.new
298
- Jasmine.stub!(:wait_for_listener)
299
- config.stub!(:start_server)
300
- Jasmine::SeleniumDriver.should_receive(:new).
301
- with(anything, "http://localhost:4321/").
302
- and_return(mock(Jasmine::SeleniumDriver, :connect => true))
303
- config.start
304
- end
274
+ it "should use AssetPipelineMapper to return src_files" do
275
+ mapped_files = ["some.js", "files.js"]
276
+ Jasmine::AssetPipelineMapper.stub_chain(:new, :files).and_return(mapped_files)
277
+ config.src_files.should == mapped_files
305
278
  end
306
279
 
307
- describe "external selenium server" do
308
- it "should use an external selenium server if SELENIUM_SERVER is set" do
309
- stub_env_hash({"SELENIUM_SERVER" => "http://myseleniumserver.com:4441"})
310
- Selenium::WebDriver.should_receive(:for).with(:remote, :url => "http://myseleniumserver.com:4441", :desired_capabilities => :firefox)
311
- Jasmine::SeleniumDriver.new('firefox', 'http://localhost:8888')
312
- end
313
- it "should use an local selenium server with a specific port if SELENIUM_SERVER_PORT is set" do
314
- stub_env_hash({"SELENIUM_SERVER_PORT" => "4441"})
315
- Selenium::WebDriver.should_receive(:for).with(:remote, :url => "http://localhost:4441/wd/hub", :desired_capabilities => :firefox)
316
- Jasmine::SeleniumDriver.new('firefox', 'http://localhost:8888')
317
- end
280
+ it "should pass the config src_files to the AssetPipelineMapper" do
281
+ Jasmine::Config.stub(:simple_config)
282
+ Jasmine::AssetPipelineMapper.should_receive(:new).with(src_files).and_return(double("mapper").as_null_object)
283
+ config.src_files
318
284
  end
319
285
  end
320
-
321
286
  end