Skip to content

Commit

Permalink
Issue 8547: side effect import deps extracted (#8670)
Browse files Browse the repository at this point in the history
  • Loading branch information
astigefb authored and SimenB committed Jul 15, 2019
1 parent a36f82d commit b76d46a
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 @@ -16,6 +16,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 b76d46a

Please sign in to comment.