Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/blockly/tests/browser/test/test_setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While testing the browser tests to ensure my changes worked, I kept getting failures on focus manager tests. This addresses that by stubbing out the window's focus state, making it so the tests no longer fail if the chrome window is not currently focused.

driver.setWindowSize(800, 600);
driver.setViewport({width: 800, height: 600});
return driver;
Expand Down
10 changes: 2 additions & 8 deletions packages/blockly/tests/browser/test/toolbox_drag_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions packages/blockly/tests/mocha/node-setup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 51 additions & 0 deletions packages/blockly/tests/scripts/bundle_block_test.mjs
Original file line number Diff line number Diff line change
@@ -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<string>} 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;
}
Loading