Skip to content

Commit 0a07fd7

Browse files
author
Takashi Kato
committed
Support live reload. update mocha to v11.1.0
1 parent 0e2fea1 commit 0a07fd7

File tree

4 files changed

+104
-21
lines changed

4 files changed

+104
-21
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export function changeFavicon(failures) {
2+
const links = document.getElementsByTagName('link')
3+
4+
for (let i=0; i<links.length; i++) {
5+
let link = links[i];
6+
if (link.rel == 'icon') {
7+
const icon = failures > 0 ? favicon('red')
8+
: favicon('green')
9+
link.remove()
10+
const newlink = document.createElement("link");
11+
newlink.rel = 'icon';
12+
newlink.href = icon;
13+
newlink.type = 'image/svg+xml';
14+
const head = document.getElementsByTagName("head")[0];
15+
head.appendChild(newlink);
16+
17+
return;
18+
}
19+
}
20+
}
21+
22+
function favicon(color, count) {
23+
const icon = `<?xml version="1.0" encoding="UTF-8"?>
24+
<svg width="100" height="100" version="1.1" xmlns="http://www.w3.org/2000/svg">
25+
<style>circle {
26+
fill: ${color};
27+
stroke: ${color};
28+
stroke-width: 3px;
29+
}
30+
</style>
31+
<circle cx="50" cy="50" r="47"/>
32+
</svg>`
33+
const base64Svg = btoa(unescape(encodeURIComponent(icon)));
34+
35+
return `data:image/svg+xml;base64,${base64Svg}`;
36+
}

app/views/importmap_mocha/test/index.html.erb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<% if Rails.application.config.importmap_mocha_scripts.size > 0 %>
77
<%= javascript_include_tag *Rails.application.config.importmap_mocha_scripts %>
88
<% end %>
9-
<%= favicon_link_tag %>
9+
<link rel="icon" href="data:image/png;base64,iVBORw0KGgo=">
1010
<%= javascript_include_tag 'mocha' %>
1111
<%= stylesheet_link_tag 'mocha' %>
1212
<%= javascript_importmap_tags 'importmap_mocha' %>
@@ -25,8 +25,10 @@
2525
</div>
2626
</div>
2727
</div>
28-
<script type="module">
29-
mocha.run();
30-
</script>
28+
<script type="module">
29+
import { changeFavicon } from 'importmap_mocha'
30+
31+
mocha.run(changeFavicon)
32+
</script>
3133
</body>
3234
</html>

importmap_mocha-rails.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ Gem::Specification.new do |spec|
2020
Dir['app/**/*', 'config/**/*', 'lib/**/*', 'vendor/**/*', 'MIT-LICENSE', 'Rakefile', 'README.md']
2121
end
2222

23-
spec.add_dependency 'importmap-rails', '~> 2.0.0'
23+
spec.add_dependency 'importmap-rails', '>= 2.0.0'
2424
end

vendor/javascripts/mocha.js

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// mocha@10.8.2 in javascript ES2018
1+
// mocha@11.1.0 in javascript ES2018
22
(function (global, factory) {
33
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
44
typeof define === 'function' && define.amd ? define(factory) :
@@ -11652,6 +11652,13 @@
1165211652

1165311653
return _breakCircularDeps(inputObj);
1165411654
};
11655+
11656+
/**
11657+
* Checks if provided input can be parsed as a JavaScript Number.
11658+
*/
11659+
exports.isNumeric = input => {
11660+
return !isNaN(parseFloat(input));
11661+
};
1165511662
}(utils$3));
1165611663

1165711664
var _nodeResolve_empty = {};
@@ -13771,7 +13778,7 @@
1377113778
var hook = this._createHook(title, fn);
1377213779
this._beforeAll.push(hook);
1377313780
this.emit(constants.EVENT_SUITE_ADD_HOOK_BEFORE_ALL, hook);
13774-
return this;
13781+
return hook;
1377513782
};
1377613783

1377713784
/**
@@ -13795,7 +13802,7 @@
1379513802
var hook = this._createHook(title, fn);
1379613803
this._afterAll.push(hook);
1379713804
this.emit(constants.EVENT_SUITE_ADD_HOOK_AFTER_ALL, hook);
13798-
return this;
13805+
return hook;
1379913806
};
1380013807

1380113808
/**
@@ -13819,7 +13826,7 @@
1381913826
var hook = this._createHook(title, fn);
1382013827
this._beforeEach.push(hook);
1382113828
this.emit(constants.EVENT_SUITE_ADD_HOOK_BEFORE_EACH, hook);
13822-
return this;
13829+
return hook;
1382313830
};
1382413831

1382513832
/**
@@ -13843,7 +13850,7 @@
1384313850
var hook = this._createHook(title, fn);
1384413851
this._afterEach.push(hook);
1384513852
this.emit(constants.EVENT_SUITE_ADD_HOOK_AFTER_EACH, hook);
13846-
return this;
13853+
return hook;
1384713854
};
1384813855

1384913856
/**
@@ -15296,11 +15303,11 @@
1529615303
* @public
1529715304
* @example
1529815305
* // this reporter needs proper object references when run in parallel mode
15299-
* class MyReporter() {
15306+
* class MyReporter {
1530015307
* constructor(runner) {
15301-
* this.runner.linkPartialObjects(true)
15308+
* runner.linkPartialObjects(true)
1530215309
* .on(EVENT_SUITE_BEGIN, suite => {
15303-
// this Suite may be the same object...
15310+
* // this Suite may be the same object...
1530415311
* })
1530515312
* .on(EVENT_TEST_BEGIN, test => {
1530615313
* // ...as the `test.parent` property
@@ -18593,7 +18600,7 @@
1859318600
* @param {Function} fn
1859418601
*/
1859518602
before: function (name, fn) {
18596-
suites[0].beforeAll(name, fn);
18603+
return suites[0].beforeAll(name, fn);
1859718604
},
1859818605

