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

HasteMap with native maps #6960

Merged
merged 5 commits into from
Sep 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
399 changes: 222 additions & 177 deletions packages/jest-haste-map/src/__tests__/index.test.js

Large diffs are not rendered by default.

70 changes: 40 additions & 30 deletions packages/jest-haste-map/src/crawlers/__tests__/node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ jest.mock('fs', () => {
});

const pearMatcher = path => /pear/.test(path);
const createMap = obj => new Map(Object.entries(obj));

let mockResponse;
let nodeCrawl;
Expand Down Expand Up @@ -104,7 +105,7 @@ describe('node crawler', () => {

const promise = nodeCrawl({
data: {
files: Object.create(null),
files: new Map(),
},
extensions: ['js', 'json'],
ignore: pearMatcher,
Expand All @@ -126,11 +127,13 @@ describe('node crawler', () => {

expect(data.files).not.toBe(null);

expect(data.files).toEqual({
'/fruits/strawberry.js': ['', 32, 0, [], null],
'/fruits/tomato.js': ['', 33, 0, [], null],
'/vegetables/melon.json': ['', 34, 0, [], null],
});
expect(data.files).toEqual(
createMap({
'/fruits/strawberry.js': ['', 32, 0, [], null],
'/fruits/tomato.js': ['', 33, 0, [], null],
'/vegetables/melon.json': ['', 34, 0, [], null],
}),
);
});

return promise;
Expand All @@ -141,26 +144,28 @@ describe('node crawler', () => {

nodeCrawl = require('../node');

const files = Object.create(null);

// In this test sample, strawberry is changed and tomato is unchanged
const tomato = ['', 33, 1, [], null];
files['/fruits/strawberry.js'] = ['', 30, 1, [], null];
files['/fruits/tomato.js'] = tomato;
const files = createMap({
'/fruits/strawberry.js': ['', 30, 1, [], null],
'/fruits/tomato.js': tomato,
});

return nodeCrawl({
data: {files},
extensions: ['js'],
ignore: pearMatcher,
roots: ['/fruits'],
}).then(data => {
expect(data.files).toEqual({
'/fruits/strawberry.js': ['', 32, 0, [], null],
'/fruits/tomato.js': tomato,
});
expect(data.files).toEqual(
createMap({
'/fruits/strawberry.js': ['', 32, 0, [], null],
'/fruits/tomato.js': tomato,
}),
);

// Make sure it is the *same* unchanged object.
expect(data.files['/fruits/tomato.js']).toBe(tomato);
expect(data.files.get('/fruits/tomato.js')).toBe(tomato);
});
});

Expand All @@ -169,17 +174,20 @@ describe('node crawler', () => {

nodeCrawl = require('../node');

const files = Object.create(null);
return nodeCrawl({
data: {files},
data: {
files: new Map(),
},
extensions: ['js'],
ignore: pearMatcher,
roots: ['/fruits'],
}).then(data => {
expect(data.files).toEqual({
'/fruits/directory/strawberry.js': ['', 33, 0, [], null],
'/fruits/tomato.js': ['', 32, 0, [], null],
});
expect(data.files).toEqual(
createMap({
'/fruits/directory/strawberry.js': ['', 33, 0, [], null],
'/fruits/tomato.js': ['', 32, 0, [], null],
}),
);
});
});

Expand All @@ -188,18 +196,20 @@ describe('node crawler', () => {

nodeCrawl = require('../node');

const files = Object.create(null);
const files = new Map();
return nodeCrawl({
data: {files},
extensions: ['js'],
forceNodeFilesystemAPI: true,
ignore: pearMatcher,
roots: ['/fruits'],
}).then(data => {
expect(data.files).toEqual({
'/fruits/directory/strawberry.js': ['', 33, 0, [], null],
'/fruits/tomato.js': ['', 32, 0, [], null],
});
expect(data.files).toEqual(
createMap({
'/fruits/directory/strawberry.js': ['', 33, 0, [], null],
'/fruits/tomato.js': ['', 32, 0, [], null],
}),
);
});
});

Expand All @@ -208,15 +218,15 @@ describe('node crawler', () => {

nodeCrawl = require('../node');

const files = Object.create(null);
const files = new Map();
return nodeCrawl({
data: {files},
extensions: ['js'],
forceNodeFilesystemAPI: true,
ignore: pearMatcher,
roots: [],
}).then(data => {
expect(data.files).toEqual({});
expect(data.files).toEqual(new Map());
});
});

Expand All @@ -225,14 +235,14 @@ describe('node crawler', () => {

nodeCrawl = require('../node');

const files = Object.create(null);
const files = new Map();
return nodeCrawl({
data: {files},
extensions: ['js'],
ignore: pearMatcher,
roots: ['/error'],
}).then(data => {
expect(data.files).toEqual({});
expect(data.files).toEqual(new Map());
});
});
});
114 changes: 66 additions & 48 deletions packages/jest-haste-map/src/crawlers/__tests__/watchman.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const WATCH_PROJECT_MOCK = {
},
};

const objectToMap = obj => new Map(Object.entries(obj));

describe('watchman watch', () => {
beforeEach(() => {
watchmanCrawl = require('../watchman');
Expand Down Expand Up @@ -97,7 +99,7 @@ describe('watchman watch', () => {
'watch-project': WATCH_PROJECT_MOCK,
};

mockFiles = Object.assign(Object.create(null), {
mockFiles = objectToMap({
[MELON]: ['', 33, 0, [], null],
[STRAWBERRY]: ['', 30, 0, [], null],
[TOMATO]: ['', 31, 0, [], null],
Expand All @@ -111,8 +113,8 @@ describe('watchman watch', () => {
test('returns a list of all files when there are no clocks', () =>
watchmanCrawl({
data: {
clocks: Object.create(null),
files: Object.create(null),
clocks: new Map(),
files: new Map(),
},
extensions: ['js', 'json'],
ignore: pearMatcher,
Expand Down Expand Up @@ -148,9 +150,11 @@ describe('watchman watch', () => {
'vegetables/**/*.json',
]);

expect(data.clocks).toEqual({
[ROOT_MOCK]: 'c:fake-clock:1',
});
expect(data.clocks).toEqual(
objectToMap({
[ROOT_MOCK]: 'c:fake-clock:1',
}),
);

expect(data.files).toEqual(mockFiles);

Expand Down Expand Up @@ -186,7 +190,7 @@ describe('watchman watch', () => {
'watch-project': WATCH_PROJECT_MOCK,
};

const clocks = Object.assign(Object.create(null), {
const clocks = objectToMap({
[ROOT_MOCK]: 'c:fake-clock:1',
});

Expand All @@ -202,15 +206,19 @@ describe('watchman watch', () => {
// The object was reused.
expect(data.files).toBe(mockFiles);

expect(data.clocks).toEqual({
[ROOT_MOCK]: 'c:fake-clock:2',
});

expect(data.files).toEqual({
[KIWI]: ['', 42, 0, [], null],
[MELON]: ['', 33, 0, [], null],
[STRAWBERRY]: ['', 30, 0, [], null],
});
expect(data.clocks).toEqual(
objectToMap({
[ROOT_MOCK]: 'c:fake-clock:2',
}),
);

expect(data.files).toEqual(
objectToMap({
[KIWI]: ['', 42, 0, [], null],
[MELON]: ['', 33, 0, [], null],
[STRAWBERRY]: ['', 30, 0, [], null],
}),
);
});
});

Expand Down Expand Up @@ -249,9 +257,9 @@ describe('watchman watch', () => {
};

const mockMetadata = ['Banana', 41, 1, ['Raspberry'], null];
mockFiles[BANANA] = mockMetadata;
mockFiles.set(BANANA, mockMetadata);

const clocks = Object.assign(Object.create(null), {
const clocks = objectToMap({
[ROOT_MOCK]: 'c:fake-clock:1',
});

Expand All @@ -267,22 +275,26 @@ describe('watchman watch', () => {
// The file object was *not* reused.
expect(data.files).not.toBe(mockFiles);

expect(data.clocks).toEqual({
[ROOT_MOCK]: 'c:fake-clock:3',
});
expect(data.clocks).toEqual(
objectToMap({
[ROOT_MOCK]: 'c:fake-clock:3',
}),
);

// /fruits/strawberry.js was removed from the file list.
expect(data.files).toEqual({
[BANANA]: mockMetadata,
[KIWI]: ['', 42, 0, [], null],
[TOMATO]: mockFiles[TOMATO],
});
expect(data.files).toEqual(
objectToMap({
[BANANA]: mockMetadata,
[KIWI]: ['', 42, 0, [], null],
[TOMATO]: mockFiles.get(TOMATO),
}),
);

// Even though the file list was reset, old file objects are still reused
// if no changes have been made.
expect(data.files[BANANA]).toBe(mockMetadata);
expect(data.files.get(BANANA)).toBe(mockMetadata);

expect(data.files[TOMATO]).toBe(mockFiles[TOMATO]);
expect(data.files.get(TOMATO)).toBe(mockFiles.get(TOMATO));
});
});

Expand Down Expand Up @@ -329,7 +341,7 @@ describe('watchman watch', () => {
},
};

const clocks = Object.assign(Object.create(null), {
const clocks = objectToMap({
[FRUITS]: 'c:fake-clock:1',
[VEGETABLES]: 'c:fake-clock:2',
});
Expand All @@ -343,15 +355,19 @@ describe('watchman watch', () => {
ignore: pearMatcher,
roots: ROOTS,
}).then(data => {
expect(data.clocks).toEqual({
[FRUITS]: 'c:fake-clock:3',
[VEGETABLES]: 'c:fake-clock:4',
});

expect(data.files).toEqual({
[KIWI]: ['', 42, 0, [], null],
[MELON]: ['', 33, 0, [], null],
});
expect(data.clocks).toEqual(
objectToMap({
[FRUITS]: 'c:fake-clock:3',
[VEGETABLES]: 'c:fake-clock:4',
}),
);

expect(data.files).toEqual(
objectToMap({
[KIWI]: ['', 42, 0, [], null],
[MELON]: ['', 33, 0, [], null],
}),
);
});
});

Expand Down Expand Up @@ -387,8 +403,8 @@ describe('watchman watch', () => {

return watchmanCrawl({
data: {
clocks: Object.create(null),
files: Object.create(null),
clocks: new Map(),
files: new Map(),
},
extensions: ['js', 'json'],
ignore: pearMatcher,
Expand Down Expand Up @@ -419,11 +435,13 @@ describe('watchman watch', () => {

expect(query[2].glob).toEqual(['**/*.js', '**/*.json']);

expect(data.clocks).toEqual({
[ROOT_MOCK]: 'c:fake-clock:1',
});
expect(data.clocks).toEqual(
objectToMap({
[ROOT_MOCK]: 'c:fake-clock:1',
}),
);

expect(data.files).toEqual({});
expect(data.files).toEqual(objectToMap({}));

expect(client.end).toBeCalled();
});
Expand Down Expand Up @@ -454,8 +472,8 @@ describe('watchman watch', () => {
await watchmanCrawl({
computeSha1: true,
data: {
clocks: Object.create(null),
files: Object.create(null),
clocks: new Map(),
files: new Map(),
},
extensions: ['js', 'json'],
roots: [ROOT_MOCK],
Expand Down Expand Up @@ -493,8 +511,8 @@ describe('watchman watch', () => {
await watchmanCrawl({
computeSha1: true,
data: {
clocks: Object.create(null),
files: Object.create(null),
clocks: new Map(),
files: new Map(),
},
extensions: ['js', 'json'],
roots: [ROOT_MOCK],
Expand Down
Loading