Skip to content

Commit d9e79ce

Browse files
committed
use Karma as the test runner for the UJS tests
1 parent b9aaf99 commit d9e79ce

31 files changed

+3994
-23446
lines changed

Gemfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ end
112112

113113
# Action View
114114
group :view do
115-
gem "blade", require: false, platforms: [:ruby]
116115
gem "sprockets-export", require: false
117116
end
118117

Gemfile.lock

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,6 @@ GEM
140140
beaneater (1.1.1)
141141
benchmark-ips (2.9.2)
142142
bindex (0.8.1)
143-
blade (0.7.3)
144-
activesupport (>= 3.0.0)
145-
blade-qunit_adapter (>= 2.0.1)
146-
coffee-script
147-
coffee-script-source
148-
curses (>= 1.4.0)
149-
eventmachine
150-
faye
151-
sprockets (>= 3.0)
152-
thin (>= 1.6.0)
153-
thor (>= 0.19.1)
154-
useragent (>= 0.16.7)
155-
blade-qunit_adapter (2.0.1)
156143
bootsnap (1.9.3)
157144
msgpack (~> 1.0)
158145
builder (3.2.4)
@@ -170,20 +157,13 @@ GEM
170157
xpath (~> 3.2)
171158
cgi (0.3.6)
172159
childprocess (4.1.0)
173-
coffee-script (2.4.1)
174-
coffee-script-source
175-
execjs
176-
coffee-script-source (1.12.2)
177160
concurrent-ruby (1.1.9)
178161
connection_pool (2.2.5)
179-
cookiejar (0.3.3)
180162
crack (0.4.5)
181163
rexml
182164
crass (1.0.6)
183165
cssbundling-rails (1.1.0)
184166
railties (>= 6.0.0)
185-
curses (1.4.4)
186-
daemons (1.4.1)
187167
dalli (3.2.0)
188168
dante (0.2.0)
189169
debug (1.4.0)
@@ -211,7 +191,6 @@ GEM
211191
et-orbi (1.2.6)
212192
tzinfo
213193
event_emitter (0.2.6)
214-
eventmachine (1.2.7)
215194
execjs (2.8.1)
216195
faraday (1.8.0)
217196
faraday-em_http (~> 1.0)
@@ -234,17 +213,6 @@ GEM
234213
faraday-rack (1.0.0)
235214
faraday_middleware (1.2.0)
236215
faraday (~> 1.0)
237-
faye (1.4.0)
238-
cookiejar (>= 0.3.0)
239-
em-http-request (>= 1.1.6)
240-
eventmachine (>= 0.12.0)
241-
faye-websocket (>= 0.11.0)
242-
multi_json (>= 1.0.0)
243-
rack (>= 1.0.0)
244-
websocket-driver (>= 0.5.1)
245-
faye-websocket (0.11.1)
246-
eventmachine (>= 0.12.0)
247-
websocket-driver (>= 0.5.1)
248216
ffi (1.15.4)
249217
fugit (1.5.2)
250218
et-orbi (~> 1.1, >= 1.1.8)
@@ -286,7 +254,6 @@ GEM
286254
os (>= 0.9, < 2.0)
287255
signet (>= 0.16, < 2.a)
288256
hashdiff (1.0.1)
289-
http_parser.rb (0.8.0)
290257
httpclient (2.8.3)
291258
i18n (1.8.11)
292259
concurrent-ruby (~> 1.0)
@@ -518,10 +485,6 @@ GEM
518485
railties (>= 6.0.0)
519486
terser (1.1.8)
520487
execjs (>= 0.3.0, < 3)
521-
thin (1.8.1)
522-
daemons (~> 1.0, >= 1.0.9)
523-
eventmachine (~> 1.0, >= 1.0.4)
524-
rack (>= 1, < 3)
525488
thor (1.2.1)
526489
tilt (2.0.10)
527490
timeout (0.3.1)
@@ -575,7 +538,6 @@ DEPENDENCIES
575538
backburner
576539
bcrypt (~> 3.1.11)
577540
benchmark-ips
578-
blade
579541
bootsnap (>= 1.4.4)
580542
capybara (>= 3.26)
581543
cgi (>= 0.3.6)

actionview/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/lib/assets/compiled/
2+
/test/ujs/compiled/
23
/log/
34
/test/fixtures/public/absolute/
45
/test/ujs/log/

actionview/RUNNING_UJS_TESTS.rdoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
== Running UJS tests
22

3-
Ensure that you can build the project by running:
4-
rake ujs:server
3+
Run the tests in headless mode by running:
4+
5+
rake test:ujs
56

6-
Then run the web tests by visiting the following URL in your browser:
7+
To run the tests in a browser, start the Rails UJS server by running:
78

8-
http://localhost:4567
9+
rake ujs:server

actionview/Rakefile

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require "rake/testtask"
44
require "fileutils"
55
require "open3"
6+
require "base64"
67

78
desc "Default Task"
89
task default: :test
@@ -38,15 +39,6 @@ namespace :test do
3839
listen_host = "localhost"
3940
listen_port = "4567"
4041

