Skip to content

Commit

Permalink
Issue 8547: side effect import deps extracted
Browse files Browse the repository at this point in the history
ed4edd8 -> red
965a24c -> green
c4bbbba -> Refinement
  • Loading branch information
astigefb committed Jul 10, 2019
1 parent a501718 commit f343f81
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

### Fixes

- `[jest-cli]` Detect side-effect only imports when running `--onlyChanged` or `--changedSince` ([#8670](https://github.com/facebook/jest/pull/8670))
- `[jest-cli]` Allow `--maxWorkers` to work with % input again ([#8565](https://github.com/facebook/jest/pull/8565))
- `[babel-plugin-jest-hoist]` Expand list of whitelisted globals in global mocks ([#8429](https://github.com/facebook/jest/pull/8429)
- `[jest-core]` Make watch plugin initialization errors look nice ([#8422](https://github.com/facebook/jest/pull/8422))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ describe('dependencyExtractor', () => {
it('should not extract dependencies inside comments', () => {
const code = `
// import a from 'ignore-line-comment';
// import 'ignore-line-comment';
// import './ignore-line-comment';
// require('ignore-line-comment');
/*
* import a from 'ignore-block-comment';
* import './ignore-block-comment';
* import 'ignore-block-comment';
* require('ignore-block-comment');
*/
`;
Expand Down Expand Up @@ -67,6 +71,23 @@ describe('dependencyExtractor', () => {
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
});

// https://github.com/facebook/jest/issues/8547
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only
it('should extract dependencies from side-effect only `import` statements', () => {
const code = `
// Good
import './side-effect-dep1';
import 'side-effect-dep2';
// Bad
import ./inv1;
import inv2
`;
expect(extract(code)).toEqual(
new Set(['./side-effect-dep1', 'side-effect-dep2']),
);
});

it('should not extract dependencies from `import type/typeof` statements', () => {
const code = `
// Bad
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-haste-map/src/lib/dependencyExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const REQUIRE_OR_DYNAMIC_IMPORT_RE = createRegExp(

const IMPORT_OR_EXPORT_RE = createRegExp(
[
'\\b(?:import|export)\\s+(?!type(?:of)?\\s+)[^\'"]+\\s+from\\s+',
'\\b(?:import|export)\\s+(?!type(?:of)?\\s+)(?:[^\'"]+\\s+from\\s+)?',
CAPTURE_STRING_LITERAL(1),
],
'g',
Expand Down

0 comments on commit f343f81

Please sign in to comment.