Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Fail if a JS file has a syntax error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Donovan + Christian Williams committed Jan 14, 2012
1 parent 953d17d commit 7dfe0f0
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 deletions.
57 changes: 56 additions & 1 deletion lib/jasmine/run.html.erb
Expand Up @@ -41,12 +41,67 @@
}

})();

jasmine._runner_ = {
expectedFiles: [<%= js_files.map {|file| file.inspect }.join(', ') %>],
loadedFiles: {},
errors: {},
addError: function(error) {
var errors = this.errors[this.currentFile] || (this.errors[this.currentFile] = []);
errors.push(error);
},
fileLoaded: function() {
this.loadedFiles[this.currentFile] = true;
},
onError: function(message, file, line) {
jasmine._runner_.addError({
message: message,
file: file,
line: line
});
},
oldOnError: window.onerror
};

window.onerror = jasmine._runner_.onError;
</script>

<% js_files.each do |js_file| %>
<script src="<%= js_file %>" type="text/javascript"></script>
<script type="text/javascript">jasmine._runner_.currentFile = <%= js_file.inspect %>;</script>
<script src="<%= js_file %>" type="text/javascript"></script>
<% end %>

<script type="text/javascript">
describe("JS file loading", function() {
var runner = jasmine._runner_;
for (var i = 0, length = runner.expectedFiles.length; i < length; i++) {
var expectedFile = runner.expectedFiles[i],
errors = runner.errors[expectedFile],
loaded = runner.loadedFiles[expectedFile];

if (!loaded || (errors && errors.length)) {
(function(loaded, errors, expectedFile) {
it("succeeds for "+expectedFile, function() {
if (!loaded) {
this.fail(expectedFile + " failed to load");
}

if (errors && errors.length) {
for (var j = 0; j < errors.length; j++) {
var error = errors[j];
this.fail(expectedFile + ": " + error.message + " at " + error.file+":"+error.line);
}
}
});
})(loaded, errors, expectedFile);
}
}
});

if (window.onerror == jasmine._runner_.onError) {
window.onerror = jasmine._runner_.oldOnError;
}
</script>
</head>
<body>
<div id="jasmine_content"></div>
Expand Down
34 changes: 34 additions & 0 deletions lib/jasmine/server.rb
Expand Up @@ -71,9 +71,43 @@ def call(env)
end
end

class JasmineLoadFlag
def initialize(app)
@app = app
end

def call(env)
code, headers, body = @app.call(env)

if code == 200 && (headers['Content-Type'] && headers['Content-Type'].include?('javascript'))
body = JsFileFlagAppender.new(body)
headers.delete('Content-Length')
end

[code, headers, body]
end
end

class JsFileFlagAppender
def initialize(body)
@body = body
end

def each(&block)
@body.each(&block)
block.call("\n\njasmine._runner_.fileLoaded();")
end

def close
@body.close if @body.respond_to?(:close)
end
end

def self.app(config)
Rack::Builder.app do
use Rack::Head
use JasmineLoadFlag

if Jasmine::Dependencies.rails_3_asset_pipeline?
map('/assets') do
run Rails.application.assets
Expand Down
4 changes: 2 additions & 2 deletions spec/fixture/spec/example_spec.js
@@ -1,5 +1,5 @@
describe("example_spec", function() {
it("should be here for path loading tests", function() {
expect(true).toBe(true);
}
})
});
});
Empty file.
14 changes: 8 additions & 6 deletions spec/server_spec.rb
Expand Up @@ -2,6 +2,8 @@
require 'rack/test'

describe "Jasmine.app" do
LOADED_FLAG = "\n\njasmine._runner_.fileLoaded();"

include Rack::Test::Methods

def app
Expand All @@ -15,25 +17,25 @@ def app
Jasmine.app(config)
end

it "should serve static files from spec dir under __spec__" do
it "should serve static files from spec dir under __spec__ with loaded flag appended" do
get "/__spec__/example_spec.js"
last_response.status.should == 200
last_response.content_type.should == "application/javascript"
last_response.body.should == File.read(File.join(@root, "fixture/spec/example_spec.js"))
last_response.body.should == File.read(File.join(@root, "fixture/spec/example_spec.js")) + LOADED_FLAG
end

it "should serve static files from root dir under __root__" do
it "should serve static files from root dir under __root__ with loaded flag appended" do
get "/__root__/fixture/src/example.js"
last_response.status.should == 200
last_response.content_type.should == "application/javascript"
last_response.body.should == File.read(File.join(@root, "fixture/src/example.js"))
last_response.body.should == File.read(File.join(@root, "fixture/src/example.js")) + LOADED_FLAG
end

it "should serve static files from src dir under /" do
it "should serve static files from src dir under / with loaded flag appended" do
get "/example.js"
last_response.status.should == 200
last_response.content_type.should == "application/javascript"
last_response.body.should == File.read(File.join(@root, "fixture/src/example.js"))
last_response.body.should == File.read(File.join(@root, "fixture/src/example.js")) + LOADED_FLAG
end

it "should serve Jasmine static files under /__JASMINE_ROOT__/" do
Expand Down

0 comments on commit 7dfe0f0

Please sign in to comment.