@@ -6,30 +6,33 @@ const ArtifactPlugin = require('./ArtifactPlugin');
66class TwoSnapshotsPerTestPlugin extends ArtifactPlugin {
77 constructor ( { api } ) {
88 super ( { api } ) ;
9- this . _snapshots = [ null , null ] ;
9+ this . snapshots = { } ;
1010 }
1111
12- async onBeforeEach ( ) {
13- await this . _takeSnapshot ( 0 ) ;
12+ async onBeforeEach ( testSummary ) {
13+ await super . onBeforeEach ( testSummary ) ;
14+ await this . takeSnapshot ( 'beforeEach' ) ;
1415 }
1516
1617 async onAfterEach ( testSummary ) {
18+ await super . onAfterEach ( testSummary ) ;
19+
1720 if ( this . shouldKeepArtifactOfTest ( testSummary ) ) {
18- await this . _takeSnapshot ( 1 ) ;
19- this . _startSavingSnapshot ( testSummary , 0 ) ;
20- this . _startSavingSnapshot ( testSummary , 1 ) ;
21+ await this . takeSnapshot ( 'afterEach' ) ;
22+ this . startSavingSnapshot ( 'beforeEach' ) ;
23+ this . startSavingSnapshot ( 'afterEach' ) ;
2124 } else {
22- this . _startDiscardingSnapshot ( 0 ) ;
25+ this . startDiscardingSnapshot ( 'beforeEach' ) ;
2326 }
2427
25- this . _clearSnapshotReferences ( ) ;
28+ this . _clearAutomaticSnapshotReferences ( ) ;
2629 }
2730
2831 /***
2932 * @protected
3033 * @abstract
3134 */
32- async preparePathForSnapshot ( testSummary , index ) { }
35+ async preparePathForSnapshot ( testSummary , snapshotName ) { }
3336
3437
3538 /***
@@ -41,33 +44,48 @@ class TwoSnapshotsPerTestPlugin extends ArtifactPlugin {
4144 */
4245 createTestArtifact ( ) { }
4346
44- async _takeSnapshot ( index ) {
47+ _clearAutomaticSnapshotReferences ( ) {
48+ delete this . snapshots . beforeEach ;
49+ delete this . snapshots . afterEach ;
50+ }
51+
52+ /***
53+ * @protected
54+ */
55+ async takeSnapshot ( name ) {
4556 if ( ! this . enabled ) {
4657 return ;
4758 }
4859
49- const snapshot = this . createTestArtifact ( ) ;
60+ const snapshot = this . snapshots [ name ] = this . createTestArtifact ( ) ;
5061 await snapshot . start ( ) ;
5162 await snapshot . stop ( ) ;
52- this . _snapshots [ index ] = snapshot ;
63+
5364 this . api . trackArtifact ( snapshot ) ;
5465 }
5566
56- _startSavingSnapshot ( testSummary , index ) {
57- const snapshot = this . _snapshots [ index ] ;
67+ /***
68+ * @protected
69+ */
70+ startSavingSnapshot ( name ) {
71+ const snapshot = this . snapshots [ name ] ;
5872 if ( ! snapshot ) {
5973 return ;
6074 }
6175
76+ const { testSummary} = this . context ;
6277 this . api . requestIdleCallback ( async ( ) => {
63- const snapshotArtifactPath = await this . preparePathForSnapshot ( testSummary , index ) ;
78+ const snapshotArtifactPath = await this . preparePathForSnapshot ( testSummary , name ) ;
6479 await snapshot . save ( snapshotArtifactPath ) ;
6580 this . api . untrackArtifact ( snapshot ) ;
6681 } ) ;
6782 }
6883
69- _startDiscardingSnapshot ( index ) {
70- const snapshot = this . _snapshots [ index ] ;
84+ /***
85+ * @protected
86+ */
87+ startDiscardingSnapshot ( name ) {
88+ const snapshot = this . snapshots [ name ] ;
7189 if ( ! snapshot ) {
7290 return ;
7391 }
@@ -77,11 +95,6 @@ class TwoSnapshotsPerTestPlugin extends ArtifactPlugin {
7795 this . api . untrackArtifact ( snapshot ) ;
7896 } ) ;
7997 }
80-
81- _clearSnapshotReferences ( ) {
82- this . _snapshots [ 0 ] = null ;
83- this . _snapshots [ 1 ] = null ;
84- }
8598}
8699
87100module . exports = TwoSnapshotsPerTestPlugin ;
0 commit comments