detox
Version:
E2E tests and automation for mobile
79 lines (63 loc) • 1.9 kB
JavaScript
/* eslint @typescript-eslint/no-unused-vars: ["error", { "args": "none" }] */
const ArtifactPlugin = require('./ArtifactPlugin');
/***
* @abstract
*/
class WholeTestRecorderPlugin extends ArtifactPlugin {
constructor({ api }) {
super({ api });
/** @type {*} */
this.testRecording = null;
}
async onTestStart(testSummary) {
await super.onTestStart(testSummary);
if (this.enabled) {
this.testRecording = this.createTrackedTestRecording();
await this.testRecording.start();
}
}
async onTestDone(testSummary) {
await super.onTestDone(testSummary);
if (this.testRecording) {
const testRecording = this.testRecording;
await testRecording.stop();
if (this.shouldKeepArtifactOfTest(testSummary)) {
this._startSavingTestRecording(testRecording, testSummary);
} else {
this._startDiscardingTestRecording(testRecording);
}
this.testRecording = null;
}
}
/***
* @protected
*/
createTrackedTestRecording(config) {
const recording = this.createTestRecording(config);
this.api.trackArtifact(recording);
return recording;
}
/***
* @abstract
* @protected
*/
createTestRecording(config) {}
/***
* @abstract
*/
async preparePathForTestArtifact(testSummary) {} // eslint-disable-line no-unused-vars
_startSavingTestRecording(testRecording, testSummary) {
this.api.requestIdleCallback(async () => {
const recordingArtifactPath = await this.preparePathForTestArtifact(testSummary);
await testRecording.save(recordingArtifactPath);
this.api.untrackArtifact(testRecording);
});
}
_startDiscardingTestRecording(testRecording) {
this.api.requestIdleCallback(async () => {
await testRecording.discard();
this.api.untrackArtifact(testRecording);
});
}
}
module.exports = WholeTestRecorderPlugin;