integration-tests-rails 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd9324031c217a8ec83714af3b5e4aa13970d8f7207329dec77cf7eac9e67f77
4
- data.tar.gz: 063caea1b6bb80fd619cb1fb9cde292140282b5d9943c5bd7d35cbdf11e1942f
3
+ metadata.gz: 8565c51a68e4a566e3a2c24beac10c56e34135db4d21d75c877b9014d94093f2
4
+ data.tar.gz: 3bc4334e481beec869779f050d8ba9993e5696739ab75544e2f7835970697fcd
5
5
  SHA512:
6
- metadata.gz: 63ca3042ff7f05e8f785f79c396b644267fb3570599bb4a7f21ca6034a58dc75460ee71d396af671502e0721a012b63daeabe22683fc24b7f825951ddce5606a
7
- data.tar.gz: d5a1972b1930d8ae3d68b1c2498cc2a08d496d32d18af7ca1274b3e6999be06b8a6990e9b58988c364c492ecf0e997649a0e8fdd75fa94adc376c6ed61dd62ab
6
+ metadata.gz: 6580bf376345a375c56a051c237ec5b0937c03313f2967a708716d231f03034629b610bd41f9ef8c0901266984ac25f76d17b80746c1896f4ddc273ed30f50bd
7
+ data.tar.gz: e5471e16f941749f504575705f3e4992084a6e34b2c9d794a65273dfb690edadb3a150f1d31c5019fcbf8d266f7b38ba515c8ac130e25906d1d708190d095dcf
data/README.md CHANGED
@@ -13,6 +13,8 @@ This gem is designed to facilitate integration testing in Ruby on Rails applicat
13
13
 
14
14
  ## Getting Started
15
15
 
16
+ Gem is easy to setup.
17
+
16
18
  ### Installation
17
19
 
18
20
  Add this line to your Rails application's Gemfile:
@@ -76,6 +78,14 @@ end
76
78
 
77
79
  ## Unit Testing JavaScript Code
78
80
 
81
+ This feature is **EXPERIMENTAL** and may change in future releases. It allows you to write unit tests for JavaScript code using RSpec and Capybara. To enable the feature, one must set the `experimental_features` configuration option to `true`:
82
+
83
+ ```ruby
84
+ IntegrationTestsRails.setup do |config|
85
+ config.experimental_features = true
86
+ end
87
+ ```
88
+
79
89
  ### Usage
80
90
 
81
91
  To unit test JavaScript code, the gem will visit a test HTML page. By default, it only renders a complete HTML page that also loads importmap-supporting JavaScript code to set up the environment for testing.
@@ -217,6 +227,8 @@ After the tests (successful, failed or cancelled), coverage reports will be gene
217
227
 
218
228
  ## Example Setups
219
229
 
230
+ The following are working examples.
231
+
220
232
  ### Using Docker Desktop
221
233
 
