Skip to content

Commit

Permalink
feat: refresh tests on update snapshot folder
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed Apr 23, 2021
1 parent 1fd4a93 commit bd03dcd
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 258 deletions.
23 changes: 21 additions & 2 deletions core/instrument/src/misc/chached-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ export class CachedFileResource<T> {
.digest('hex'),
);
};

private getKey = () => {
const version = '2';
const files = Array.isArray(this.filePath)
? this.filePath
: [this.filePath];
return files
.reduce((acc, f) => acc.update(f), createHash('md5').update(version))
.digest('hex');
};
/**
*
* @returns if the data is in the cache or undefined
Expand All @@ -56,7 +66,12 @@ export class CachedFileResource<T> {
if (cacheModified >= fileModified) {
const fileData = fs.readFileSync(cachedFileName, 'utf8');
const json = JSON.parse(fileData);
return Object.keys(json).length ? json : undefined;
if (json.hasOwnProperty('key') && json.hasOwnProperty('data')) {
if (json.key === this.getKey()) {
return json['data'];
}
}
return undefined;
}
}
return undefined;
Expand All @@ -68,6 +83,10 @@ export class CachedFileResource<T> {
*/
set = (data: T | undefined): void => {
const cachedFileName = this.getCachedFile();
fs.writeFileSync(cachedFileName, JSON.stringify(data || {}));
const json = {
key: this.getKey(),
data,
};
fs.writeFileSync(cachedFileName, JSON.stringify(json));
};
}
17 changes: 15 additions & 2 deletions core/instrument/src/misc/jest-tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'path';
import fs from 'fs';
import {
JestConfig,
runProjectTests,
Expand Down Expand Up @@ -27,19 +28,31 @@ export const extractTests = async (
acc: {
testFiles: string[];
coverageFiles: string[];
snapshotFiles: string[];
},
file,
) => {
acc.testFiles.push(...getRelatedTests(file));
acc.coverageFiles.push(file);
const snapshotFolder = path.join(path.dirname(file), '__snapshots__');
if (fs.existsSync(snapshotFolder)) {
acc.snapshotFiles.push(
...fs
.readdirSync(snapshotFolder)
.map(f => path.resolve(snapshotFolder, f)),
);
}
if (!acc.coverageFiles.includes(file)) {
acc.coverageFiles.push(file);
}
return acc;
},
{ testFiles: [], coverageFiles: [] },
{ testFiles: [], coverageFiles: [], snapshotFiles: [] },
);
if (tests.testFiles.length) {
const cached = new CachedFileResource<JestTests>('jest-tests', files[0], [
...tests.testFiles,
...tests.coverageFiles,
...tests.snapshotFiles,
]);
const cachedResults = cached.get();
if (cachedResults) {
Expand Down
19 changes: 13 additions & 6 deletions core/loader/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,31 @@ async function loader(this: WebpackLoaderContext): Promise<string> {
if (store?.doc) {
log('loaded: ', filePath);
if (store.stories && store.components && store.packages) {
const dependencies: string[] = [];
const fileDependencies: string[] = [];
const folderDependencies: string[] = [];
Object.values(store.components).forEach(component => {
if (component.request) {
dependencies.push(component.request as string);
fileDependencies.push(component.request as string);
if (component.jest) {
const componentFolder = path.dirname(component.request);
component.jest.results.forEach(r => {
dependencies.push(
path.resolve(componentFolder, r.testFilePath),
const testFilePath = path.resolve(
componentFolder,
r.testFilePath,
);
fileDependencies.push(testFilePath);
folderDependencies.push(
path.join(path.dirname(testFilePath), '__snapshots__'),
);
});
Object.keys(component.jest.coverage).forEach(f => {
dependencies.push(path.resolve(componentFolder, f));
fileDependencies.push(path.resolve(componentFolder, f));
});
}
}
});
new Set(dependencies).forEach(d => this.addDependency(d));
new Set(fileDependencies).forEach(d => this.addDependency(d));
new Set(folderDependencies).forEach(d => this.addContextDependency(d));
addStoriesDoc(filePath, this._compilation.records.hash, {
stories: store.stories,
components: store.components,
Expand Down
Loading

0 comments on commit bd03dcd

Please sign in to comment.