为 Jasmine 自动编译 Rails 资源(使用 Sprockets)(jasmine-browser-runner)

问题描述 投票:0回答:1

我最终将 Rails 应用程序从旧的

jasmine-rails
Rails gem 移至新的
jasmine-core
jasmine-browser-runner
以进行 Javascript 测试。一切都很好,除了我现在似乎总是需要:

  • 删除之前编译的资源(以防止
    "srcFiles": [ "application-*.js" ]
    配置让测试服务器将所有之前编译的版本加载到
    application.js
  • 再次预编译资产(通过链轮交付)
  • 使用
    npx jasmine-browser-runner
  • 重新启动 Express 测试服务器

由于旧的

jasmine-rails
方式是使用现有的应用程序服务器,资产管道编译是自动完成的,因此Javascript代码中的任何更改都可以立即在Jasmine中进行测试。谁知道一种好方法来设置
jasmine-browser-runner
,这样只需要一个命令就可以让服务器运行并且可以立即测试源文件更新?

ruby-on-rails jasmine
1个回答
0
投票

我决定创建一个 Jasmine 测试运行程序 (

bin/jasmine
),它将在运行规范一次之前执行所需的步骤。

#!/usr/bin/env ruby

# Pass in parameter -i to simply start the test server so that you can run
# the test in a browser by going to localhost:8888. 
# This enables you to debug the code.

# Pass in parameter -h to run the tests in headless Chrome.

# Make sure to configure the right SRC_DIR and SRC_FILES for your project

require 'fileutils'
include FileUtils

SRC_DIR = ['public/test-assets']
SRC_FILES = ['jasmine-src-*']

# path to your application root.
APP_ROOT = File.expand_path('..', __dir__)
COMPILED_ASSETS_DIR = File.join(APP_ROOT, SRC_DIR)

def system!(*args)
  system(*args) || abort("\n== Command #{args} failed ==")
end

if File.directory? COMPILED_ASSETS_DIR
  chdir COMPILED_ASSETS_DIR do
    puts '== Removing previously compiled versions of source files =='
    system! "rm #{SRC_FILES.join(' ')}"
  end
end

puts '== Precompiling assets =='
system! 'RAILS_ENV=test WEBPACKER_PRECOMPILE=false bundle exec rails assets:precompile'

mode = ARGV.include?('-i') ? 'serve' : 'runSpecs'
headless_browser = mode == 'runSpecs' && ARGV.include?('-h') ? '--browser=headlessChrome' : ''

puts "== Running specs =="
system! "npx jasmine-browser-runner #{mode} #{headless_browser}"

请注意,上面的脚本编译了测试环境中的资源。通过将测试环境中的资产目录设置为与开发环境中不同的目录,我们可以防止编译的资产妨碍开发资产。在上面的脚本中,我们假设我们在

config/environments/test.rb
中有以下配置:

config.assets.prefix = "/test-assets"
© www.soinside.com 2019 - 2024. All rights reserved.