Skip to content

Commit b76d46a

Browse files
astigefbSimenB
authored andcommitted
Issue 8547: side effect import deps extracted (#8670)
1 parent a36f82d commit b76d46a

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
### Fixes
1818

19+
- `[jest-cli]` Detect side-effect only imports when running `--onlyChanged` or `--changedSince` ([#8670](https://github.com/facebook/jest/pull/8670))
1920
- `[jest-cli]` Allow `--maxWorkers` to work with % input again ([#8565](https://github.com/facebook/jest/pull/8565))
2021
- `[babel-plugin-jest-hoist]` Expand list of whitelisted globals in global mocks ([#8429](https://github.com/facebook/jest/pull/8429)
2122
- `[jest-core]` Make watch plugin initialization errors look nice ([#8422](https://github.com/facebook/jest/pull/8422))

packages/jest-haste-map/src/lib/__tests__/dependencyExtractor.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ describe('dependencyExtractor', () => {
1414
it('should not extract dependencies inside comments', () => {
1515
const code = `
1616
// import a from 'ignore-line-comment';
17+
// import 'ignore-line-comment';
18+
// import './ignore-line-comment';
1719
// require('ignore-line-comment');
1820
/*
1921
* import a from 'ignore-block-comment';
22+
* import './ignore-block-comment';
23+
* import 'ignore-block-comment';
2024
* require('ignore-block-comment');
2125
*/
2226
`;
@@ -67,6 +71,23 @@ describe('dependencyExtractor', () => {
6771
expect(extract(code)).toEqual(new Set(['dep1', 'dep2', 'dep3', 'dep4']));
6872
});
6973

74+
// https://github.com/facebook/jest/issues/8547
75+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only
76+
it('should extract dependencies from side-effect only `import` statements', () => {
77+
const code = `
78+
// Good
79+
import './side-effect-dep1';
80+
import 'side-effect-dep2';
81+
82+
// Bad
83+
import ./inv1;
84+
import inv2
85+
`;
86+
expect(extract(code)).toEqual(
87+
new Set(['./side-effect-dep1', 'side-effect-dep2']),
88+
);
89+
});
90+
7091
it('should not extract dependencies from `import type/typeof` statements', () => {
7192
const code = `
7293
// Bad

packages/jest-haste-map/src/lib/dependencyExtractor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const REQUIRE_OR_DYNAMIC_IMPORT_RE = createRegExp(
5454

5555
const IMPORT_OR_EXPORT_RE = createRegExp(
5656
[
57-
'\\b(?:import|export)\\s+(?!type(?:of)?\\s+)[^\'"]+\\s+from\\s+',
57+
'\\b(?:import|export)\\s+(?!type(?:of)?\\s+)(?:[^\'"]+\\s+from\\s+)?',
5858
CAPTURE_STRING_LITERAL(1),
5959
],
6060
'g',

0 commit comments

Comments
 (0)