Skip to content

Commit

Permalink
Address the lack of asynchronous handling in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
csouchet committed Aug 24, 2023
1 parent 8c27e3a commit b1f5ade
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 61 deletions.
61 changes: 30 additions & 31 deletions test/config/jest.image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class RetriesCounter {

const retriesCounter = new RetriesCounter();

function saveAndRegisterImages(matcherContext: MatcherContext, received: Buffer, options: MatchImageSnapshotOptions): void {
async function saveAndRegisterImages(matcherContext: MatcherContext, received: Buffer, options: MatchImageSnapshotOptions): Promise<void> {
const snapshotIdentifier = <string>options.customSnapshotIdentifier;
// Manage expected and received images
const baseImagePathWithName = `${options.customDiffDir}/${snapshotIdentifier}`;
Expand All @@ -79,42 +79,41 @@ function saveAndRegisterImages(matcherContext: MatcherContext, received: Buffer,
matchers: {}, // required by the jest-html-reporters getJestGlobalData function even if not used
};

addAttach({
attach: computeRelativePathFromReportToSnapshots(`${baseImagePathWithName}-diff.png`),
description: 'diff',
bufferFormat: 'png',
context,
})
.then(() =>
addAttach({
attach: computeRelativePathFromReportToSnapshots(expectedImagePath),
description: 'expected',
bufferFormat: 'png',
context,
}),
)
.then(() => {
addAttach({
attach: computeRelativePathFromReportToSnapshots(receivedImagePath),
description: 'received',
bufferFormat: 'png',
context,
});
})
.catch(e =>
console.error(
`Error while attaching images to test ${snapshotIdentifier}.` +
`The 'jest-html-reporters' reporter is probably not in use. For instance, this occurs when running tests with the IntelliJ/Webstorm Jest runner.`,
e,
),
try {
await addAttach({
attach: computeRelativePathFromReportToSnapshots(`${baseImagePathWithName}-diff.png`),
description: 'diff',
bufferFormat: 'png',
context,
});

await addAttach({
attach: computeRelativePathFromReportToSnapshots(expectedImagePath),
description: 'expected',
bufferFormat: 'png',
context,
});

await addAttach({
attach: computeRelativePathFromReportToSnapshots(receivedImagePath),
description: 'received',
bufferFormat: 'png',
context,
});
} catch (e) {
console.error(
`Error while attaching images to test ${snapshotIdentifier}.` +
`The 'jest-html-reporters' reporter is probably not in use. For instance, this occurs when running tests with the IntelliJ/Webstorm Jest runner.`,
e,
);
}
}

// Improve jest-image-snapshot outputs to facilitate debug
// The 'options' parameter is mandatory for us, and some properties must be set as well
// All options properties used here are always set in bpmn-visualization tests
// If the following implementation would be done directly in jest-image-snapshot, this won't be required as it set default values we cannot access here
function toMatchImageSnapshotCustom(this: MatcherContext, received: Buffer, options: MatchImageSnapshotOptions): CustomMatcherResult {
async function toMatchImageSnapshotCustom(this: MatcherContext, received: Buffer, options: MatchImageSnapshotOptions): Promise<CustomMatcherResult> {
const testId = this.currentTestName;
retriesCounter.incrementExecutionCount(testId);
jestLog("Test: '%s' (test file path: '%s')", this.currentTestName, this.testPath);
Expand All @@ -126,7 +125,7 @@ function toMatchImageSnapshotCustom(this: MatcherContext, received: Buffer, opti
if (!result.pass) {
jestLog('Result: failure');
if (retriesCounter.hasReachMaxRetries(testId)) {
saveAndRegisterImages(this, received, options);
await saveAndRegisterImages(this, received, options);
}

// Add configured failure threshold in the error message
Expand Down
4 changes: 2 additions & 2 deletions test/integration/mxGraph.model.bpmn.elements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,7 @@ describe('mxGraph model - BPMN elements', () => {
});
});

test('Elements in expanded Sub Process', async () => {
test('Elements in expanded Sub Process', () => {
expect('start_event_in_adHoc_sub_process_id').toBeStartEvent({
eventDefinitionKind: ShapeBpmnEventDefinitionKind.NONE,
label: 'Start Event In AdHoc Sub-Process',
Expand All @@ -1003,7 +1003,7 @@ describe('mxGraph model - BPMN elements', () => {
});
});

test('Elements of collapsed Sub Process', async () => {
test('Elements of collapsed Sub Process', () => {
expect('message_boundary_event_attached_to_collapsed_adHoc_sub_process_id').toBeBoundaryEvent({
eventDefinitionKind: ShapeBpmnEventDefinitionKind.MESSAGE,
label: 'Interrupting Message Boundary Event attached to collapsed AdHoc Sub-Process',
Expand Down
31 changes: 17 additions & 14 deletions test/performance/bpmn.load.performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ import { calculateMetrics } from './helpers/perf-utils';
import { performanceDataFilePath } from './helpers/file-utils';

const metricsArray: Array<PerformanceMetric> = [];

let metricsCollector: ChromiumMetricsCollector;

beforeAll(async () => {
metricsCollector = await ChromiumMetricsCollector.create(page);
});

describe('load performance', () => {
const pageTester = new PageTester({ targetedPage: AvailableTestPages.DIAGRAM_NAVIGATION, diagramSubfolder: 'performance' }, page);
const bpmnDiagramName = 'B.2.0';
Expand All @@ -41,17 +42,19 @@ describe('load performance', () => {
expect(true).toBe(true);
});
});
afterAll(() => {
metricsCollector.destroy();
try {
const oldDataString = fs.readFileSync(performanceDataFilePath, 'utf8');
const oldData = JSON.parse(oldDataString.substring('const data = '.length, oldDataString.length)) as ChartData;
const data = {
zoom: oldData.zoom,
load: oldData.load.concat(metricsArray),
};
fs.writeFileSync(performanceDataFilePath, 'const data = ' + JSON.stringify(data));
} catch (err) {
console.error(err);
}

afterAll(async () => {
await metricsCollector.destroy().then(() => {
try {
const oldDataString = fs.readFileSync(performanceDataFilePath, 'utf8');
const oldData = JSON.parse(oldDataString.substring('const data = '.length, oldDataString.length)) as ChartData;
const data = {
zoom: oldData.zoom,
load: oldData.load.concat(metricsArray),
};
fs.writeFileSync(performanceDataFilePath, 'const data = ' + JSON.stringify(data));
} catch (err) {
console.error(err);
}
});
});
31 changes: 17 additions & 14 deletions test/performance/bpmn.navigation.performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import { ZoomType } from '@lib/component/options';
import { performanceDataFilePath } from './helpers/file-utils';

const metricsArray: Array<PerformanceMetric> = [];

let metricsCollector: ChromiumMetricsCollector;

beforeAll(async () => {
metricsCollector = await ChromiumMetricsCollector.create(page);
});

describe('Mouse wheel zoom performance', () => {
const pageTester = new PageTester({ targetedPage: AvailableTestPages.DIAGRAM_NAVIGATION, diagramSubfolder: 'performance' }, page);

Expand Down Expand Up @@ -66,17 +67,19 @@ describe('Mouse wheel zoom performance', () => {
expect(true).toBe(true);
});
});
afterAll(() => {
metricsCollector.destroy();
try {
const oldDataString = fs.readFileSync(performanceDataFilePath, 'utf8');
const oldData = JSON.parse(oldDataString.substring('const data = '.length, oldDataString.length)) as ChartData;
const data = {
zoom: oldData.zoom.concat(metricsArray),
load: oldData.load,
};
fs.writeFileSync(performanceDataFilePath, 'const data = ' + JSON.stringify(data));
} catch (err) {
console.error(err);
}

afterAll(async () => {
await metricsCollector.destroy().then(() => {
try {
const oldDataString = fs.readFileSync(performanceDataFilePath, 'utf8');
const oldData = JSON.parse(oldDataString.substring('const data = '.length, oldDataString.length)) as ChartData;
const data = {
zoom: oldData.zoom.concat(metricsArray),
load: oldData.load,
};
fs.writeFileSync(performanceDataFilePath, 'const data = ' + JSON.stringify(data));
} catch (err) {
console.error(err);
}
});
});

0 comments on commit b1f5ade

Please sign in to comment.