Skip to content

Commit

Permalink
Parallelize tests (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
grigasp authored Oct 7, 2024
1 parent 79bcbf1 commit 1b51140
Show file tree
Hide file tree
Showing 33 changed files with 256 additions and 197 deletions.
21 changes: 11 additions & 10 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/components/lib/**/*.js"
Expand All @@ -101,7 +101,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/opentelemetry/lib/**/*.js"
Expand All @@ -121,7 +121,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/testing/lib/**/*.js"
Expand All @@ -141,7 +141,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"./lib/**/*.test.js"
"--parallel=false"
],
"env": {
"NODE_ENV": "development",
Expand All @@ -158,7 +158,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/shared/lib/**/*.js"
Expand All @@ -178,7 +178,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/hierarchies/lib/**/*.js"
Expand All @@ -198,7 +198,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/core-interop/lib/**/*.js"
Expand All @@ -217,7 +217,8 @@
"args": [
"--config",
"./.mocharc.json",
"--no-timeouts"
"--no-timeouts",
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/components/lib/**/*.js",
Expand All @@ -244,7 +245,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/unified-selection/lib/**/*.js"
Expand All @@ -264,7 +265,7 @@
"--config",
"./.mocharc.json",
"--no-timeouts",
"lib/cjs/test/**/*.test.js"
"--parallel=false"
],
"outFiles": [
"${workspaceFolder}/packages/hierarchies-react/lib/**/*.js"
Expand Down
6 changes: 2 additions & 4 deletions apps/full-stack-tests/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{
"require": ["ignore-styles"],
"require": ["ignore-styles", "./lib/setup.js"],
"checkLeaks": true,
"globals": ["requestAnimationFrame", "IS_REACT_ACT_ENVIRONMENT"],
"timeout": 60000,
"slow": 500,
"file": ["./lib/setup.js"],
"reporter": ["node_modules/@itwin/build-tools/mocha-reporter"],
"reporterOptions": ["mochaFile=lib/test/junit_results.xml"],
"parallel": true,
"spec": ["./lib/**/*.test.js"]
}
4 changes: 2 additions & 2 deletions apps/full-stack-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"description": "Presentation integration tests",
"private": true,
"scripts": {
"build": "tsc",
"build:watch": "tsc -w",
"build": "node ./scripts/setup-tests.js && tsc",
"build:watch": "npm run build -- -w",
"clean": "rimraf lib build",
"cover": "npm run -s test",
"lint": "eslint ./src/**/*.{ts,tsx}",
Expand Down
35 changes: 35 additions & 0 deletions apps/full-stack-tests/scripts/setup-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
const { execFileSync } = require("child_process");
const cpx = require("cpx2");
const fs = require("fs");
const path = require("path");

cpx.copySync(`assets/**/*`, "lib/assets");
copyITwinFrontendAssets("lib/public");
pseudoLocalize("lib/public/locales");

function copyITwinFrontendAssets(outputDir) {
const iTwinPackagesPath = "node_modules/@itwin";
fs.readdirSync(iTwinPackagesPath)
.map((packageName) => {
const packagePath = path.resolve(iTwinPackagesPath, packageName);
return path.join(packagePath, "lib", "public");
})
.filter((assetsPath) => fs.existsSync(assetsPath))
.forEach((src) => {
cpx.copySync(`${src}/**/*`, outputDir);
});
}

function pseudoLocalize(localesDir) {
const betoolsPath = path.resolve("node_modules", "@itwin", "build-tools", "bin", "betools.js");
const args = [betoolsPath, "pseudolocalize", "--englishDir", `${localesDir}/en`, "--out", `${localesDir}/en-PSEUDO`];
try {
execFileSync("node", args);
} catch {
throw new Error("Failed to pseudoLocalize localization files");
}
}
2 changes: 1 addition & 1 deletion apps/full-stack-tests/src/IntegrationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export async function initialize(props?: { backendTimeout?: number }) {
const presentationTestingInitProps: PresentationTestingInitProps = {
rpcs: [SnapshotIModelRpcInterface, IModelReadRpcInterface, PresentationRpcInterface, ECSchemaRpcInterface],
backendProps: backendInitProps,
backendHostProps: { cacheDir: path.join(__dirname, ".cache") },
backendHostProps: { cacheDir: path.join(__dirname, ".cache", `${process.pid}`) },
frontendProps: frontendInitProps,
frontendApp: IntegrationTestsApp,
frontendAppOptions,
Expand Down
11 changes: 10 additions & 1 deletion apps/full-stack-tests/src/components/tree/Update.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ import {
TreeModelNode,
TreeModelRootNode,
TreeModelSource,
UiComponents,
} from "@itwin/components-react";
import { IModelConnection, SnapshotConnection } from "@itwin/core-frontend";
import { IModelApp, IModelConnection, SnapshotConnection } from "@itwin/core-frontend";
import { ChildNodeSpecificationTypes, Ruleset, RuleTypes } from "@itwin/presentation-common";
import { IPresentationTreeDataProvider, usePresentationTreeState, UsePresentationTreeStateProps } from "@itwin/presentation-components";
import { Presentation } from "@itwin/presentation-frontend";
Expand Down Expand Up @@ -46,6 +47,14 @@ describe("Tree update", () => {
describe("detection", () => {
let defaultProps: Omit<UsePresentationTreeStateProps, "ruleset">;

before(async () => {
await UiComponents.initialize(IModelApp.localization);
});

after(() => {
UiComponents.terminate();
});

beforeEach(() => {
defaultProps = {
imodel,
Expand Down
94 changes: 32 additions & 62 deletions apps/full-stack-tests/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,50 @@
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
// WARNING: The order of imports in this file is important!

// setup chai
import * as chai from "chai";
import chaiJestSnapshot from "chai-jest-snapshot";
import chaiSubset from "chai-subset";
import { execFileSync } from "child_process";
import * as cpx from "cpx2";
import * as fs from "fs";
import globalJsdom from "global-jsdom";
import * as jsdom from "jsdom";
import * as path from "path";
import ResizeObserver from "resize-observer-polyfill";
import sinonChai from "sinon-chai";

// eslint-disable-next-line no-console
console.log(`Backend PID: ${process.pid}`);

// get rid of various xhr errors in the console
globalJsdom(undefined, {
virtualConsole: new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: true }),
});
global.ResizeObserver = ResizeObserver;

// setup chai
chai.use(chaiJestSnapshot);
chai.use(sinonChai);
chai.use(chaiSubset);

before(function () {
chaiJestSnapshot.resetSnapshotRegistry();

cpx.copySync(`assets/**/*`, "lib/assets");
copyITwinFrontendAssets("lib/public");
pseudoLocalize("lib/public/locales");

getGlobalThis().IS_REACT_ACT_ENVIRONMENT = true;
});

beforeEach(function () {
const currentTest = this.currentTest!;

// set up snapshot name
const sourceFilePath = currentTest.file!.replace("lib", "src").replace(/\.(jsx?|tsx?)$/, "");
const snapPath = `${sourceFilePath}.snap`;
chaiJestSnapshot.setFilename(snapPath);
chaiJestSnapshot.setTestName(currentTest.fullTitle());
// get rid of various xhr errors in the console
import globalJsdom from "global-jsdom";
import * as jsdom from "jsdom";
globalJsdom(undefined, {
virtualConsole: new jsdom.VirtualConsole().sendTo(console, { omitJSDOMErrors: true }),
});

after(function () {
delete getGlobalThis().IS_REACT_ACT_ENVIRONMENT;
});
// polyfill ResizeObserver
import ResizeObserver from "resize-observer-polyfill";
global.ResizeObserver = ResizeObserver;

// supply mocha hooks
import { cleanup } from "@testing-library/react";
export const mochaHooks = {
beforeAll() {
chaiJestSnapshot.resetSnapshotRegistry();
getGlobalThis().IS_REACT_ACT_ENVIRONMENT = true;
},
beforeEach() {
// set up snapshot name
const currentTest = (this as unknown as Mocha.Context).currentTest!;
const sourceFilePath = currentTest.file!.replace("lib", "src").replace(/\.(jsx?|tsx?)$/, "");
const snapPath = `${sourceFilePath}.snap`;
chaiJestSnapshot.setFilename(snapPath);
chaiJestSnapshot.setTestName(currentTest.fullTitle());
},
afterEach() {
cleanup();
},
afterAll() {
delete getGlobalThis().IS_REACT_ACT_ENVIRONMENT;
},
};
// eslint-disable-next-line @typescript-eslint/naming-convention
function getGlobalThis(): typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boolean } {
/* istanbul ignore else */
Expand All @@ -74,26 +67,3 @@ function getGlobalThis(): typeof globalThis & { IS_REACT_ACT_ENVIRONMENT?: boole
/* istanbul ignore next */
throw new Error("unable to locate global object");
}

function copyITwinFrontendAssets(outputDir: string) {
const iTwinPackagesPath = "node_modules/@itwin";
fs.readdirSync(iTwinPackagesPath)
.map((packageName) => {
const packagePath = path.resolve(iTwinPackagesPath, packageName);
return path.join(packagePath, "lib", "public");
})
.filter((assetsPath) => fs.existsSync(assetsPath))
.forEach((src) => {
cpx.copySync(`${src}/**/*`, outputDir);
});
}

function pseudoLocalize(localesDir: string) {
const betoolsPath = path.resolve("node_modules", "@itwin", "build-tools", "bin", "betools.js");
const args = [betoolsPath, "pseudolocalize", "--englishDir", `${localesDir}/en`, "--out", `${localesDir}/en-PSEUDO`];
try {
execFileSync("node", args);
} catch {
throw new Error("Failed to pseudoLocalize localization files");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ describe("HiliteSet", () => {
let iModel: IModelConnection;

before(async () => {
await initialize();
await initialize({
backendHostProps: {
cacheDir: path.join(__dirname, ".cache", `${process.pid}`),
},
});
// eslint-disable-next-line @itwin/no-internal
RpcManager.registerImpl(ECSchemaRpcInterface, ECSchemaRpcImpl);
RpcConfiguration.developmentMode = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ describe("SelectionScope", () => {
let iModel: IModelConnection;

before(async () => {
await initialize();
await initialize({
backendHostProps: {
cacheDir: path.join(__dirname, ".cache", `${process.pid}`),
},
});
// eslint-disable-next-line @itwin/no-internal
RpcManager.registerImpl(ECSchemaRpcInterface, ECSchemaRpcImpl);
RpcConfiguration.developmentMode = true;
Expand Down
9 changes: 4 additions & 5 deletions packages/components/.mocharc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"require": ["ignore-styles"],
"require": ["ignore-styles", "./lib/cjs/test/setup"],
"checkLeaks": true,
"global": ["IS_REACT_ACT_ENVIRONMENT", "__iui"],
"timeout": 60000,
"file": ["./lib/cjs/test/index.js"],
"exclude": ["./lib/test/coverage/**/*"],
"reporter": ["node_modules/@itwin/build-tools/mocha-reporter"],
"reporterOptions": ["mochaFile=lib/test/junit_results.xml"]
"parallel": true,
"spec": ["./lib/cjs/test/**/*.test.js"],
"exclude": ["./lib/test/coverage/**/*"]
}
2 changes: 1 addition & 1 deletion packages/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"clean": "rimraf lib build",
"cover": "nyc npm -s test",
"lint": "eslint ./src/**/*.{ts,tsx}",
"test": "mocha --enable-source-maps --config ./.mocharc.json \"./lib/cjs/test/**/*.test.js\"",
"test": "mocha --enable-source-maps --config ./.mocharc.json",
"test:watch": "npm -s test -- --reporter min --watch-extensions ts,tsx --watch",
"docs": "npm run -s docs:extract && npm run -s docs:reference && npm run -s docs:changelog",
"docs:changelog": "cpx ./CHANGELOG.md ./build/docs/reference/presentation-components",
Expand Down
Loading

0 comments on commit 1b51140

Please sign in to comment.