업데이트 : JSCover 버전 1.05부터 시작하여 이전 답변에서 설명한 해킹은 더 이상 필요하지 않습니다. 이를 반영하여 답변을 업데이트했습니다.
나는 Rails + Capybara 파이프 라인에서 JSCover 작업을 할 수 있었지만 작동시키기 위해 해킹이 필요했습니다.
- 는
- 모든 파일을 기기에 자바 항아리를 호출하는 스크립트를 생성하고
- 가 jscover 패치 디렉토리 임시로 빈 보고서를 생성하기 위해 레일 자산 파이프 라인을 사용하여 좀 레이크 작업을 내장 .js 스크립트는 "report mode"에서 작동합니다 (끝에 jscoverage_isReport = true를 추가하면됩니다).
- 은/public/assets에 결과를 복사하여 테스트가 변경되지 않고 자동으로 적용 보고서를 자동으로 열 수 있도록합니다 브라우저에서
그런 다음 테스트 시작시 브라우저의 localStorage를 지우는 설정 작업과 마지막에 완성 된 보고서를 작성하는 teardown 작업을 추가했습니다.
def setup
unless $startup_once
$startup_once=true
puts 'Clearing localStorage'
visit('/')
page.execute_script('localStorage.removeItem("jscover");')
end
end
def teardown
out=page.evaluate_script("typeof(_$jscoverage)!='undefined' && jscoverage_serializeCoverageToJSON()")
unless out.blank? then
File.open(File.join(Rails.root,"public/assets/jscoverage.json"), 'w') {|f| f.write(out) }
end
end
어쨌든, 최종 결과가 잘 작동, 이런 식으로 일을의 장점은 또한 CI에 포함 할 수 있도록 그것은 또한 머리가없는 브라우저에서 작동한다는 것입니다.
*** 업데이트 2 : 다음의 단계를 자동화하는 레이크 작업이 이제 (트렁크) JSCover에 추가 된
# Coverage testing for JavaScript
#
# Usage:
# Download JSCover from: http://tntim96.github.io/JSCover/ and move it to
# ~/Applications/JSCover-1
# First instumentalize the javascript files:
# rake assets:coverage
# Then run browser tests
# rake test
# See the results in the browser
# http://localhost:3000/assets/jscoverage.html
# Don't forget to clean up instrumentalization afterwards:
# rake assets:clobber
# Also don't forget to re-do instrumentalization after changing a JS file
namespace :assets do
desc 'Instrument all the assets named in config.assets.precompile'
task :coverage do
Rake::Task["assets:coverage:primary"].execute
end
namespace :coverage do
def jscoverage_loc;Dir.home+'/Applications/JSCover-1/';end
def internal_instrumentalize
config = Rails.application.config
target=File.join(Rails.public_path,config.assets.prefix)
environment = Sprockets::Environment.new
environment.append_path 'app/assets/javascripts'
`rm -rf #{tmp=File.join(Rails.root,'tmp','jscover')}`
`mkdir #{tmp}`
`rm -rf #{target}`
`mkdir #{target}`
print 'Generating assets'
require File.join(Rails.root,'config','initializers','assets.rb')
(%w{application.js}+config.assets.precompile.select{|f| f.is_a?(String) && f =~ /\.js$/}).each do |f|
print '.';File.open(File.join(target,f), 'w') {|ff| ff.write(environment[f].to_s) }
end
puts "\nInstrumentalizing…"
`java -Dfile.encoding=UTF-8 -jar #{jscoverage_loc}target/dist/JSCover-all.jar -fs #{target} #{tmp} #{'--no-branch' unless ENV['C1']} --local-storage`
puts 'Copying into place…'
`cp -R #{tmp}/ #{target}`
`rm -rf #{tmp}`
File.open("#{target}/jscoverage.js",'a'){|f| f.puts 'jscoverage_isReport = true' }
end
task :primary => %w(assets:environment) do
unless Dir.exist?(jscoverage_loc)
abort "Cannot find JSCover! Download from: http://tntim96.github.io/JSCover/ and put in #{jscoverage_loc}"
end
internal_instrumentalize
end
end
end
나는 또한 이것에 흥미가있다. 요청 사양에서 js 적용 범위를 달성 할 수 있는지 궁금하지 않으십니까? – steve