Skip to content

Commit a685126

Browse files
authored
Merge pull request microsoft#465 from lijunle/ts-snap
Copy only if the snap base file does exist.
2 parents cbcb190 + ce72400 commit a685126

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

build-tests/web-library-build-test/src/test/__snapshots__/foo.test.js.snap renamed to build-tests/web-library-build-test/src/test/__snapshots__/foo.test.ts.snap

File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/gulp-core-build",
5+
"comment": "Fix an issue where snapshot filenames did not follow Jest's naming convention.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/gulp-core-build",
10+
"email": "lijunle@users.noreply.github.com"
11+
}

‎core-build/gulp-core-build/src/tasks/JestTask.ts‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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(/\.test\..+\.snap$/, 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

Comments
 (0)