Skip to content

Commit 6daa634

Browse files
committed
don't write imports to test helpers if they aren't actually used in the file after transformation
1 parent 3cc033a commit 6daa634

7 files changed

Lines changed: 33 additions & 4 deletions

File tree

transforms/integration/__testfixtures__/default-component-test.output.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { find } from '@ember/test-helpers';
21
import { moduleForComponent, test } from 'ember-qunit';
32
import hbs from 'htmlbars-inline-precompile';
43

transforms/native-dom/__testfixtures__/context-argument.output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { find, findAll, visit, click, fillIn } from '@ember/test-helpers';
1+
import { find, findAll, visit, click } from '@ember/test-helpers';
22
import { test } from 'qunit';
33
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
44

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
import { click, currentURL } from 'ember-native-dom-helpers';
22
import { find, visit } from 'ember-native-dom-helpers';
3+
4+
test('visiting /foo', async function(assert) {
5+
await visit('/foo');
6+
await click('.foo');
7+
assert.ok(find('.foo'));
8+
assert.ok(currentURL());
9+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
import { click, currentURL, find, visit } from '@ember/test-helpers';
2+
3+
test('visiting /foo', async function(assert) {
4+
await visit('/foo');
5+
await click('.foo');
6+
assert.ok(find('.foo'));
7+
assert.ok(currentURL());
8+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import { click } from 'ember-native-dom-helpers';
2+
3+
click('.foo');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
import { click } from '@ember/test-helpers';
2+
3+
click('.foo');

transforms/utils.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,25 @@ function writeImportStatements(j, root) {
360360
source: { value: '@ember/test-helpers' }
361361
});
362362

363+
let actuallyNeedsImport = Array.from(_statementsToImport).filter(methodName => {
364+
return root.find(j.CallExpression, {
365+
callee: {
366+
name: methodName
367+
}
368+
}).length > 0;
369+
});
370+
371+
if (actuallyNeedsImport.length === 0) {
372+
return;
373+
}
374+
363375
if (importStatement.length === 0) {
364-
importStatement = createImportStatement(j, '@ember/test-helpers', 'default', Array.from(_statementsToImport));
376+
importStatement = createImportStatement(j, '@ember/test-helpers', 'default', actuallyNeedsImport);
365377
body.unshift(importStatement);
366378
} else {
367379
let existingSpecifiers = importStatement.get("specifiers");
368380

369-
_statementsToImport.forEach(name => {
381+
actuallyNeedsImport.forEach(name => {
370382
if (existingSpecifiers.filter(exSp => exSp.value.imported.name === name).length === 0) {
371383
existingSpecifiers.push(j.importSpecifier(j.identifier(name)));
372384
}

0 commit comments

Comments
 (0)