Skip to content

Commit

Permalink
feat(jest-haste-map): add DependencyExtractor type
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Feb 23, 2022
1 parent 4e7f6ec commit 6a57b81
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
3 changes: 1 addition & 2 deletions e2e/__tests__/findRelatedFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,7 @@ describe('--findRelatedTests flag', () => {
expect(stderr).toMatch('PASS __tests__/test.test.js');
expect(stderr).not.toMatch('PASS __tests__/test-skip-deps.test.js');

const summaryMsg =
'Ran all test suites related to files matching /a.js/i.';
const summaryMsg = 'Ran all test suites related to files matching /a.js/i.';
expect(stderr).toMatch(summaryMsg);
});

Expand Down
10 changes: 6 additions & 4 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import normalizePathSep from './lib/normalizePathSep';
import type {
ChangeEvent,
CrawlerOptions,
DependencyExtractor,
EventsQueue,
FileData,
FileMetaData,
Expand Down Expand Up @@ -316,10 +317,11 @@ export default class HasteMap extends EventEmitter {
}

if (options.dependencyExtractor) {
const dependencyExtractor = await requireOrImportModule<any>(
options.dependencyExtractor,
false,
);
const dependencyExtractor =
await requireOrImportModule<DependencyExtractor>(
options.dependencyExtractor,
false,
);
if (dependencyExtractor.getCacheKey) {
dependencyExtractorHash = String(dependencyExtractor.getCacheKey());
}
Expand Down
9 changes: 9 additions & 0 deletions packages/jest-haste-map/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,12 @@ export type ChangeEvent = {
hasteFS: HasteFS;
moduleMap: ModuleMap;
};

export type DependencyExtractor = {
extract: (
code: string,
filePath: string,
defaultExtract: DependencyExtractor['extract'],
) => Iterable<string>;
getCacheKey?: () => string;
};
12 changes: 10 additions & 2 deletions packages/jest-haste-map/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {requireOrImportModule} from 'jest-util';
import blacklist from './blacklist';
import H from './constants';
import * as dependencyExtractor from './lib/dependencyExtractor';
import type {HasteImpl, WorkerMessage, WorkerMetadata} from './types';
import type {
DependencyExtractor,
HasteImpl,
WorkerMessage,
WorkerMetadata,
} from './types';

const PACKAGE_JSON = path.sep + 'package.json';

Expand Down Expand Up @@ -75,7 +80,10 @@ export async function worker(data: WorkerMessage): Promise<WorkerMetadata> {
dependencies = Array.from(
data.dependencyExtractor
? (
await requireOrImportModule<any>(data.dependencyExtractor, false)
await requireOrImportModule<DependencyExtractor>(
data.dependencyExtractor,
false,
)
).extract(content, filePath, dependencyExtractor.extract)
: dependencyExtractor.extract(content),
);
Expand Down

0 comments on commit 6a57b81

Please sign in to comment.