johnbintz / jasmine-headless-webkit

This project is dead. You should use Karma instead. I do.
http://karma-runner.github.io/
197 stars 99 forks source link

JST object has different keys in jasmine-headless-webkit #138

Open atroche opened 12 years ago

atroche commented 12 years ago

My tests were passing in the browser-based Jasmine test runner (/specs/) but failing when in jasmine-headless-webkit. I worked out that it was because the keys in the global JST object (the compiled from JavaScript templates by Srockets) were different.

For example, in the browser the object looked like:

JST = { templates/diary_entry: function() { etc. } }

But in headless, it was:

JST = { app/assets/javascripts/templates/diary_entry: function() { etc. } }

I worked around this by writing a hacky little function:

findTemplate: function(template_name) {
  // Search through global JavaScript Templates object compiled by
  // asset pipeline, and return a template with a matching name.
  var blank_template = function() { return "" };

  if (typeof JST === "undefined") { return blank_template }

  for (var key in JST) {
    if (key.indexOf(template_name) !== -1) {
      return JST[key];
    }
  }

  return blank_template;
}

Is there a better way to fix this? Am I missing something obvious?

Any help would be much appreciated.

johnbintz commented 12 years ago

What is in your jasmine.yml file?

atroche commented 12 years ago
src_files:
  - "vendor/**/*.{js,coffee}"
  - "lib/**/*.{js,coffee}"
  - "app/**/*.{js,coffee}"

stylesheets:
  - "vendor/**/*.css"
  - "lib/**/*.css"
  - "app/**/*.css"

helpers:
  - "helpers/**/*.{js,coffee}"

spec_files:
  - "**/*[Ss]pec.{js,coffee}"

src_dir:

spec_dir: spec/javascripts

I've tried changing {js,coffee} to {js,coffee,jst}, but it doesn't make a difference.

ddellacosta commented 12 years ago

Hi folks, I'm having this issue also on jasmine-headless-webkit-0.8.4 w/jasmine-core-1.1.0 (if there are any other relevant version #s you need let me know). In response to the last comment by johnbintz, related to what atroche responded with, if I do a

bundle exec jasmine-headless-webkit -l

...I do see the (in my case) .ejs files included, so I think the jasmine.yml file is not the issue. Or more generally, inclusion of the files is not the issue...if that's what you were trying to investigate.

Also, atroche, I've converted your little hacky function to CoffeeScript, so until we've got a better solution to this here's another version for those using CS:

findTemplate = (template_name) ->
  # Search through global JavaScript Templates object compiled by
  # asset pipeline, and return a template with a matching name.
  blank_template = -> ""

  return blank_template if typeof JST is "undefined"

  for key of JST
    return JST[key] if key.indexOf(template_name) isnt -1

  blank_template

I'm using this as a helper in Jasmine, which is better than it not working but needless to say, it'd be better if I knew what was up and could fix this in a more "solid" way.

madtrick commented 12 years ago

@ddellacosta I think that you should change your

for key in JST

for

for key of JST

As that's the way to iterate over object properties in Coffeescript : )

ddellacosta commented 12 years ago

Gotcha, thanks madtrick! I'm new to CS so still picking up the syntax...

amisarca commented 12 years ago

Where should I place the function? I placed it in a helper file, but it had no effect.

Thanks.