diff --git a/packages/blockly/tests/browser/test/test_setup.mjs b/packages/blockly/tests/browser/test/test_setup.mjs index 341a99ab3d1..dd6c27eb7c1 100644 --- a/packages/blockly/tests/browser/test/test_setup.mjs +++ b/packages/blockly/tests/browser/test/test_setup.mjs @@ -62,6 +62,13 @@ export async function driverSetup() { // Use Selenium to bring up the page console.log('Starting webdriverio...'); driver = await webdriverio.remote(options); + // Chrome only fires focus events while the page has OS focus, so tests that + // rely on them fail whenever another window is in front. Emulate page focus so + // the outcome doesn't depend on which window is active. + const puppeteer = await driver.getPuppeteer(); + const [page] = await puppeteer.pages(); + const session = await page.createCDPSession(); + await session.send('Emulation.setFocusEmulationEnabled', {enabled: true}); driver.setWindowSize(800, 600); driver.setViewport({width: 800, height: 600}); return driver; diff --git a/packages/blockly/tests/browser/test/toolbox_drag_test.mjs b/packages/blockly/tests/browser/test/toolbox_drag_test.mjs index d7fbc5c87b2..bfbf1d2103a 100644 --- a/packages/blockly/tests/browser/test/toolbox_drag_test.mjs +++ b/packages/blockly/tests/browser/test/toolbox_drag_test.mjs @@ -9,9 +9,8 @@ */ import * as chai from 'chai'; -import {readFileSync} from 'fs'; -import {createRequire} from 'module'; import {Key} from 'webdriverio'; +import {bundleBlockTest} from '../../scripts/bundle_block_test.mjs'; import { getBlockTypeFromCategory, getCategory, @@ -21,12 +20,7 @@ import { testSetup, } from './test_setup.mjs'; -const require = createRequire(import.meta.url); - -const blockTestSource = readFileSync( - require.resolve('@blockly/block-test'), - 'utf8', -); +const blockTestSource = await bundleBlockTest(); /** * Load the test blocks into the playground page and swap in their toolbox. diff --git a/packages/blockly/tests/mocha/node-setup.mjs b/packages/blockly/tests/mocha/node-setup.mjs index f7e48810faa..d48a0ab165a 100644 --- a/packages/blockly/tests/mocha/node-setup.mjs +++ b/packages/blockly/tests/mocha/node-setup.mjs @@ -18,9 +18,9 @@ import {config as chaiConfig} from 'chai'; import {JSDOM} from 'jsdom'; import {readFileSync} from 'node:fs'; -import {createRequire} from 'node:module'; import vm from 'node:vm'; import sinon from 'sinon'; +import {bundleBlockTest} from '../scripts/bundle_block_test.mjs'; import {installFixtures} from './test_helpers/dom_fixtures.js'; import { HEADLESS_VIEWPORT_SIZE, @@ -191,10 +191,7 @@ vm.runInThisContext(readFileSync(`${buildDir}/msg/en.js`, 'utf8')); // Register the shared test blocks (test_fields_*, test_dropdowns_*, etc.) that // many suites rely on. -const require = createRequire(import.meta.url); -vm.runInThisContext( - readFileSync(require.resolve('@blockly/block-test'), 'utf8'), -); +vm.runInThisContext(await bundleBlockTest()); // Install the shared DOM fixtures now that Blockly is loaded. installFixtures(window.document); diff --git a/packages/blockly/tests/scripts/bundle_block_test.mjs b/packages/blockly/tests/scripts/bundle_block_test.mjs new file mode 100644 index 00000000000..d7947d313ad --- /dev/null +++ b/packages/blockly/tests/scripts/bundle_block_test.mjs @@ -0,0 +1,51 @@ +/** + * @license + * Copyright 2026 Raspberry Pi Foundation + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @fileoverview Bundles `@blockly/block-test` for test harnesses that load + * Blockly as a global rather than importing it. + */ + +import {build} from 'esbuild'; + +/** + * Bundles `@blockly/block-test` into a classic script. + * + * The bundle uses the global Blockly rather than including its own copy, so + * the test blocks register with the Blockly the tests run against. Running it + * also exposes toolboxTestBlocks and toolboxTestBlocksInit on globalThis. + * @returns {Promise} The source of the bundled script. + */ +export async function bundleBlockTest() { + const {outputFiles} = await build({ + stdin: { + contents: ` + import {toolboxTestBlocks, toolboxTestBlocksInit} from '@blockly/block-test'; + globalThis.toolboxTestBlocks = toolboxTestBlocks; + globalThis.toolboxTestBlocksInit = toolboxTestBlocksInit; + `, + resolveDir: import.meta.dirname, + }, + bundle: true, + write: false, + format: 'iife', + plugins: [ + { + name: 'global-blockly', + setup(build) { + build.onResolve({filter: /^blockly\/core$/}, () => ({ + path: 'blockly/core', + namespace: 'global-blockly', + })); + build.onLoad({filter: /.*/, namespace: 'global-blockly'}, () => ({ + contents: 'module.exports = globalThis.Blockly;', + })); + }, + }, + ], + }); + return outputFiles[0].text; +}