如何有效地使用 Jasmine 测试通过 Jammit 打包的 javascript 资源?

发布于 2024-11-03 23:11:06 字数 934 浏览 1 评论 0原文

我有一个 Rails 应用程序,它使用 Jammit 组合 javascript 资源,并且我想使用 Jasmine 用于对我的 javascript 进行 BDD 式测试。我想知道是否有人对从 Jasmine 内部访问 Jammit 生成的“包”有什么建议?

问题是 Jasmine 是通过在磁盘上定义要测试的 JS 文件列表来配置的,然后它将这些文件包含在自己的测试运行器页面中,该测试运行器页面在浏览器中加载并运行。

在使用 Jammit 打包之前,我可以引用 jasmine.yml 配置文件中的每个单独的 JS 文件...但是,Jammit 已经在为我处理文件之间的依赖关系,更重要的是,我还需要访问Jammit 生成的已编译的 javascript 模板

我还可以手动运行 Jammit 首先生成已编译的资产,然后运行 ​​Jasmine,但最终我必须在每次测试运行之前手动重新生成资产以测试更改,这会严重限制快速测试 -驱动型工作流程。

我想知道是否可以通过某种方式:

  • 从 Jasmine 的机架服务器内安装 Jammit 控制器,以便它可以提供 Jasmine 的软件包?这基本上与 Jammit 在 Rails 开发环境中所做的工作方式相同。
  • 在执行测试之前,以某种方式挂钩 Jasmine 来打包每个页面加载的资源?这会比较慢,但会节省我一步并确保事情是最新的。

有什么建议吗?我刚刚开始做这件事,所以我可能会做错事。任何建议将不胜感激。 :-)

谢谢! -约翰

I have a rails app that is combining javascript assets using Jammit, and I'd like to use Jasmine for BDD-style testing of my javascript. I'm wondering if anyone has any advice on accessing the Jammit-generated 'pacakges' from within Jasmine?

The issue is that Jasmine is configured by defining a list of JS files on disk to test, and it then includes those files within its own test runner page which is loaded and run in a browser.

I could reference each of the individual JS files inside of the jasmine.yml config file before they're packaged with Jammit... however, Jammit is already dealing with dependencies between files for me, and, more importantly, I also need access to the compiled javascript templates that Jammit produces.

I could also manually run Jammit to generate the compiled assets first and then run Jasmine, but I'd wind up having to re-generate the assets by hand before each test run in order to test changes, which would seriously cramp a fast test-driven type workflow.

I'm wondering if I could somehow:

  • Mount the Jammit controller from within Jasmine's rack server so it could serve out the packages from Jasmine? This would basically work the same way as Jammit already does from within Rails' development env.
  • Hook into Jasmine somehow to package the assets on every page load before the tests are executed? This would be slower, but would save me a step and ensure things were up to date.

Any suggestions? I'm just getting started with this, so I could be going about it all wrong. Any advice would be greatly appreciated. :-)

Thanks!
-John

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎盛开的樱花 2024-11-10 23:11:06

这是您正在寻找的神奇组合:

  1. guard gemguard-jammit gem 用于监视项目资产的更改
  2. 安装 LiveReload 插件 在您选择的浏览器中安装 guard-livereload gem 这样,只要您的规格或资产发生变化,您就可以让浏览器自动重新加载。
  3. 启动守卫,然后耙茉莉花,然后在浏览器中加载茉莉花规格,然后按实时重新加载按钮连接到守卫启动的实时重新加载服务器
  4. 更改您的文件。当守卫运行 jammit 并指示您的浏览器刷新 jasmine 规范时,观看奇迹的发生。

这是一个可以帮助您入门的 Guardfile 示例:

guard 'jammit' do
  watch(%r{public/javascripts/(.*)\.js})
  watch(%r{app/views/jst/(.*)\.jst})
  watch(%r{public/stylesheets/(.*)\.css})
end

guard 'livereload', :apply_js_live => false do
  watch(%r{app/.+\.(erb|haml)})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{public/.+\.(css|js|html)})
  watch(%r{config/locales/.+\.yml})
  watch(%r{spec/javascripts/.+\.js})
end

Here's the magic combo you're looking for:

  1. Use the guard gem along with the guard-jammit gem to watch for changes to your project's assets
  2. Install the LiveReload plugin in the browser of your choice and install the guard-livereload gem so you can have your browser auto reload whenever your specs or assets change.
  3. Fire up guard, then rake jasmine, then load your jasmine specs in your browser, then press the live-reload button to connect to the live-reload server that guard started
  4. Change your files. Watch the magic happen as guard runs jammit and instructs your browser to refresh the jasmine specs.

Here's an example Guardfile to get you started:

guard 'jammit' do
  watch(%r{public/javascripts/(.*)\.js})
  watch(%r{app/views/jst/(.*)\.jst})
  watch(%r{public/stylesheets/(.*)\.css})
end

guard 'livereload', :apply_js_live => false do
  watch(%r{app/.+\.(erb|haml)})
  watch(%r{app/helpers/.+\.rb})
  watch(%r{public/.+\.(css|js|html)})
  watch(%r{config/locales/.+\.yml})
  watch(%r{spec/javascripts/.+\.js})
end
烟酉 2024-11-10 23:11:06

我想出了一个好的解决方案:在 jasmine 启动时强制 Jammit 重新加载和打包。为此,您需要编辑 jasmine_config.rb 文件:

require 'jammit'
module Jasmine
  class Config

    Jammit.reload!
    Jammit.package!

   end
end

我在这里写了一篇更详细的文章:http://www.rebeccamiller-webster.com/2011/05/jammit-jasmine-bdd/

I've come up with an OK solution: force Jammit to reload and package when jasmine starts. To do this, you need to edit the jasmine_config.rb file:

require 'jammit'
module Jasmine
  class Config

    Jammit.reload!
    Jammit.package!

   end
end

I wrote a bit more detailed post about it here: http://www.rebeccamiller-webster.com/2011/05/jammit-jasmine-bdd/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文