Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements a mapper option #6940

Merged
merged 11 commits into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

- `[babel-jest]` Add support for `babel.config.js` added in Babel 7.0.0 ([#6911](https://github.com/facebook/jest/pull/6911))
- `[jest-resolve]` Add support for an experimental `mapper` option (Watchman crawler only) that adds virtual files to the Haste map ([#6940](https://github.com/facebook/jest/pull/6940))

### Fixes

Expand Down
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'],
ignore: pearMatcher,
mapper: n =>
n.endsWith('.zip')
? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')]
: null,
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) : null;

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