|
| 1 | +// Setup for running TKO specs in a real browser under Mocha. |
| 2 | +// Counterpart to vitest-setup.js. Loaded as the first import of |
| 3 | +// the test bundle (tko.io/scripts/bundle-tests.mjs). |
| 4 | +// |
| 5 | +// Assumes `mocha.setup('bdd')` already ran (so `before` / `after` |
| 6 | +// / `beforeEach` / `afterEach` are global) and `globalThis.ko` |
| 7 | +// was set by the bundled IIFE in /tests/source/setup.js, which |
| 8 | +// this module is imported from. |
| 9 | + |
| 10 | +import * as chai from 'chai' |
| 11 | +import sinon from 'sinon' |
| 12 | +// Register punctuation filters on shared `@tko/utils` options so |
| 13 | +// specs that construct Parsers directly (`new Parser().parse('x | tail')`) |
| 14 | +// can resolve them. The builder registers the same filters at page |
| 15 | +// startup but on a module-local options reference — not this one. |
| 16 | +import { filters as punctuationFilters } from '@tko/filter.punches' |
| 17 | +import { options as sharedOptions } from '@tko/utils' |
| 18 | + |
| 19 | +globalThis.chai = chai |
| 20 | +globalThis.expect = chai.expect |
| 21 | +globalThis.sinon = sinon |
| 22 | +globalThis.isHappyDom = () => false |
| 23 | + |
| 24 | +sharedOptions.filters = Object.assign(sharedOptions.filters || {}, punctuationFilters) |
| 25 | + |
| 26 | +// mocha-test-helpers wires root beforeEach/afterEach hooks, so it |
| 27 | +// must be imported after `mocha.setup('bdd')` ran. |
| 28 | +import './mocha-test-helpers.js' |
| 29 | + |
| 30 | +// Disable the 25ms JSX cleanup timer so it can't race test teardown. |
| 31 | +before(() => { |
| 32 | + if (globalThis.ko?.options) { |
| 33 | + globalThis.ko.options.jsxCleanBatchSize = 0 |
| 34 | + } |
| 35 | +}) |
| 36 | + |
| 37 | +// Iframe focus-event polyfill. |
| 38 | +// |
| 39 | +// Chromium refuses to grant programmatic `iframe.contentWindow.focus()` |
| 40 | +// true system focus from a parent that already holds focus — the |
| 41 | +// iframe never passes `document.hasFocus() === true`, so `focusin` |
| 42 | +// / `focusout` are suppressed when specs call `element.focus()` |
| 43 | +// inside. The `hasfocus` binding observes those events (not |
| 44 | +// `document.activeElement`), so without this patch those specs |
| 45 | +// fail under both Playwright and a real Chrome tab. |
| 46 | +// |
| 47 | +// Wrap `focus`/`blur` to dispatch the missing events synchronously |
| 48 | +// after the native call. If the browser DOES regain system focus |
| 49 | +// and fires them too, observers see duplicates — harmless for |
| 50 | +// these specs (state-checking, not call-count). |
| 51 | +// |
| 52 | +// Scope-guarded to iframes (`window.parent !== window`) so the |
| 53 | +// parent page is never patched. |
| 54 | +// |
| 55 | +// Refs: https://github.com/jsdom/jsdom/pull/2996 (same shape as |
| 56 | +// this wrap), https://html.spec.whatwg.org/multipage/interaction.html#focusing-elements. |
| 57 | +if (window.parent !== window && !HTMLElement.prototype.__tkoFocusPatched) { |
| 58 | + const HE = HTMLElement.prototype |
| 59 | + HE.__tkoFocusPatched = true |
| 60 | + const origFocus = HE.focus |
| 61 | + const origBlur = HE.blur |
| 62 | + HE.focus = function (...args) { |
| 63 | + const wasActive = this.ownerDocument.activeElement |
| 64 | + origFocus.apply(this, args) |
| 65 | + if (this.ownerDocument.activeElement === this && wasActive !== this) { |
| 66 | + this.dispatchEvent(new FocusEvent('focus', { bubbles: false, relatedTarget: wasActive })) |
| 67 | + this.dispatchEvent(new FocusEvent('focusin', { bubbles: true, relatedTarget: wasActive })) |
| 68 | + } |
| 69 | + } |
| 70 | + HE.blur = function (...args) { |
| 71 | + const wasActive = this.ownerDocument.activeElement |
| 72 | + origBlur.apply(this, args) |
| 73 | + if (wasActive === this && this.ownerDocument.activeElement !== this) { |
| 74 | + this.dispatchEvent(new FocusEvent('blur', { bubbles: false, relatedTarget: this.ownerDocument.activeElement })) |
| 75 | + this.dispatchEvent(new FocusEvent('focusout', { bubbles: true, relatedTarget: this.ownerDocument.activeElement })) |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Unscoped sinon fakes (`sinon.spy(obj,'m')`, `sinon.useFakeTimers()`) |
| 81 | +// leak across specs if not restored, producing bogus call-count |
| 82 | +// diffs or "Can't install fake timers twice". `sinon.restore()` is |
| 83 | +// a no-op for sandbox-scoped fakes. Vitest isolates per-file so |
| 84 | +// doesn't need this hook. |
| 85 | +afterEach(() => { |
| 86 | + if (globalThis.sinon?.restore) globalThis.sinon.restore() |
| 87 | +}) |
| 88 | + |
| 89 | +// Vitest-style context-arg shim. |
| 90 | +// |
| 91 | +// Specs written `function (ctx) { if (isHappyDom()) return ctx.skip(…) }` |
| 92 | +// look like Mocha done-callback specs (`fn.length === 1`) and time |
| 93 | +// out after ~10s because they never call done. Wrap `it` to detect |
| 94 | +// the ctx shape (uses `.skip(...)` and never calls `done(`) and |
| 95 | +// invoke with a synthetic `{ skip }` while hiding arity from Mocha. |
| 96 | +{ |
| 97 | + const wrap = orig => |
| 98 | + function (name, fn) { |
| 99 | + if (typeof fn === 'function' && fn.length === 1) { |
| 100 | + const src = fn.toString() |
| 101 | + const ctxStyle = /\.skip\s*\(/.test(src) && !/\bdone\s*\(/.test(src) |
| 102 | + if (ctxStyle) { |
| 103 | + const wrapped = function () { |
| 104 | + return fn.call(this, { skip: reason => this.skip(reason) }) |
| 105 | + } |
| 106 | + Object.defineProperty(wrapped, 'length', { value: 0 }) |
| 107 | + return orig.call(this, name, wrapped) |
| 108 | + } |
| 109 | + } |
| 110 | + return orig.apply(this, arguments) |
| 111 | + } |
| 112 | + const origIt = globalThis.it |
| 113 | + const wrappedIt = wrap(origIt) |
| 114 | + wrappedIt.only = wrap(origIt.only) |
| 115 | + wrappedIt.skip = origIt.skip |
| 116 | + globalThis.it = wrappedIt |
| 117 | +} |
0 commit comments