222
234
  If you want to use the [Alpine Chrome](https://hub.docker.com/r/zenika/alpine-chrome) image, this is definitely possible.
@@ -37,6 +37,7 @@ module IntegrationTestsRails
37
37
  let(:function) { nil }
38
38
 
39
39
  before do
40
+ # page.driver.wait_for_network_idle(timeout: IntegrationTestsRails.configuration.timeout)
40
41
  visit '/tests'
41
42
  result
42
43
  end
@@ -34,6 +34,10 @@ module IntegrationTestsRails
34
34
  timeout = config.timeout
35
35
  options = {
36
36
  window_size: config.window_size,
37
+ browser_options: {
38
+ 'no-sandbox': nil,
39
+ 'disable-dev-shm-usage': nil
40
+ },
37
41
  url: config.chrome_url,
38
42
  timeout: timeout,
39
43
  process_timeout: timeout
@@ -16,12 +16,15 @@ module IntegrationTestsRails
16
16
  def ensure_server_ready(context)
17
17
  return if @server_ready
18
18
 
19
- config = IntegrationTestsRails.configuration
20
19
  log "Waiting for server on #{::Capybara.app_host.presence || 'localhost'} to start..."
21
-
22
- server_retries = config.max_server_retries
20
+ configuration = IntegrationTestsRails.configuration
21
+ server_retries = configuration.max_server_retries
23
22
  server_retries.times do |attempt|
24
- context.visit('/400')
23
+ if configuration.experimental_features
24
+ context.visit('/tests')
25
+ else
26
+ context.visit('/')
27
+ end
25
28
  @server_ready = true
26
29
  log 'Server is ready!'
27
30
  break
@@ -38,11 +41,15 @@ module IntegrationTestsRails
38
41
  IntegrationTestsRails::Capybara::Util.ensure_server_ready(self)
39
42
  end
40
43
 
41
- config.include(Helper, type: :feature, unit: true)
44
+ if IntegrationTestsRails.configuration.experimental_features
45
+ config.include(Helper, type: :feature, unit: true)
46
+ end
42
47
  end
43
48
  end
44
49
 
45
50
  def configure_routes
51
+ return unless IntegrationTestsRails.configuration.experimental_features
52
+
46
53
  app = Rails.application
47
54
  routes = app.routes
48
55
  # Use append and let Rails handle finalization automatically
@@ -39,12 +39,13 @@ module IntegrationTestsRails
39
39
 
40
40
  attr_accessor :source_dir, :output_dir, :backup_dir, :coverage_path, :wait_time, :remote,
41
41
  :chrome_url, :tests_page_html, :window_size, :max_server_retries,
42
- :verbose, :timeout, :server_host, :server_port, :puma_threads
42
+ :verbose, :timeout, :server_host, :server_port, :puma_threads, :experimental_features
43
43
 
44
44
  def initialize
45
45
  @backup_dir = 'tmp/js_backup'
46
46
  @chrome_url = nil
47
47
  @coverage_path = 'coverage/nyc'
48
+ @experimental_features = false
48
49
  @max_server_retries = 1000
49
50
  @output_dir = 'tmp/instrumented_js'
50
51
  @puma_threads = '1:1'
@@ -32,27 +32,45 @@ module IntegrationTestsRails
32
32
 
33
33
  def instrument_file(file)
34
34
  config = IntegrationTestsRails.configuration
35
-
36
35
  relative_path = Pathname.new(file).relative_path_from(config.source_path)
37
36
  output_file = config.output_path.join(relative_path)
38
37
 
39
38
  FileUtils.mkdir_p(output_file.dirname)
40
39
 
41
- # Use Node.js to instrument the file
42
40
  code = File.read(file)
43
- escaped_code = JSON.generate(code)
41
+ instrumented = instrument_code_with_istanbul(code, file)
42
+
43
+ write_instrumented_file(output_file, instrumented, relative_path)
44
+ end
45
+
46
+ private
47
+
48
+ def instrument_code_with_istanbul(code, file)
49
+ require 'tempfile'
50
+ Tempfile.create(['code', '.js']) do |temp_code|
51
+ temp_code.write(code)
52
+ temp_code.flush
53
+
54
+ js_command = build_istanbul_command(temp_code.path, file)
55
+ `node -e #{Shellwords.escape(js_command)}`.strip
56
+ end
57
+ end
58
+
59
+ def build_istanbul_command(code_path, file)
60
+ escaped_code_path = JSON.generate(code_path)
44
61
  escaped_file = JSON.generate(file.to_s)
45
62
 
46
- js_command = <<~JS
63
+ <<~JS
64
+ const fs = require('fs');
47
65
  const { createInstrumenter } = require('istanbul-lib-instrument');
48
66
  const instrumenter = createInstrumenter({ esModules: true, compact: false });
49
- const code = #{escaped_code};
67
+ const code = fs.readFileSync(#{escaped_code_path}, 'utf8');
50
68
  const filename = #{escaped_file};
51
69
  console.log(instrumenter.instrumentSync(code, filename));
52
70
  JS
71
+ end
53
72
 
54
- instrumented = `node -e #{Shellwords.escape(js_command)}`.strip
55
-
73
+ def write_instrumented_file(output_file, instrumented, relative_path)
56
74
  if $CHILD_STATUS.success?
57
75
  File.write(output_file, instrumented)
58
76
  else
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module IntegrationTestsRails
4
- VERSION = '1.0.8'
4
+ VERSION = '1.0.9'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: integration-tests-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 1.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tien