Skip to content

Commit

Permalink
fix 'Runner hangs when error occurs (Error in Role initializer) in re…
Browse files Browse the repository at this point in the history
…usable role' close (DevExpress#5278)
  • Loading branch information
miherlosev committed Dec 17, 2021
1 parent 4229a22 commit 8184e21
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
31 changes: 23 additions & 8 deletions src/role/role.ts
Expand Up @@ -53,7 +53,20 @@ export default class Role extends EventEmitter {
};
}

private async _setInitError (err: Error, testRun: TestRun): Promise<void> {
this.initErr = err;

await testRun?.compilerService?.updateRoleProperty({
roleId: this.id,
name: 'initErr',
value: this.initErr,
});
}

private async _executeInitFn (testRun: TestRun): Promise<void> {
if (this.initErr)
return;

try {
if (testRun.compilerService)
this._wrapTestFn(testRun);
Expand All @@ -66,21 +79,23 @@ export default class Role extends EventEmitter {
await fn();
}
catch (err) {
this.initErr = err;
await this._setInitError(err, testRun);
}
}

await testRun?.compilerService?.updateRoleProperty({
roleId: this.id,
name: 'initErr',
value: this.initErr,
});
private async _switchToCleanRun (testRun: TestRun): Promise<void> {
try {
await testRun.switchToCleanRun(this.loginUrl as string);
}
catch (err) {
await this._setInitError(err, testRun);
}
}

public async initialize (testRun: TestRun): Promise<void> {
this.phase = RolePhase.pendingInitialization;

await testRun.switchToCleanRun(this.loginUrl as string);

await this._switchToCleanRun(testRun);
await this._executeInitFn(testRun);
await this._storeStateSnapshot(testRun);

Expand Down
13 changes: 11 additions & 2 deletions test/functional/fixtures/api/es-next/roles/test.js
@@ -1,5 +1,6 @@
const { expect } = require('chai');
const config = require('../../../../config');
const { expect } = require('chai');
const config = require('../../../../config');
const { errorInEachBrowserContains } = require('../../../../assertion-helper');


// NOTE: we set selectorTimeout to a large value in some tests to wait for
Expand Down Expand Up @@ -102,6 +103,14 @@ if (config.currentEnvironmentName !== config.testingEnvironmentNames.osXDesktopA
expect(errs[0]).contains('> 29 | .useRole(Role.anonymous());');
});
});

it('Should fail if an error occurred while switching to clean run (GH-5278)', function () {
return runTests('./testcafe-fixtures/error-on-switching-to-clean-run-test.js', null, { shouldFail: true })
.catch(function (errs) {
errorInEachBrowserContains(errs, 'Error in Role initializer - Failed to load the page at "https://non-existing-url.com/"', 0);
errorInEachBrowserContains(errs, 'Error in Role initializer - Failed to load the page at "https://non-existing-url.com/"', 1);
});
});
});
});
}
@@ -0,0 +1,14 @@
import { Role } from 'testcafe';

const role = Role('https://non-existing-url.com', () => {}, { preserveUrl: true });

fixture`Fixture`
.beforeEach(
async (t) => {
await t.useRole(role);
}
);

test('test1', async () => {});

test('test2', async () => {});

0 comments on commit 8184e21

Please sign in to comment.