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

Speed up no-exclusive-tests and no-pending-tests #276

Merged
merged 6 commits into from
Mar 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/runtime.bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function lintManyFilesWithAllRecommendedRules({ numberOfFiles }) {
describe('runtime', () => {
it('should not take longer as the defined budget to lint many files with the recommended config', () => {
const nodeVersionMultiplier = getNodeVersionMultiplier();
const budget = 80000000 / cpuSpeed * nodeVersionMultiplier;
const budget = 70000000 / cpuSpeed * nodeVersionMultiplier;

const { medianDuration } = runBenchmark(() => {
lintManyFilesWithAllRecommendedRules({ numberOfFiles: 350 });
Expand Down
8 changes: 5 additions & 3 deletions lib/rules/no-exclusive-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ module.exports = {
create(context) {
const astUtils = createAstUtils(context.settings);

const options = { modifiers: [ 'only' ], modifiersOnly: true };
const isDescribe = astUtils.buildIsDescribeAnswerer(options);
const isTestCase = astUtils.buildIsTestCaseAnswerer(options);

return {
CallExpression(node) {
const options = { modifiers: [ 'only' ], modifiersOnly: true };

if (astUtils.isDescribe(node, options) || astUtils.isTestCase(node, options)) {
if (isDescribe(node) || isTestCase(node)) {
const callee = node.callee;

context.report({
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-pending-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ module.exports = {
},
create(context) {
const astUtils = createAstUtils(context.settings);
const isTestCase = astUtils.buildIsTestCaseAnswerer();

function isPendingMochaTest(node) {
return astUtils.isTestCase(node) &&
return isTestCase(node) &&
node.arguments.length === 1 &&
node.arguments[0].type === 'Literal';
}
Expand Down
21 changes: 16 additions & 5 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,30 @@ function isFunctionCallWithName(node, names) {
return isCallExpression(node) && names.includes(getNodeName(node.callee));
}

// eslint-disable-next-line max-statements
function createAstUtils(settings) {
const additionalCustomNames = getAddtionalNames(settings);

function isDescribe(node, options = {}) {
function buildIsDescribeAnswerer(options) {
const { modifiers = [ 'skip', 'only' ], modifiersOnly = false } = options;
const describeAliases = getSuiteNames({ modifiersOnly, modifiers, additionalCustomNames });

return isFunctionCallWithName(node, describeAliases);
return (node) => isFunctionCallWithName(node, describeAliases);
}

function isTestCase(node, options = {}) {
function isDescribe(node, options = {}) {
return buildIsDescribeAnswerer(options)(node);
}

function buildIsTestCaseAnswerer(options = {}) {
const { modifiers = [ 'skip', 'only' ], modifiersOnly = false } = options;
const testCaseNames = getTestCaseNames({ modifiersOnly, modifiers, additionalCustomNames });

return isFunctionCallWithName(node, testCaseNames);
return (node) => isFunctionCallWithName(node, testCaseNames);
}

function isTestCase(node, options = {}) {
return buildIsTestCaseAnswerer(options)(node);
}

function isSuiteConfigExpression(node) {
Expand Down Expand Up @@ -132,7 +141,9 @@ function createAstUtils(settings) {
isSuiteConfigCall,
hasParentMochaFunctionCall,
findReturnStatement,
isReturnOfUndefined
isReturnOfUndefined,
buildIsDescribeAnswerer,
buildIsTestCaseAnswerer
};
}

Expand Down