Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implements a mapper option
  • Loading branch information
Maël Nison committed Sep 3, 2018
commit 2e16c3a9ce51341ea28909f4840d62d797a8ae49
28 changes: 28 additions & 0 deletions packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const STRAWBERRY = path.join(FRUITS, 'strawberry.js');
const KIWI = path.join(FRUITS, 'kiwi.js');
const TOMATO = path.join(FRUITS, 'tomato.js');
const MELON = path.join(VEGETABLES, 'melon.json');
const DURIAN = path.join(VEGETABLES, 'durian.zip');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this theme!

const WATCH_PROJECT_MOCK = {
[FRUITS]: {
relative_path: 'fruits',
Expand Down Expand Up @@ -89,6 +90,11 @@ describe('watchman watch', () => {
mtime_ms: {toNumber: () => 33},
name: 'vegetables/melon.json',
},
{
exists: true,
mtime_ms: {toNumber: () => 33},
name: 'vegetables/durian.zip',
},
],
is_fresh_instance: true,
version: '4.5.0',
Expand Down Expand Up @@ -157,6 +163,28 @@ describe('watchman watch', () => {
expect(client.end).toBeCalled();
}));

test('applies the mapper when needed', async () =>
watchmanCrawl({
data: {
clocks: Object.create(null),
files: Object.create(null),
},
extensions: ['js', 'json', 'zip'],
mapper: n =>
n.endsWith('.zip')
? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')]
: null,
ignore: pearMatcher,
roots: ROOTS,
}).then(data => {
expect(data.files).toEqual(
Object.assign({}, mockFiles, {
[path.join(DURIAN, 'foo.1.js')]: ['', 33, 0, [], null],
[path.join(DURIAN, 'foo.2.js')]: ['', 33, 0, [], null],
}),
);
}));

test('updates the file object when the clock is given', () => {
mockResponse = {
'list-capabilities': {
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-haste-map/src/crawlers/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function findNative(
module.exports = function nodeCrawl(
options: CrawlerOptions,
): Promise<InternalHasteMap> {
if (options.mapper) {
throw new Error(`Option 'mapper' isn't supported on this crawler`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Option 'mapper' is not supported in the node crawler"?

}

const {data, extensions, forceNodeFilesystemAPI, ignore, roots} = options;

return new Promise(resolve => {
Expand Down
21 changes: 17 additions & 4 deletions packages/jest-haste-map/src/crawlers/watchman.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,19 +169,32 @@ module.exports = async function watchmanCrawl(
typeof fileData.mtime_ms === 'number'
? fileData.mtime_ms
: fileData.mtime_ms.toNumber();

const existingFileData = data.files[name];
let nextData;

const isOld = existingFileData && existingFileData[H.MTIME] === mtime;
if (isOld) {
files[name] = existingFileData;
nextData = existingFileData;
} else {
let sha1hex = fileData['content.sha1hex'];

if (typeof sha1hex !== 'string' || sha1hex.length !== 40) {
sha1hex = null;
}

// See ../constants.js
files[name] = ['', mtime, 0, [], sha1hex];
nextData = ['', mtime, 0, [], sha1hex];
}

const mappings = options.mapper ? options.mapper(name) : nullo;

if (mappings) {
for (const name of mappings) {
if (!ignore(name)) {
files[name] = nextData;
}
}
} else {
files[name] = nextData;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/jest-haste-map/src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import type {InternalHasteMap, ModuleMetaData} from 'types/HasteMap';

export type IgnoreMatcher = (item: string) => boolean;
export type Mapper = (item: string) => ?Array<string>;

export type WorkerMessage = {
computeDependencies: boolean,
Expand All @@ -31,6 +32,7 @@ export type CrawlerOptions = {|
extensions: Array<string>,
forceNodeFilesystemAPI: boolean,
ignore: IgnoreMatcher,
mapper: ?Mapper,
roots: Array<string>,
|};

Expand Down