Skip to content

Commit

Permalink
Implements a mapper option (#6940)
Browse files Browse the repository at this point in the history
* Implements a mapper option

* Updates changelog

* Fixes linting

* Makes the mapper optional typewise

* Fixes tests

* Adds the plumbing to set the value from the options

* Updates the error message

* Linting

* Fixes things
  • Loading branch information
arcanis authored Oct 22, 2018
1 parent 1615a2d commit 4954f46
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@

- `[jest-cli]` Add `changedSince` to allowed watch mode configs ([#6955](https://github.com/facebook/jest/pull/6955))
- `[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
49 changes: 49 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 @@ -44,6 +44,8 @@ const STRAWBERRY_RELATIVE = path.join(FRUITS_RELATIVE, 'strawberry.js');
const KIWI_RELATIVE = path.join(FRUITS_RELATIVE, 'kiwi.js');
const TOMATO_RELATIVE = path.join(FRUITS_RELATIVE, 'tomato.js');
const MELON_RELATIVE = path.join(VEGETABLES_RELATIVE, 'melon.json');
const DURIAN_RELATIVE = path.join(VEGETABLES_RELATIVE, 'durian.zip');

const WATCH_PROJECT_MOCK = {
[FRUITS]: {
relative_path: 'fruits',
Expand Down Expand Up @@ -164,6 +166,53 @@ describe('watchman watch', () => {
expect(client.end).toBeCalled();
}));

test('applies the mapper when needed', () => {
mockResponse = {
'list-capabilities': {
[undefined]: {
capabilities: ['field-content.sha1hex'],
},
},
query: {
[ROOT_MOCK]: {
clock: 'c:fake-clock:1',
files: [
{
exists: true,
mtime_ms: {toNumber: () => 33},
name: 'vegetables/durian.zip',
},
],
is_fresh_instance: true,
version: '4.5.0',
},
},
'watch-project': WATCH_PROJECT_MOCK,
};

return watchmanCrawl({
data: {
clocks: new Map(),
files: new Map(),
},
extensions: ['js', 'json', 'zip'],
ignore: pearMatcher,
mapper: n =>
n.endsWith('.zip')
? [path.join(n, 'foo.1.js'), path.join(n, 'foo.2.js')]
: null,
rootDir: ROOT_MOCK,
roots: ROOTS,
}).then(data => {
expect(data.files).toEqual(
createMap({
[path.join(DURIAN_RELATIVE, 'foo.1.js')]: ['', 33, 0, [], null],
[path.join(DURIAN_RELATIVE, '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 @@ -128,6 +128,10 @@ function findNative(
module.exports = function nodeCrawl(
options: CrawlerOptions,
): Promise<InternalHasteMap> {
if (options.mapper) {
throw new Error(`Option 'mapper' isn't supported by the Node crawler`);
}

const {
data,
extensions,
Expand Down
32 changes: 23 additions & 9 deletions packages/jest-haste-map/src/crawlers/watchman.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,29 +175,43 @@ module.exports = async function watchmanCrawl(
typeof fileData.mtime_ms === 'number'
? fileData.mtime_ms
: fileData.mtime_ms.toNumber();

let sha1hex = fileData['content.sha1hex'];
if (typeof sha1hex !== 'string' || sha1hex.length !== 40) {
sha1hex = null;
}

const existingFileData = data.files.get(relativeFilePath);
let nextData;

if (existingFileData && existingFileData[H.MTIME] === mtime) {
files.set(relativeFilePath, existingFileData);
nextData = existingFileData;
} else if (
existingFileData &&
sha1hex &&
existingFileData[H.SHA1] === sha1hex
) {
files.set(relativeFilePath, [
existingFileData[0],
mtime,
existingFileData[2],
existingFileData[3],
existingFileData[4],
]);
nextData = [...existingFileData];
nextData[1] = mtime;
} else {
// See ../constants.js
files.set(relativeFilePath, ['', mtime, 0, [], sha1hex]);
nextData = ['', mtime, 0, [], sha1hex];
}

const mappings = options.mapper ? options.mapper(filePath) : null;

if (mappings) {
for (const absoluteVirtualFilePath of mappings) {
if (!ignore(absoluteVirtualFilePath)) {
const relativeVirtualFilePath = fastPath.relative(
rootDir,
absoluteVirtualFilePath,
);
files.set(relativeVirtualFilePath, nextData);
}
}
} else {
files.set(relativeFilePath, nextData);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import * as fastPath from './lib/fast_path';
import Worker from 'jest-worker';

import type {Console} from 'console';
import type {Mapper} from './types';
import type {Path} from 'types/Config';
import type {
HasteMap as HasteMapObject,
Expand All @@ -55,6 +56,7 @@ type Options = {
forceNodeFilesystemAPI?: boolean,
hasteImplModulePath?: string,
ignorePattern?: ?HasteRegExp,
mapper?: ?Mapper,
maxWorkers: number,
mocksPattern?: string,
name: string,
Expand All @@ -77,6 +79,7 @@ type InternalOptions = {
forceNodeFilesystemAPI: boolean,
hasteImplModulePath?: string,
ignorePattern: ?HasteRegExp,
mapper?: ?Mapper,
maxWorkers: number,
mocksPattern: ?RegExp,
name: string,
Expand Down Expand Up @@ -671,6 +674,7 @@ class HasteMap extends EventEmitter {
extensions: options.extensions,
forceNodeFilesystemAPI: options.forceNodeFilesystemAPI,
ignore,
mapper: options.mapper,
rootDir: options.rootDir,
roots: options.roots,
}).catch(e => {
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 @@ -32,6 +33,7 @@ export type CrawlerOptions = {|
extensions: Array<string>,
forceNodeFilesystemAPI: boolean,
ignore: IgnoreMatcher,
mapper?: ?Mapper,
rootDir: string,
roots: Array<string>,
|};
Expand Down

0 comments on commit 4954f46

Please sign in to comment.