resolve( file: Config.Path, options?: Resolver.ResolveModuleConfig, ): Array<Config.Path> { const dependencies = this._hasteFS.getDependencies(file); if (!dependencies) { return []; } return dependencies.reduce<Array<Config.Path>>((acc, dependency) => { if (this._resolver.isCoreModule(dependency)) { return acc; } let resolvedDependency; try { resolvedDependency = this._resolver.resolveModule( file, dependency, options, ); } catch (e) { resolvedDependency = this._resolver.getMockModule(file, dependency); } if (resolvedDependency) { acc.push(resolvedDependency); } return acc; }, []); }
const cleanup = ( hasteFS: HasteFS, update: Config.SnapshotUpdateState, snapshotResolver: JestSnapshotResolver, ) => { const pattern = '\\.' + EXTENSION + '$'; const files = hasteFS.matchFiles(pattern); const filesRemoved = files.reduce((acc, snapshotFile) => { if (!fileExists(snapshotResolver.resolveTestPath(snapshotFile), hasteFS)) { if (update === 'all') { fs.unlinkSync(snapshotFile); } return acc + 1; } return acc; }, 0); return { filesRemoved, }; };
resolveInverseModuleMap( paths: Set<Config.Path>, filter: (file: Config.Path) => boolean, options?: Resolver.ResolveModuleConfig, ): Array<DependencyResolver.ResolvedModule> { if (!paths.size) { return []; } const collectModules = ( related: Set<Config.Path>, moduleMap: Array<DependencyResolver.ResolvedModule>, changed: Set<Config.Path>, ) => { const visitedModules = new Set(); const result: Array<DependencyResolver.ResolvedModule> = []; while (changed.size) { changed = new Set( moduleMap.reduce<Array<Config.Path>>((acc, module) => { if ( visitedModules.has(module.file) || !module.dependencies.some(dep => changed.has(dep)) ) { return acc; } const file = module.file; if (filter(file)) { result.push(module); related.delete(file); } visitedModules.add(file); acc.push(module.file); return acc; }, []), ); } return result.concat( Array.from(related).map(file => ({dependencies: [], file})), ); }; const relatedPaths = new Set<Config.Path>(); const changed: Set<Config.Path> = new Set(); for (const path of paths) { if (this._hasteFS.exists(path)) { const modulePath = isSnapshotPath(path) ? this._snapshotResolver.resolveTestPath(path) : path; changed.add(modulePath); if (filter(modulePath)) { relatedPaths.add(modulePath); } } } const modules: Array<DependencyResolver.ResolvedModule> = []; for (const file of this._hasteFS.getAbsoluteFileIterator()) { modules.push({ dependencies: this.resolve(file, options), file, }); } return collectModules(relatedPaths, modules, changed); }
const fileExists = (filePath: Config.Path, hasteFS: HasteFS): boolean => hasteFS.exists(filePath) || fs.existsSync(filePath);