From 504c7a1ae6cdab7f1ad988e11f149c34e4c410c3 Mon Sep 17 00:00:00 2001 From: Alex Rudenko Date: Fri, 9 Sep 2022 11:12:18 +0200 Subject: [PATCH] chore: implement custom mocha interface for the runner (#8929) Instead of checking skipped tests in mocha-utils this PR implements a custom mocha interface for better flexibility when skipping tests. That should allow skipping tests without running before and after hooks. --- test/src/CDPSession.spec.ts | 1 - test/src/EventEmitter.spec.ts | 1 - test/src/NetworkManager.spec.ts | 1 - test/src/TargetManager.spec.ts | 1 - test/src/accessibility.spec.ts | 1 - test/src/ariaqueryhandler.spec.ts | 1 - test/src/browser.spec.ts | 1 - test/src/browsercontext.spec.ts | 1 - test/src/chromiumonly.spec.ts | 1 - test/src/click.spec.ts | 3 +- test/src/cookies.spec.ts | 1 - test/src/coverage.spec.ts | 5 +- test/src/defaultbrowsercontext.spec.ts | 1 - test/src/dialog.spec.ts | 1 - test/src/drag-and-drop.spec.ts | 1 - test/src/elementhandle.spec.ts | 1 - test/src/emulation.spec.ts | 1 - test/src/evaluation.spec.ts | 5 +- test/src/fixtures.spec.ts | 1 - test/src/frame.spec.ts | 1 - test/src/headful.spec.ts | 3 +- test/src/idle_override.spec.ts | 1 - test/src/ignorehttpserrors.spec.ts | 1 - test/src/input.spec.ts | 1 - test/src/jshandle.spec.ts | 1 - test/src/keyboard.spec.ts | 1 - test/src/launcher.spec.ts | 1 - test/src/mocha-utils.ts | 34 +---- test/src/mouse.spec.ts | 1 - test/src/navigation.spec.ts | 1 - test/src/network.spec.ts | 1 - test/src/oopif.spec.ts | 1 - test/src/page.spec.ts | 3 +- test/src/proxy.spec.ts | 1 - test/src/queryhandler.spec.ts | 1 - test/src/queryselector.spec.ts | 1 - .../requestinterception-experimental.spec.ts | 1 - test/src/requestinterception.spec.ts | 1 - test/src/screenshot.spec.ts | 1 - test/src/target.spec.ts | 1 - test/src/touchscreen.spec.ts | 1 - test/src/tracing.spec.ts | 1 - test/src/waittask.spec.ts | 1 - test/src/worker.spec.ts | 1 - utils/mochaRunner/src/interface.ts | 119 ++++++++++++++++++ utils/mochaRunner/src/main.ts | 34 ++++- utils/mochaRunner/src/test.ts | 16 +++ utils/mochaRunner/src/types.ts | 16 +++ utils/mochaRunner/src/utils.ts | 24 ++-- 49 files changed, 203 insertions(+), 97 deletions(-) create mode 100644 utils/mochaRunner/src/interface.ts diff --git a/test/src/CDPSession.spec.ts b/test/src/CDPSession.spec.ts index a45ff2c11a6..9f82f862343 100644 --- a/test/src/CDPSession.spec.ts +++ b/test/src/CDPSession.spec.ts @@ -22,7 +22,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import {isErrorLike} from '../../lib/cjs/puppeteer/util/ErrorLike.js'; -import {it} from './mocha-utils.js'; describe('Target.createCDPSession', function () { setupTestBrowserHooks(); diff --git a/test/src/EventEmitter.spec.ts b/test/src/EventEmitter.spec.ts index b62e932e6e1..67e1ca610d8 100644 --- a/test/src/EventEmitter.spec.ts +++ b/test/src/EventEmitter.spec.ts @@ -17,7 +17,6 @@ import {EventEmitter} from '../../lib/cjs/puppeteer/common/EventEmitter.js'; import sinon from 'sinon'; import expect from 'expect'; -import {it} from './mocha-utils.js'; describe('EventEmitter', () => { let emitter: EventEmitter; diff --git a/test/src/NetworkManager.spec.ts b/test/src/NetworkManager.spec.ts index f0d8ee795eb..5602a5de65b 100644 --- a/test/src/NetworkManager.spec.ts +++ b/test/src/NetworkManager.spec.ts @@ -23,7 +23,6 @@ import {HTTPRequest} from '../../lib/cjs/puppeteer/common/HTTPRequest.js'; import {EventEmitter} from '../../lib/cjs/puppeteer/common/EventEmitter.js'; import {Frame} from '../../lib/cjs/puppeteer/common/Frame.js'; import {HTTPResponse} from '../../lib/cjs/puppeteer/common/HTTPResponse.js'; -import {it} from './mocha-utils.js'; class MockCDPSession extends EventEmitter { async send(): Promise {} diff --git a/test/src/TargetManager.spec.ts b/test/src/TargetManager.spec.ts index 8e39bb12d47..e47ac9cc83b 100644 --- a/test/src/TargetManager.spec.ts +++ b/test/src/TargetManager.spec.ts @@ -16,7 +16,6 @@ import {getTestState} from './mocha-utils'; // eslint-disable-line import/extensions import utils from './utils.js'; -import {it} from './mocha-utils.js'; import expect from 'expect'; diff --git a/test/src/accessibility.spec.ts b/test/src/accessibility.spec.ts index 412f5bedba3..c20b137b98e 100644 --- a/test/src/accessibility.spec.ts +++ b/test/src/accessibility.spec.ts @@ -22,7 +22,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Accessibility', function () { setupTestBrowserHooks(); diff --git a/test/src/ariaqueryhandler.spec.ts b/test/src/ariaqueryhandler.spec.ts index a34831c3f85..f6c11e83b69 100644 --- a/test/src/ariaqueryhandler.spec.ts +++ b/test/src/ariaqueryhandler.spec.ts @@ -24,7 +24,6 @@ import { import {ElementHandle} from '../../lib/cjs/puppeteer/common/ElementHandle.js'; import utils from './utils.js'; import assert from 'assert'; -import {it} from './mocha-utils.js'; describe('AriaQueryHandler', () => { setupTestBrowserHooks(); diff --git a/test/src/browser.spec.ts b/test/src/browser.spec.ts index 1c06124cc5e..375b3dba934 100644 --- a/test/src/browser.spec.ts +++ b/test/src/browser.spec.ts @@ -16,7 +16,6 @@ import expect from 'expect'; import {getTestState, setupTestBrowserHooks} from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Browser specs', function () { setupTestBrowserHooks(); diff --git a/test/src/browsercontext.spec.ts b/test/src/browsercontext.spec.ts index ad4d3683f75..09eb0e4e709 100644 --- a/test/src/browsercontext.spec.ts +++ b/test/src/browsercontext.spec.ts @@ -17,7 +17,6 @@ import expect from 'expect'; import {getTestState, setupTestBrowserHooks} from './mocha-utils.js'; import {waitEvent} from './utils.js'; -import {it} from './mocha-utils.js'; describe('BrowserContext', function () { setupTestBrowserHooks(); diff --git a/test/src/chromiumonly.spec.ts b/test/src/chromiumonly.spec.ts index 8bf9f00ef81..93a670f1f2e 100644 --- a/test/src/chromiumonly.spec.ts +++ b/test/src/chromiumonly.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Chromium-Specific Launcher tests', function () { describe('Puppeteer.launch |browserURL| option', function () { diff --git a/test/src/click.spec.ts b/test/src/click.spec.ts index 5de40fef24c..b9e7f47a00b 100644 --- a/test/src/click.spec.ts +++ b/test/src/click.spec.ts @@ -21,7 +21,6 @@ import { setupTestBrowserHooks, } from './mocha-utils.js'; import utils from './utils.js'; -import {it} from './mocha-utils.js'; describe('Page.click', function () { setupTestBrowserHooks(); @@ -419,7 +418,7 @@ describe('Page.click', function () { ).toBe('Clicked'); }); // @see https://github.com/puppeteer/puppeteer/issues/4110 - xit('should click the button with fixed position inside an iframe', async () => { + it.skip('should click the button with fixed position inside an iframe', async () => { const {page, server} = getTestState(); await page.goto(server.EMPTY_PAGE); diff --git a/test/src/cookies.spec.ts b/test/src/cookies.spec.ts index f493271319e..6cf1ba1686b 100644 --- a/test/src/cookies.spec.ts +++ b/test/src/cookies.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Cookie specs', () => { setupTestBrowserHooks(); diff --git a/test/src/coverage.spec.ts b/test/src/coverage.spec.ts index 930f33cd4b0..2f8f2e7a1de 100644 --- a/test/src/coverage.spec.ts +++ b/test/src/coverage.spec.ts @@ -20,7 +20,6 @@ import { setupTestPageAndContextHooks, setupTestBrowserHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Coverage specs', function () { describe('JSCoverage', function () { @@ -134,7 +133,7 @@ describe('Coverage specs', function () { ).toBeGolden('jscoverage-involved.txt'); }); // @see https://crbug.com/990945 - xit('should not hang when there is a debugger statement', async () => { + it.skip('should not hang when there is a debugger statement', async () => { const {page, server} = getTestState(); await page.coverage.startJSCoverage(); @@ -190,7 +189,7 @@ describe('Coverage specs', function () { }); }); // @see https://crbug.com/990945 - xit('should not hang when there is a debugger statement', async () => { + it.skip('should not hang when there is a debugger statement', async () => { const {page, server} = getTestState(); await page.coverage.startJSCoverage(); diff --git a/test/src/defaultbrowsercontext.spec.ts b/test/src/defaultbrowsercontext.spec.ts index f9ff849b119..cbdf34f634f 100644 --- a/test/src/defaultbrowsercontext.spec.ts +++ b/test/src/defaultbrowsercontext.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('DefaultBrowserContext', function () { setupTestBrowserHooks(); diff --git a/test/src/dialog.spec.ts b/test/src/dialog.spec.ts index 890437648ec..e4489004cdb 100644 --- a/test/src/dialog.spec.ts +++ b/test/src/dialog.spec.ts @@ -21,7 +21,6 @@ import { setupTestPageAndContextHooks, setupTestBrowserHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Page.Events.Dialog', function () { setupTestBrowserHooks(); diff --git a/test/src/drag-and-drop.spec.ts b/test/src/drag-and-drop.spec.ts index 93ae07856bf..1ce367bb896 100644 --- a/test/src/drag-and-drop.spec.ts +++ b/test/src/drag-and-drop.spec.ts @@ -20,7 +20,6 @@ import { setupTestPageAndContextHooks, setupTestBrowserHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Input.drag', function () { setupTestBrowserHooks(); diff --git a/test/src/elementhandle.spec.ts b/test/src/elementhandle.spec.ts index 0d64cd48bfe..928145f7679 100644 --- a/test/src/elementhandle.spec.ts +++ b/test/src/elementhandle.spec.ts @@ -22,7 +22,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; import utils from './utils.js'; diff --git a/test/src/emulation.spec.ts b/test/src/emulation.spec.ts index c275e12d24a..45548a373af 100644 --- a/test/src/emulation.spec.ts +++ b/test/src/emulation.spec.ts @@ -21,7 +21,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Emulation', () => { setupTestBrowserHooks(); diff --git a/test/src/evaluation.spec.ts b/test/src/evaluation.spec.ts index b68cfdc1f2c..4f34e2d8079 100644 --- a/test/src/evaluation.spec.ts +++ b/test/src/evaluation.spec.ts @@ -21,7 +21,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; const bigint = typeof BigInt !== 'undefined'; @@ -38,7 +37,7 @@ describe('Evaluation specs', function () { }); expect(result).toBe(21); }); - (bigint ? it : xit)('should transfer BigInt', async () => { + (bigint ? it : it.skip)('should transfer BigInt', async () => { const {page} = getTestState(); const result = await page.evaluate((a: bigint) => { @@ -259,7 +258,7 @@ describe('Evaluation specs', function () { expect(result).not.toBe(object); expect(result).toEqual(object); }); - (bigint ? it : xit)('should return BigInt', async () => { + (bigint ? it : it.skip)('should return BigInt', async () => { const {page} = getTestState(); const result = await page.evaluate(() => { diff --git a/test/src/fixtures.spec.ts b/test/src/fixtures.spec.ts index 4d32f732855..ee87e7139af 100644 --- a/test/src/fixtures.spec.ts +++ b/test/src/fixtures.spec.ts @@ -20,7 +20,6 @@ import expect from 'expect'; import {getTestState} from './mocha-utils.js'; import path from 'path'; -import {it} from './mocha-utils.js'; describe('Fixtures', function () { it('dumpio option should work with pipe option', async () => { diff --git a/test/src/frame.spec.ts b/test/src/frame.spec.ts index ac010d570f5..d8769a74a8d 100644 --- a/test/src/frame.spec.ts +++ b/test/src/frame.spec.ts @@ -23,7 +23,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import utils, {dumpFrames} from './utils.js'; -import {it} from './mocha-utils.js'; describe('Frame specs', function () { setupTestBrowserHooks(); diff --git a/test/src/headful.spec.ts b/test/src/headful.spec.ts index 6ba729ab3f0..9cd9c4fb026 100644 --- a/test/src/headful.spec.ts +++ b/test/src/headful.spec.ts @@ -25,7 +25,6 @@ import { PuppeteerNode, } from '../../lib/cjs/puppeteer/node/Puppeteer.js'; import {getTestState} from './mocha-utils.js'; -import {it} from './mocha-utils.js'; const rmAsync = promisify(rimraf); const mkdtempAsync = promisify(fs.mkdtemp); @@ -244,7 +243,7 @@ describe('headful tests', function () { expect(cookie).toBe('foo=true'); }); // TODO: Support OOOPIF. @see https://github.com/puppeteer/puppeteer/issues/2548 - xit('OOPIF: should report google.com frame', async () => { + it.skip('OOPIF: should report google.com frame', async () => { const {server, puppeteer} = getTestState(); // https://google.com is isolated by default in Chromium embedder. diff --git a/test/src/idle_override.spec.ts b/test/src/idle_override.spec.ts index 1dae41d0c91..4402f934c5d 100644 --- a/test/src/idle_override.spec.ts +++ b/test/src/idle_override.spec.ts @@ -21,7 +21,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Emulate idle state', () => { setupTestBrowserHooks(); diff --git a/test/src/ignorehttpserrors.spec.ts b/test/src/ignorehttpserrors.spec.ts index f4f5e6da8a3..493452de348 100644 --- a/test/src/ignorehttpserrors.spec.ts +++ b/test/src/ignorehttpserrors.spec.ts @@ -23,7 +23,6 @@ import { import {Page} from '../../lib/cjs/puppeteer/common/Page.js'; import {HTTPResponse} from '../../lib/cjs/puppeteer/common/HTTPResponse.js'; import {getTestState} from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('ignoreHTTPSErrors', function () { /* Note that this test creates its own browser rather than use diff --git a/test/src/input.spec.ts b/test/src/input.spec.ts index c06f4dd0b46..925125a2c74 100644 --- a/test/src/input.spec.ts +++ b/test/src/input.spec.ts @@ -21,7 +21,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; const FILE_TO_UPLOAD = path.join(__dirname, '/../assets/file-to-upload.txt'); diff --git a/test/src/jshandle.spec.ts b/test/src/jshandle.spec.ts index a97925f7781..a54b59a6fbf 100644 --- a/test/src/jshandle.spec.ts +++ b/test/src/jshandle.spec.ts @@ -21,7 +21,6 @@ import { setupTestPageAndContextHooks, shortWaitForArrayToHaveAtLeastNElements, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('JSHandle', function () { setupTestBrowserHooks(); diff --git a/test/src/keyboard.spec.ts b/test/src/keyboard.spec.ts index 81270296340..f6d0ce0a83b 100644 --- a/test/src/keyboard.spec.ts +++ b/test/src/keyboard.spec.ts @@ -23,7 +23,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import {KeyInput} from '../../lib/cjs/puppeteer/common/USKeyboardLayout.js'; -import {it} from './mocha-utils.js'; describe('Keyboard', function () { setupTestBrowserHooks(); diff --git a/test/src/launcher.spec.ts b/test/src/launcher.spec.ts index 01e01e5cc9b..49db77320ed 100644 --- a/test/src/launcher.spec.ts +++ b/test/src/launcher.spec.ts @@ -26,7 +26,6 @@ import {Page} from '../../lib/cjs/puppeteer/common/Page.js'; import {Product} from '../../lib/cjs/puppeteer/common/Product.js'; import {getTestState, itOnlyRegularInstall} from './mocha-utils.js'; import utils from './utils.js'; -import {it} from './mocha-utils.js'; const mkdtempAsync = promisify(fs.mkdtemp); const readFileAsync = promisify(fs.readFile); diff --git a/test/src/mocha-utils.ts b/test/src/mocha-utils.ts index d21196c1e1a..487c7df0f9f 100644 --- a/test/src/mocha-utils.ts +++ b/test/src/mocha-utils.ts @@ -34,7 +34,6 @@ import puppeteer from '../../lib/cjs/puppeteer/puppeteer.js'; import {TestServer} from '../../utils/testserver/lib/index.js'; import {extendExpectWithToBeGolden} from './utils.js'; import * as Mocha from 'mocha'; -import {getTestId} from '../../utils/mochaRunner/lib/utils.js'; const setupServer = async () => { const assetsPath = path.join(__dirname, '../assets'); @@ -162,7 +161,7 @@ export const itOnlyRegularInstall = ( body: Mocha.AsyncFunc ): Mocha.Test => { if (alternativeInstall || process.env['BINARY']) { - return xit(description, body); + return it.skip(description, body); } else { return it(description, body); } @@ -297,34 +296,3 @@ export const shortWaitForArrayToHaveAtLeastNElements = async ( }); } }; - -type SyncFn = (this: Mocha.Context) => void; - -const skippedTests: string[] = process.env['PUPPETEER_SKIPPED_TEST_CONFIG'] - ? JSON.parse(process.env['PUPPETEER_SKIPPED_TEST_CONFIG']) - : []; - -function skipTestIfNeeded(test: Mocha.Test): void { - const testId = getTestId(test.file!, test.fullTitle()); - if ( - skippedTests.find(skippedTest => { - return testId.startsWith(skippedTest); - }) - ) { - try { - test.skip(); - } catch {} - } -} - -export function it(title: string, fn?: Mocha.AsyncFunc | SyncFn): Mocha.Test { - const test = Mocha.it.call(null, title, fn as any); - skipTestIfNeeded(test); - return test; -} - -it.only = function (title: string, fn?: Mocha.AsyncFunc | SyncFn): Mocha.Test { - const test = Mocha.it.only.call(null, title, fn as any); - skipTestIfNeeded(test); - return test; -}; diff --git a/test/src/mouse.spec.ts b/test/src/mouse.spec.ts index 661c2e8c2d5..01ee49bfd3a 100644 --- a/test/src/mouse.spec.ts +++ b/test/src/mouse.spec.ts @@ -21,7 +21,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import {KeyInput} from '../../lib/cjs/puppeteer/common/USKeyboardLayout.js'; -import {it} from './mocha-utils.js'; interface Dimensions { x: number; diff --git a/test/src/navigation.spec.ts b/test/src/navigation.spec.ts index 98efda2a9a5..02158ee7176 100644 --- a/test/src/navigation.spec.ts +++ b/test/src/navigation.spec.ts @@ -24,7 +24,6 @@ import { import os from 'os'; import {ServerResponse} from 'http'; import {HTTPRequest} from '../../lib/cjs/puppeteer/common/HTTPRequest.js'; -import {it} from './mocha-utils.js'; describe('navigation', function () { setupTestBrowserHooks(); diff --git a/test/src/network.spec.ts b/test/src/network.spec.ts index b71b0187f75..9cfbb35fda9 100644 --- a/test/src/network.spec.ts +++ b/test/src/network.spec.ts @@ -26,7 +26,6 @@ import { import {HTTPRequest} from '../../lib/cjs/puppeteer/common/HTTPRequest.js'; import {HTTPResponse} from '../../lib/cjs/puppeteer/common/HTTPResponse.js'; import {ServerResponse} from 'http'; -import {it} from './mocha-utils.js'; describe('network', function () { setupTestBrowserHooks(); diff --git a/test/src/oopif.spec.ts b/test/src/oopif.spec.ts index 174d3c5ae69..beb46cc18d6 100644 --- a/test/src/oopif.spec.ts +++ b/test/src/oopif.spec.ts @@ -22,7 +22,6 @@ import { BrowserContext, } from '../../lib/cjs/puppeteer/common/Browser.js'; import {Page} from '../../lib/cjs/puppeteer/common/Page.js'; -import {it} from './mocha-utils.js'; describe('OOPIF', function () { /* We use a special browser for this test as we need the --site-per-process flag */ diff --git a/test/src/page.spec.ts b/test/src/page.spec.ts index 3d59994a12a..c1f3c83cf26 100644 --- a/test/src/page.spec.ts +++ b/test/src/page.spec.ts @@ -27,7 +27,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import utils, {attachFrame, waitEvent} from './utils.js'; -import {it} from './mocha-utils.js'; describe('Page', function () { setupTestBrowserHooks(); @@ -1758,7 +1757,7 @@ describe('Page', function () { }); // @see https://github.com/puppeteer/puppeteer/issues/4840 - xit('should throw when added with content to the CSP page', async () => { + it.skip('should throw when added with content to the CSP page', async () => { const {page, server} = getTestState(); await page.goto(server.PREFIX + '/csp.html'); diff --git a/test/src/proxy.spec.ts b/test/src/proxy.spec.ts index 9aac24e4bcb..921e6274037 100644 --- a/test/src/proxy.spec.ts +++ b/test/src/proxy.spec.ts @@ -22,7 +22,6 @@ import type {Server, IncomingMessage, ServerResponse} from 'http'; import type {Browser} from '../../lib/cjs/puppeteer/common/Browser.js'; import type {AddressInfo} from 'net'; import {TestServer} from '../../utils/testserver/lib/index.js'; -import {it} from './mocha-utils.js'; let HOSTNAME = os.hostname(); diff --git a/test/src/queryhandler.spec.ts b/test/src/queryhandler.spec.ts index 921fbb7d6b2..30e5d69254d 100644 --- a/test/src/queryhandler.spec.ts +++ b/test/src/queryhandler.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Query handler tests', function () { setupTestBrowserHooks(); diff --git a/test/src/queryselector.spec.ts b/test/src/queryselector.spec.ts index 8c94a60e74c..7eeab8e44e7 100644 --- a/test/src/queryselector.spec.ts +++ b/test/src/queryselector.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('querySelector', function () { setupTestBrowserHooks(); diff --git a/test/src/requestinterception-experimental.spec.ts b/test/src/requestinterception-experimental.spec.ts index 02971d20234..a0c6da6fd6a 100644 --- a/test/src/requestinterception-experimental.spec.ts +++ b/test/src/requestinterception-experimental.spec.ts @@ -29,7 +29,6 @@ import { HTTPRequest, InterceptResolutionAction, } from '../../lib/cjs/puppeteer/common/HTTPRequest.js'; -import {it} from './mocha-utils.js'; describe('request interception', function () { setupTestBrowserHooks(); diff --git a/test/src/requestinterception.spec.ts b/test/src/requestinterception.spec.ts index a1abc3ab5a9..cfbe67e9589 100644 --- a/test/src/requestinterception.spec.ts +++ b/test/src/requestinterception.spec.ts @@ -25,7 +25,6 @@ import { } from './mocha-utils.js'; import {HTTPRequest} from '../../lib/cjs/puppeteer/common/HTTPRequest.js'; import {ConsoleMessage} from '../../lib/cjs/puppeteer/common/ConsoleMessage.js'; -import {it} from './mocha-utils.js'; describe('request interception', function () { setupTestBrowserHooks(); diff --git a/test/src/screenshot.spec.ts b/test/src/screenshot.spec.ts index eefeda4bf61..d6cf443e07f 100644 --- a/test/src/screenshot.spec.ts +++ b/test/src/screenshot.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Screenshots', function () { setupTestBrowserHooks(); diff --git a/test/src/target.spec.ts b/test/src/target.spec.ts index 96fbc5fe359..b92987c6ab2 100644 --- a/test/src/target.spec.ts +++ b/test/src/target.spec.ts @@ -24,7 +24,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import utils from './utils.js'; -import {it} from './mocha-utils.js'; const {waitEvent} = utils; diff --git a/test/src/touchscreen.spec.ts b/test/src/touchscreen.spec.ts index 12992674732..bfdfb3bbc5c 100644 --- a/test/src/touchscreen.spec.ts +++ b/test/src/touchscreen.spec.ts @@ -20,7 +20,6 @@ import { setupTestBrowserHooks, setupTestPageAndContextHooks, } from './mocha-utils.js'; -import {it} from './mocha-utils.js'; describe('Touchscreen', function () { setupTestBrowserHooks(); diff --git a/test/src/tracing.spec.ts b/test/src/tracing.spec.ts index 27b1ae50837..3f582e73a77 100644 --- a/test/src/tracing.spec.ts +++ b/test/src/tracing.spec.ts @@ -20,7 +20,6 @@ import expect from 'expect'; import {getTestState} from './mocha-utils.js'; import {Browser} from '../../lib/cjs/puppeteer/common/Browser.js'; import {Page} from '../../lib/cjs/puppeteer/common/Page.js'; -import {it} from './mocha-utils.js'; describe('Tracing', function () { let outputFile!: string; diff --git a/test/src/waittask.spec.ts b/test/src/waittask.spec.ts index 0988b5ac5b9..6c63fabff2e 100644 --- a/test/src/waittask.spec.ts +++ b/test/src/waittask.spec.ts @@ -22,7 +22,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import {attachFrame, detachFrame} from './utils.js'; -import {it} from './mocha-utils.js'; describe('waittask specs', function () { setupTestBrowserHooks(); diff --git a/test/src/worker.spec.ts b/test/src/worker.spec.ts index 6ff8cfb1682..c279f6efb99 100644 --- a/test/src/worker.spec.ts +++ b/test/src/worker.spec.ts @@ -23,7 +23,6 @@ import { setupTestPageAndContextHooks, } from './mocha-utils.js'; import {waitEvent} from './utils.js'; -import {it} from './mocha-utils.js'; describe('Workers', function () { setupTestBrowserHooks(); diff --git a/utils/mochaRunner/src/interface.ts b/utils/mochaRunner/src/interface.ts new file mode 100644 index 00000000000..1880fe2958f --- /dev/null +++ b/utils/mochaRunner/src/interface.ts @@ -0,0 +1,119 @@ +/** + * Copyright 2022 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import Mocha from 'mocha'; +import commonInterface from 'mocha/lib/interfaces/common'; +import {getTestId} from './utils.js'; + +type SuiteFunction = ((this: Mocha.Suite) => void) | undefined; +type ExclusiveSuiteFunction = (this: Mocha.Suite) => void; + +const skippedTests: Array<{testIdPattern: string; skip: true}> = process.env[ + 'PUPPETEER_SKIPPED_TEST_CONFIG' +] + ? JSON.parse(process.env['PUPPETEER_SKIPPED_TEST_CONFIG']) + : []; + +skippedTests.reverse(); + +function shouldSkipTest(test: Mocha.Test): boolean { + const testId = getTestId(test.file!, test.fullTitle()); + // TODO: more efficient lookup. + const defintion = skippedTests.find(skippedTest => { + return testId.startsWith(skippedTest.testIdPattern); + }); + if (defintion && defintion.skip) { + return true; + } + return false; +} + +function customBDDInterface(suite: Mocha.Suite) { + const suites = [suite]; + + suite.on( + Mocha.Suite.constants.EVENT_FILE_PRE_REQUIRE, + function (context, file, mocha) { + const common = commonInterface(suites, context, mocha); + + context['before'] = common.before; + context['after'] = common.after; + context['beforeEach'] = common.beforeEach; + context['afterEach'] = common.afterEach; + if (mocha.options.delay) { + context['run'] = common.runWithSuite(suite); + } + function describe(title: string, fn: SuiteFunction) { + return common.suite.create({ + title: title, + file: file, + fn: fn, + }); + } + describe.only = function (title: string, fn: ExclusiveSuiteFunction) { + return common.suite.only({ + title: title, + file: file, + fn: fn, + }); + }; + + describe.skip = function (title: string, fn: SuiteFunction) { + return common.suite.skip({ + title: title, + file: file, + fn: fn, + }); + }; + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + context['describe'] = describe; + + function it(title: string, fn: Mocha.TestFunction) { + const suite = suites[0]!; + const test = new Mocha.Test(title, suite.isPending() ? undefined : fn); + test.file = file; + test.parent = suite; + if (shouldSkipTest(test)) { + const test = new Mocha.Test(title); + test.file = file; + suite.addTest(test); + return test; + } else { + suite.addTest(test); + return test; + } + } + + it.only = function (title: string, fn: Mocha.TestFunction) { + return common.test.only(mocha, context['it'](title, fn)); + }; + + it.skip = function (title: string) { + return context['it'](title); + }; + + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + context.it = it; + } + ); +} + +customBDDInterface.description = 'Custom BDD'; + +module.exports = customBDDInterface; diff --git a/utils/mochaRunner/src/main.ts b/utils/mochaRunner/src/main.ts index 05f3168ce8f..ad9d6df2f09 100644 --- a/utils/mochaRunner/src/main.ts +++ b/utils/mochaRunner/src/main.ts @@ -1,3 +1,19 @@ +/** + * Copyright 2022 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import { TestExpectation, MochaResults, @@ -19,7 +35,6 @@ import { readJSON, filterByParameters, getExpectationUpdates, - getSkippedTests, } from './utils.js'; function getApplicableTestSuites( @@ -80,16 +95,17 @@ async function main() { parameters ); - const skippedTests = getSkippedTests(applicableExpectations); - const env = extendProcessEnv([ ...parameters.map(param => { return parsedSuitesFile.parameterDefinitons[param]; }), { PUPPETEER_SKIPPED_TEST_CONFIG: JSON.stringify( - skippedTests.map(ex => { - return ex.testIdPattern; + applicableExpectations.map(ex => { + return { + testIdPattern: ex.testIdPattern, + skip: ex.expectations.includes('SKIP'), + }; }) ), }, @@ -102,7 +118,13 @@ async function main() { console.log('Running', JSON.stringify(parameters), tmpFilename); const handle = spawn( 'npx mocha', - ['--reporter=json', '--reporter-option', 'output=' + tmpFilename], + [ + '-u', + path.join(__dirname, 'interface.js'), + '--reporter=json', + '--reporter-option', + 'output=' + tmpFilename, + ], { shell: true, cwd: process.cwd(), diff --git a/utils/mochaRunner/src/test.ts b/utils/mochaRunner/src/test.ts index 2de6fbe4607..ae8daed451e 100644 --- a/utils/mochaRunner/src/test.ts +++ b/utils/mochaRunner/src/test.ts @@ -1,3 +1,19 @@ +/** + * Copyright 2022 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import assert from 'assert/strict'; import test from 'node:test'; import {filterByParameters, getTestResultForFailure} from './utils.js'; diff --git a/utils/mochaRunner/src/types.ts b/utils/mochaRunner/src/types.ts index 7fe7b711686..31fea50916f 100644 --- a/utils/mochaRunner/src/types.ts +++ b/utils/mochaRunner/src/types.ts @@ -1,3 +1,19 @@ +/** + * Copyright 2022 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import {z} from 'zod'; export const zPlatform = z.enum(['win32', 'linux', 'darwin']); diff --git a/utils/mochaRunner/src/utils.ts b/utils/mochaRunner/src/utils.ts index d6deb0efa7a..4a2be6d827a 100644 --- a/utils/mochaRunner/src/utils.ts +++ b/utils/mochaRunner/src/utils.ts @@ -1,3 +1,19 @@ +/** + * Copyright 2022 Google Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + import { MochaTestResult, TestExpectation, @@ -140,14 +156,6 @@ export function getTestResultForFailure( return test.err?.code === 'ERR_MOCHA_TIMEOUT' ? 'TIMEOUT' : 'FAIL'; } -export function getSkippedTests( - expectations: TestExpectation[] -): TestExpectation[] { - return expectations.filter(ex => { - return ex.expectations.includes('SKIP'); - }); -} - export function getTestId(file: string, fullTitle: string): string { return `[${getFilename(file)}] ${fullTitle}`; }