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

haste-map: Support extracting dynamic imports #5883

Merged
merged 4 commits into from
Mar 28, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features

* `[jest-haste-map]` Support extracting dynamic `import`s (TBD)
* `[expect]` Improve output format for mismatchedArgs in mock/spy calls.
([#5846](https://github.com/facebook/jest/pull/5846))
* `[jest-cli]` Add support for using `--coverage` in combination with watch
Expand Down
4 changes: 1 addition & 3 deletions packages/jest-haste-map/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import crypto from 'crypto';
import EventEmitter from 'events';
import getMockName from './get_mock_name';
import getPlatformExtension from './lib/get_platform_extension';
// eslint-disable-next-line import/no-duplicates
import H from './constants';
import HasteFS from './haste_fs';
import HasteModuleMap from './module_map';
Expand All @@ -41,8 +40,7 @@ import type {
MockData,
} from 'types/HasteMap';

// eslint-disable-next-line import/no-duplicates
import typeof HType from './constants';
type HType = typeof H;

type Options = {
cacheDirectory?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ it('extracts both requires and imports from code', () => {
const code = `
import module1 from 'module1';
const module2 = require('module2');
import('module3').then(module3 => {})';
`;

expect(extractRequires(code)).toEqual(['module1', 'module2']);
expect(extractRequires(code)).toEqual(['module1', 'module2', 'module3']);
});

it('extracts requires in order', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-haste-map/src/lib/extract_requires.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const blockCommentRe = /\/\*[^]*?\*\//g;
const lineCommentRe = /\/\/.*/g;

const replacePatterns = {
DYNAMIC_IMPORT_RE: /(?:^|[^.]\s*)(\import\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
EXPORT_RE: /(\bexport\s+(?!type )(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
IMPORT_RE: /(\bimport\s+(?!type )(?:[^'"]+\s+from\s+)??)(['"])([^'"]+)(\2)/g,
REQUIRE_EXTENSIONS_PATTERN: /(?:^|[^.]\s*)(\b(?:require\s*?\.\s*?(?:requireActual|requireMock)|jest\s*?\.\s*?(?:requireActual|requireMock|genMockFromModule))\s*?\(\s*?)([`'"])([^`'"]+)(\2\s*?\))/g,
Expand All @@ -30,7 +31,8 @@ export default function extractRequires(code: string): Array<string> {
.replace(replacePatterns.EXPORT_RE, addDependency)
.replace(replacePatterns.IMPORT_RE, addDependency)
.replace(replacePatterns.REQUIRE_EXTENSIONS_PATTERN, addDependency)
.replace(replacePatterns.REQUIRE_RE, addDependency);
.replace(replacePatterns.REQUIRE_RE, addDependency)
.replace(replacePatterns.DYNAMIC_IMPORT_RE, addDependency);

return Array.from(dependencies);
}