1859918606
/**
@@ -18603,7 +18610,7 @@
1860318610
* @param {Function} fn
1860418611
*/
1860518612
after: function (name, fn) {
18606-
suites[0].afterAll(name, fn);
18613+
return suites[0].afterAll(name, fn);
1860718614
},
1860818615

1860918616
/**
@@ -18613,7 +18620,7 @@
1861318620
* @param {Function} fn
1861418621
*/
1861518622
beforeEach: function (name, fn) {
18616-
suites[0].beforeEach(name, fn);
18623+
return suites[0].beforeEach(name, fn);
1861718624
},
1861818625

1861918626
/**
@@ -18623,7 +18630,7 @@
1862318630
* @param {Function} fn
1862418631
*/
1862518632
afterEach: function (name, fn) {
18626-
suites[0].afterEach(name, fn);
18633+
return suites[0].afterEach(name, fn);
1862718634
},
1862818635

1862918636
suite: {
@@ -19201,7 +19208,7 @@
1920119208
};
1920219209

1920319210
var name = "mocha";
19204-
var version = "10.8.2";
19211+
var version = "11.1.0";
1920519212
var homepage = "https://mochajs.org/";
1920619213
var notifyLogo = "https://ibin.co/4QuRuGjXvl36.png";
1920719214
var require$$17 = {
@@ -19407,7 +19414,9 @@
1940719414
.ui(options.ui)
1940819415
.reporter(
1940919416
options.reporter,
19410-
options.reporterOption || options.reporterOptions // for backwards compatibility
19417+
options['reporter-option'] ||
19418+
options.reporterOption ||
19419+
options.reporterOptions // for backwards compatibility
1941119420
)
1941219421
.slow(options.slow)
1941319422
.global(options.global);
@@ -20548,6 +20557,42 @@
2054820557
/* eslint no-unused-vars: off */
2054920558
/* eslint-env commonjs */
2055020559

20560+
// https://qiita.com/yamadashy/items/86702dfb3c572b4c5514
20561+
const setZeroTimeout = (function(global) {
20562+
const timeouts = [];
20563+
const messageName = "zero-timeout-message";
20564+
20565+
if (typeof global === 'undefined') return;
20566+
20567+
function handleMessage(event) {
20568+
if (event.source == global && event.data == messageName) {
20569+
if (event.stopPropagation) {
20570+
event.stopPropagation();
20571+
}
20572+
if (timeouts.length) {
20573+
timeouts.shift()();
20574+
}
20575+
}
20576+
}
20577+
20578+
if (global.postMessage) {
20579+
if (global.addEventListener) {
20580+
global.addEventListener("message", handleMessage, true);
20581+
} else if (global.attachEvent) {
20582+
global.attachEvent("onmessage", handleMessage);
20583+
}
20584+
20585+
return function (fn) {
20586+
timeouts.push(fn);
20587+
global.postMessage(messageName, "*");
20588+
}
20589+
} else {
20590+
return function () {
20591+
setTimeout$1(fn, 0);
20592+
}
20593+
}
20594+
}(window));
20595+
2055120596
/**
2055220597
* Shim process.stdout.
2055320598
*/
@@ -20645,7 +20690,7 @@
2064520690
immediateQueue.shift()();
2064620691
}
2064720692
if (immediateQueue.length) {
20648-
immediateTimeout = setTimeout$1(timeslice, 0);
20693+
immediateTimeout = setZeroTimeout(timeslice);
2064920694
} else {
2065020695
immediateTimeout = null;
2065120696
}
@@ -20658,7 +20703,7 @@
2065820703
Mocha.Runner.immediately = function (callback) {
2065920704
immediateQueue.push(callback);
2066020705
if (!immediateTimeout) {
20661-
immediateTimeout = setTimeout$1(timeslice, 0);
20706+
immediateTimeout = setZeroTimeout(timeslice);
2066220707
}
2066320708
};
2066420709

0 commit comments

Comments
 (0)