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

Fix "container is falsey" error when using code coverage #156

Merged
merged 9 commits into from Feb 11, 2021
41 changes: 41 additions & 0 deletions __tests__/index-test.js
Expand Up @@ -457,3 +457,44 @@ let MyController = (_dec = Ember._action, (_class = class MyController extends E
export { MyController as default };`);
});
});

describe('when used with babel-plugin-istanbul', () => {
// babel-plugin-istanbul won't run on <= Node 6
const majorVersion = parseInt(process.version.match(/^v(\d+)\./)[1], 10);
const runOrSkip = majorVersion > 6 ? it : it.skip;
Comment on lines +462 to +464
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice one, thank you!


runOrSkip('works with mixins', () => {
let source = `
import EmberObject from '@ember/object';
import Evented from '@ember/object/evented';

const TestObject = EmberObject.extend(Evented);
export default TestObject;
`;

let actual = babel7.transformSync(source, {
filename: 'istanbul-should-cover.js',
plugins: [require('babel-plugin-istanbul'), Plugin],
}).code;

expect(actual).toContain('Ember.Object.extend(Ember.Evented)');
});

runOrSkip('works with classes that extend from mixins', () => {
let source = `
import EmberObject from '@ember/object';
import Evented from '@ember/object/evented';

export default class TestObject extends EmberObject.extend(Evented) {};
`;

let actual = babel7.transformSync(source, {
filename: 'istanbul-should-cover.js',
plugins: [require('babel-plugin-istanbul'), Plugin],
}).code;

expect(actual).toContain(
'export default class TestObject extends (Ember.Object.extend(Ember.Evented)) {}'
);
});
});
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -38,6 +38,7 @@
"babel-core": "^6.25.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"babel-plugin-istanbul": "^6.0.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^24.9.0",
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -183,6 +183,7 @@ module.exports = function (babel) {
])
);
} else {
// Replace the occurences of the imported name with the global name.
let binding = path.scope.getBinding(local.name);
let referencePaths = binding.referencePaths;

Expand Down Expand Up @@ -211,10 +212,17 @@ module.exports = function (babel) {
});
}

// Replace the occurences of the imported name with the global name.
// Replace the occurrences of the imported name with the global name.
referencePaths.forEach((referencePath) => {
if (!isTypescriptNode(referencePath.parentPath)) {
referencePath.replaceWith(getMemberExpressionFor(global));
const memberExpression = getMemberExpressionFor(global);

try {
referencePath.replaceWith(memberExpression);
} catch (e) {
referencePath.scope.crawl();
referencePath.replaceWith(memberExpression);
}
}
});
}
Expand Down