Skip to content

Commit

Permalink
fix: remove race condition where many suites run in parallel (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanzander authored Nov 1, 2024
1 parent 2814469 commit 66b71fd
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 23 deletions.
48 changes: 38 additions & 10 deletions __tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ const systemAttr = {
};

describe('index script', () => {
/**
* @type {JestReportPortal}
*/
let reporter;

beforeAll(() => {
Expand Down Expand Up @@ -371,7 +374,14 @@ describe('index script', () => {
const spyFinishFailedTest = jest.spyOn(reporter, '_finishFailedStep');
const spyFinishSkippedTest = jest.spyOn(reporter, '_finishSkippedStep');

reporter._finishStep({ status: TEST_ITEM_STATUSES.PASSED, failureMessages: [] });
reporter.tempStepIds.set('/fake test', 'tempStepId');

reporter._finishStep({
status: TEST_ITEM_STATUSES.PASSED,
failureMessages: [],
ancestorTitles: [],
title: 'fake test',
});

expect(spyFinishPassedTest).toHaveBeenCalled();
expect(spyFinishFailedTest).not.toHaveBeenCalled();
Expand All @@ -383,12 +393,19 @@ describe('index script', () => {
const spyFinishFailedTest = jest.spyOn(reporter, '_finishFailedStep');
const spyFinishSkippedTest = jest.spyOn(reporter, '_finishSkippedStep');

reporter.tempStepIds.set('/fake test', 'tempStepId');

reporter._finishStep(
{ status: TEST_ITEM_STATUSES.FAILED, failureMessages: ['error message'] },
{
status: TEST_ITEM_STATUSES.FAILED,
failureMessages: ['error message'],
ancestorTitles: [],
title: 'fake test',
},
false,
);

expect(spyFinishFailedTest).toHaveBeenCalledWith('error message');
expect(spyFinishFailedTest).toHaveBeenCalledWith('tempStepId', 'error message');
expect(spyFinishPassedTest).not.toHaveBeenCalled();
expect(spyFinishSkippedTest).not.toHaveBeenCalled();
});
Expand All @@ -398,7 +415,14 @@ describe('index script', () => {
const spyFinishFailedTest = jest.spyOn(reporter, '_finishFailedStep');
const spyFinishSkippedTest = jest.spyOn(reporter, '_finishSkippedStep');

reporter._finishStep({ status: TEST_ITEM_STATUSES.SKIPPED, failureMessages: [] });
reporter.tempStepIds.set('/fake test', 'tempStepId');

reporter._finishStep({
status: TEST_ITEM_STATUSES.SKIPPED,
failureMessages: [],
ancestorTitles: [],
title: 'fake test',
});

expect(spyFinishSkippedTest).toHaveBeenCalled();
expect(spyFinishPassedTest).not.toHaveBeenCalled();
Expand Down Expand Up @@ -447,7 +471,7 @@ describe('index script', () => {
};
reporter.tempStepId = 'tempStepId';

reporter._finishPassedStep(false);
reporter._finishPassedStep('tempStepId', false);

expect(reporter.client.finishTestItem).toHaveBeenCalledWith(
'tempStepId',
Expand All @@ -467,9 +491,13 @@ describe('index script', () => {
};
reporter.tempStepId = tempStepId;

reporter._finishFailedStep(errorMessage, false);
reporter._finishFailedStep('tempStepId', errorMessage, false);

expect(spySendLog).toHaveBeenCalledWith({ message: errorMessage, level: LOG_LEVEL.ERROR });
expect(spySendLog).toHaveBeenCalledWith({
message: errorMessage,
level: LOG_LEVEL.ERROR,
tempStepId: 'tempStepId',
});
expect(reporter.client.finishTestItem).toHaveBeenCalledWith(
tempStepId,
expectedFinishTestItemParameter,
Expand All @@ -486,7 +514,7 @@ describe('index script', () => {
reporter.tempStepId = 'tempStepId';
reporter.reportOptions.extendTestDescriptionWithLastError = false;

reporter._finishFailedStep('error message', false);
reporter._finishFailedStep('tempStepId', 'error message', false);

expect(reporter.client.finishTestItem).toHaveBeenCalledWith(
'tempStepId',
Expand All @@ -503,7 +531,7 @@ describe('index script', () => {
};
reporter.tempStepId = 'tempStepId';

reporter._finishSkippedStep(false);
reporter._finishSkippedStep('tempStepId', false);

expect(reporter.client.finishTestItem).toHaveBeenCalledWith(
'tempStepId',
Expand All @@ -519,7 +547,7 @@ describe('index script', () => {
reporter.tempStepId = 'tempStepId';
reporter.reportOptions.skippedIssue = false;

reporter._finishSkippedStep(false);
reporter._finishSkippedStep('tempStepId', false);

expect(reporter.client.finishTestItem).toHaveBeenCalledWith(
'tempStepId',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 20 additions & 12 deletions src/reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,46 +201,54 @@ class JestReportPortal {
_finishStep(test) {
const errorMsg = test.failureMessages[0];

const fullName = getFullStepName(test);
const tempStepId = this.tempStepIds.get(fullName);

if (tempStepId === undefined) {
console.error(`Could not finish Test Step - "${fullName}"`);
return;
}

switch (test.status) {
case TEST_ITEM_STATUSES.PASSED:
this._finishPassedStep();
this._finishPassedStep(tempStepId);
break;
case TEST_ITEM_STATUSES.FAILED:
this._finishFailedStep(errorMsg);
this._finishFailedStep(tempStepId, errorMsg);
break;
default:
this._finishSkippedStep();
this._finishSkippedStep(tempStepId);
}
}

_finishPassedStep() {
_finishPassedStep(tempStepId) {
const status = TEST_ITEM_STATUSES.PASSED;
const { promise } = this.client.finishTestItem(this.tempStepId, { status });
const { promise } = this.client.finishTestItem(tempStepId, { status });

promiseErrorHandler(promise);
this.promises.push(promise);
}

_finishFailedStep(failureMessage) {
_finishFailedStep(tempStepId, failureMessage) {
const status = TEST_ITEM_STATUSES.FAILED;
const description =
this.reportOptions.extendTestDescriptionWithLastError === false
? null
: `\`\`\`error\n${stripAnsi(failureMessage)}\n\`\`\``;
const finishTestObj = { status, ...(description && { description }) };

this.sendLog({ message: failureMessage, level: LOG_LEVEL.ERROR });
this.sendLog({ message: failureMessage, level: LOG_LEVEL.ERROR, tempStepId });

const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
const { promise } = this.client.finishTestItem(tempStepId, finishTestObj);

promiseErrorHandler(promise);
this.promises.push(promise);
}

sendLog({ level = LOG_LEVEL.INFO, message = '', file, time }) {
sendLog({ level = LOG_LEVEL.INFO, message = '', file, time, tempStepId }) {
const newMessage = stripAnsi(message);
const { promise } = this.client.sendLog(
this.tempStepId,
tempStepId === undefined ? this.tempStepId : tempStepId,
{
message: newMessage,
level,
Expand All @@ -253,14 +261,14 @@ class JestReportPortal {
this.promises.push(promise);
}

_finishSkippedStep() {
_finishSkippedStep(tempStepId) {
const status = 'skipped';
const issue = this.reportOptions.skippedIssue === false ? { issueType: 'NOT_ISSUE' } : null;
const finishTestObj = {
status,
...(issue && { issue }),
};
const { promise } = this.client.finishTestItem(this.tempStepId, finishTestObj);
const { promise } = this.client.finishTestItem(tempStepId, finishTestObj);

promiseErrorHandler(promise);
this.promises.push(promise);
Expand Down

0 comments on commit 66b71fd

Please sign in to comment.