Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKamaev committed Mar 22, 2020
1 parent de92f1a commit 3e8fd62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
43 changes: 24 additions & 19 deletions src/test-run/index.js
Expand Up @@ -295,14 +295,10 @@ export default class TestRun extends AsyncEventEmitter {
await fn(this);
}
catch (err) {
let screenshotPath = this.errScreenshotPath;
await this._makeScreenshotOnFail();

const { screenshots } = this.opts;
this.addError(err);

if (!screenshotPath && screenshots && screenshots.takeOnFails)
screenshotPath = await this.executeCommand(new browserManipulationCommands.TakeScreenshotOnFailCommand());

this.addError(err, screenshotPath);
return false;
}

Expand Down Expand Up @@ -379,19 +375,19 @@ export default class TestRun extends AsyncEventEmitter {
return false;
}

_createErrorAdapter (err, screenshotPath) {
_createErrorAdapter (err) {
return new TestRunErrorFormattableAdapter(err, {
userAgent: this.browserConnection.userAgent,
screenshotPath: screenshotPath || '',
screenshotPath: this.errScreenshotPath || '',
testRunPhase: this.phase
});
}

addError (err, screenshotPath) {
addError (err) {
const errList = err instanceof TestCafeErrorList ? err.items : [err];

errList.forEach(item => {
const adapter = this._createErrorAdapter(item, screenshotPath);
const adapter = this._createErrorAdapter(item);

this.errs.push(adapter);
});
Expand Down Expand Up @@ -618,9 +614,9 @@ export default class TestRun extends AsyncEventEmitter {
}

async executeAction (actionName, command, callsite) {
let adapter = null;
let error = null;
let result = null;
let errorAdapter = null;
let error = null;
let result = null;

await this.emitActionStart(actionName, command);

Expand All @@ -630,15 +626,17 @@ export default class TestRun extends AsyncEventEmitter {
catch (err) {
error = err;

const { screenshots } = this.opts;

if (!this.errScreenshotPath && screenshots && screenshots.takeOnFails)
this.errScreenshotPath = await this.executeCommand(new browserManipulationCommands.TakeScreenshotOnFailCommand());
// NOTE: check if error is TestCafeErrorList is specific for the `useRole` action
// if error is TestCafeErrorList we do not need to create an adapter,
// since error is already was processed in role initializer
if (!(err instanceof TestCafeErrorList)) {
await this._makeScreenshotOnFail();

adapter = this._createErrorAdapter(processTestFnError(error), this.errScreenshotPath);
errorAdapter = this._createErrorAdapter(processTestFnError(err));
}
}

await this.emitActionDone(actionName, command, result, adapter);
await this.emitActionDone(actionName, command, result, errorAdapter);

if (error)
throw error;
Expand Down Expand Up @@ -718,6 +716,13 @@ export default class TestRun extends AsyncEventEmitter {
return Promise.reject(err);
}

async _makeScreenshotOnFail () {
const { screenshots } = this.opts;

if (!this.errScreenshotPath && screenshots && screenshots.takeOnFails)
this.errScreenshotPath = await this.executeCommand(new browserManipulationCommands.TakeScreenshotOnFailCommand());
}

_decorateWithFlag (fn, flagName, value) {
return async () => {
this[flagName] = value;
Expand Down
3 changes: 1 addition & 2 deletions test/functional/fixtures/reporter/test.js
Expand Up @@ -553,8 +553,7 @@ describe('Reporter', () => {
phase: 'initialized'
},
type: 'useRole'
},
errors: [ void 0 ] // TODO: investigate
}
}
]);
});
Expand Down
24 changes: 14 additions & 10 deletions test/server/test-controller-events-test.js
@@ -1,11 +1,12 @@
const expect = require('chai').expect;
const AsyncEventEmitter = require('../../lib/utils/async-event-emitter');
const TestRun = require('../../lib/test-run');
const TestController = require('../../lib/api/test-controller');
const Task = require('../../lib/runner/task');
const BrowserJob = require('../../lib/runner/browser-job');
const Reporter = require('../../lib/reporter');
const { Role } = require('../../lib/api/exportable-lib');
const expect = require('chai').expect;
const AsyncEventEmitter = require('../../lib/utils/async-event-emitter');
const TestRun = require('../../lib/test-run');
const TestController = require('../../lib/api/test-controller');
const Task = require('../../lib/runner/task');
const BrowserJob = require('../../lib/runner/browser-job');
const Reporter = require('../../lib/reporter');
const { Role } = require('../../lib/api/exportable-lib');
const TestRunErrorFormattableAdapter = require('../../lib/errors/test-run/formattable-adapter');

class TestRunMock extends TestRun {
constructor () {
Expand Down Expand Up @@ -175,10 +176,12 @@ describe('TestController action events', () => {

it('Error action', () => {
let actionResult = null;
let errorAdapter = null;

initializeReporter({
async reportTestActionDone (name, { command, errors }) {
actionResult = { name, command: command.type, err: errors[0].message };
errorAdapter = errors[0];
actionResult = { name, command: command.type, err: errors[0].errMsg };
}
});

Expand All @@ -191,8 +194,9 @@ describe('TestController action events', () => {
throw new Error();
})
.catch(err => {
expect(errorAdapter).instanceOf(TestRunErrorFormattableAdapter);
expect(err.message).eql('test error');
expect(actionResult).eql({ name: 'click', command: 'click', err: 'test error' });
expect(actionResult).eql({ name: 'click', command: 'click', err: 'Error: test error' });
});
});
});

0 comments on commit 3e8fd62

Please sign in to comment.