41-
runner_command = %w(ruby ../ci/qunit-selenium-runner.rb)
42-
if ENV["SELENIUM_DRIVER_URL"]
43-
require "socket"
44-
runner_command += %W(http://#{Socket.gethostname}:#{listen_port}/ #{ENV["SELENIUM_DRIVER_URL"]})
45-
listen_host = "0.0.0.0"
46-
else
47-
runner_command += %W(http://localhost:#{listen_port}/)
48-
end
49-
5042
Dir.mkdir("log")
5143
pid = File.open("log/test.log", "w") do |f|
5244
spawn(*%W(rackup test/ujs/config.ru -o #{listen_host} -p #{listen_port} -s puma), out: f, err: f, pgroup: true)
@@ -66,8 +58,7 @@ namespace :test do
6658

6759
sleep 0.2
6860
end
69-
70-
system(*runner_command)
61+
system(Hash[*Base64.decode64(ENV.fetch("ENCODED", "")).split(/[ =]/)], "npm", "test")
7162
status = $?.exitstatus
7263
ensure
7364
Process.kill("KILL", -pid) if pid
@@ -101,7 +92,8 @@ end
10192
namespace :ujs do
10293
desc "Starts the test server"
10394
task :server do
104-
system "bundle exec rackup test/ujs/config.ru -p 4567 -s puma"
95+
spawn("bundle exec rackup test/ujs/config.ru -p 4567 -s puma")
96+
system("npm test -- --no-single-run --browsers Chrome")
10597
end
10698
end
10799

actionview/karma.conf.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const config = {
2+
browsers: ["ChromeHeadless"],
3+
frameworks: ["qunit"],
4+
files: [
5+
"test/ujs/compiled/test.js",
6+
],
7+
8+
client: {
9+
clearContext: false,
10+
qunit: {
11+
showUI: true
12+
}
13+
},
14+
15+
singleRun: true,
16+
autoWatch: false,
17+
18+
captureTimeout: 180000,
19+
browserDisconnectTimeout: 180000,
20+
browserDisconnectTolerance: 3,
21+
browserNoActivityTimeout: 300000,
22+
proxies: {
23+
'/echo': 'http://localhost:4567/echo',
24+
'/error': 'http://localhost:4567/error'
25+
}
26+
}
27+
28+
if (process.env.CI) {
29+
config.customLaunchers = {
30+
sl_chrome: sauce("chrome", "latest", "Windows 10")
31+
}
32+
33+
config.browsers = Object.keys(config.customLaunchers)
34+
config.reporters = ["dots", "saucelabs"]
35+
36+
config.sauceLabs = {
37+
testName: "Rails UJS",
38+
retryLimit: 3,
39+
build: buildId(),
40+
}
41+
42+
function sauce(browserName, version, platform) {
43+
const options = {
44+
base: "SauceLabs",
45+
browserName: browserName.toString(),
46+
version: version.toString(),
47+
}
48+
if (platform) {
49+
options.platform = platform.toString()
50+
}
51+
return options
52+
}
53+
54+
function buildId() {
55+
const { BUILDKITE_JOB_ID } = process.env
56+
return BUILDKITE_JOB_ID
57+
? `Buildkite ${BUILDKITE_JOB_ID}`
58+
: ""
59+
}
60+
}
61+
62+
module.exports = function(karmaConfig) {
63+
karmaConfig.set(config)
64+
}

actionview/package.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
},
1313
"scripts": {
1414
"build": "rollup --config rollup.config.js",
15-
"test": "echo \"See the README: https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts#how-to-run-tests\" && exit 1",
15+
"pretest": "rollup --config rollup.config.test.js",
16+
"test": "karma start",
1617
"lint": "eslint app/javascript"
1718
},
1819
"repository": {
@@ -31,8 +32,16 @@
3132
},
3233
"homepage": "https://rubyonrails.org/",
3334
"devDependencies": {
35+
"@rollup/plugin-commonjs": "^19.0.1",
36+
"@rollup/plugin-node-resolve": "^11.0.1",
3437
"eslint": "^4.19.1",
3538
"eslint-plugin-import": "^2.23.4",
39+
"jquery": "^2.2.0",
40+
"karma": "^3.1.1",
41+
"karma-chrome-launcher": "^2.2.0",
42+
"karma-qunit": "^2.1.0",
43+
"karma-sauce-launcher": "^1.2.0",
44+
"qunit": "^2.8.0",
3645
"rollup": "^2.53.3",
3746
"rollup-plugin-terser": "^7.0.2"
3847
}

actionview/rollup.config.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import commonjs from "@rollup/plugin-commonjs"
2+
import resolve from "@rollup/plugin-node-resolve"
3+
4+
export default {
5+
input: "test/ujs/src/test.js",
6+
7+
output: {
8+
file: "test/ujs/compiled/test.js",
9+
format: "iife"
10+
},
11+
12+
plugins: [
13+
resolve(),
14+
commonjs()
15+
]
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
document.addEventListener('rails:attachBindings', function() {
2+
window.Rails.linkClickSelector += ', a[data-custom-remote-link]';
3+
// Hijacks link click before ujs binds any handlers
4+
// This is only used for ctrl-clicking test on remote links
5+
window.Rails.delegate(document, '#qunit-fixture a', 'click', function(e) {
6+
e.preventDefault();
7+
});
8+
});

actionview/test/ujs/public/test/call-ajax.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
(function() {
1+
import $ from 'jquery'
2+
import Rails from "../../../../app/javascript/rails-ujs/index"
23

34
QUnit.module('call-ajax', {
45
beforeEach: function() {
@@ -24,5 +25,3 @@ QUnit.test('call ajax without "ajax:beforeSend"', function(assert) {
2425

2526
link.triggerNative('click')
2627
})
27-
28-
})()

0 commit comments

Comments
 (0)