@@ -225,9 +225,31 @@ export class JestTask extends GulpTask<IJestConfig> {
225225
226226 private _copySnapshots ( srcRoot : string , destRoot : string ) : void {
227227 const pattern : string = path . join ( srcRoot , '**/__snapshots__/*.snap' ) ;
228- globby . sync ( pattern ) . forEach ( sourceFile => {
229- const destination : string = sourceFile . replace ( srcRoot , destRoot ) ;
230- fsx . copySync ( sourceFile , destination ) ;
228+ globby . sync ( pattern ) . forEach ( snapFile => {
229+ const destination : string = snapFile . replace ( srcRoot , destRoot ) ;
230+ if ( this . _copyIfMatchExtension ( snapFile , destination , '.test.tsx.snap' ) ) {
231+ this . logVerbose ( `Snapshot file ${ snapFile } is copied to match extension ".test.tsx.snap".` ) ;
232+ } else if ( this . _copyIfMatchExtension ( snapFile , destination , '.test.ts.snap' ) ) {
233+ this . logVerbose ( `Snapshot file ${ snapFile } is copied to match extension ".test.ts.snap".` ) ;
234+ } else if ( this . _copyIfMatchExtension ( snapFile , destination , '.test.jsx.snap' ) ) {
235+ this . logVerbose ( `Snapshot file ${ snapFile } is copied to match extension ".test.jsx.snap".` ) ;
236+ } else if ( this . _copyIfMatchExtension ( snapFile , destination , '.test.js.snap' ) ) {
237+ this . logVerbose ( `Snapshot file ${ snapFile } is copied to match extension ".test.js.snap".` ) ;
238+ } else {
239+ this . logWarning ( `Snapshot file ${ snapFile } is not copied because don't find that matching test file.` ) ;
240+ }
231241 } ) ;
232242 }
243+
244+ private _copyIfMatchExtension ( snapSourceFile : string , destinationFile : string , extension : string ) : boolean {
245+ const snapDestFile : string = destinationFile . replace ( / \. t e s t \. .+ \. s n a p $ / , extension ) ;
246+ const testFileName : string = path . basename ( snapDestFile , '.snap' ) ;
247+ const testFile : string = path . resolve ( path . dirname ( snapDestFile ) , '..' , testFileName ) ; // Up from `__snapshots__`.
248+ if ( fsx . existsSync ( testFile ) ) {
249+ fsx . copySync ( snapSourceFile , snapDestFile ) ;
250+ return true ;
251+ } else {
252+ return false ;
253+ }
254+ }
233255}
0 commit comments