UNPKG

eslint-plugin-mocha

Version:

Eslint rules for mocha.

101 lines 3.77 kB
import { isConstantPath } from '../ast/member-expression.js'; import { filterWithArgs, flatMapWithArgs, mapWithArgs } from '../list.js'; import { configCallNames } from './descriptors.js'; function hasMatchingType(item, typesToMatch) { return typesToMatch.includes(item.type); } const testCaseAndSuite = ['testCase', 'suite']; function filterTestCasesAndSuites(nameDetailsList) { return filterWithArgs(nameDetailsList, hasMatchingType, testCaseAndSuite); } function hasMatchingInterface(item, interfaceToMatch) { return interfaceToMatch === item.interface; } function filterByInterface(nameDetailsList, interfaceToMatch) { return filterWithArgs(nameDetailsList, hasMatchingInterface, interfaceToMatch); } function formatXVariant(nameDetails) { const [firstPathSegment, ...remainingPath] = nameDetails.path; return { ...nameDetails, path: [`x${firstPathSegment}`, ...remainingPath], modifier: 'pending' }; } function buildXVariants(nameDetailsList) { const testCasesAndSuites = filterTestCasesAndSuites(nameDetailsList); return filterByInterface(testCasesAndSuites, 'BDD').map(formatXVariant); } function formatSkipVariant(nameDetails) { return { ...nameDetails, path: [...nameDetails.path, 'skip'], modifier: 'pending' }; } function buildSkipVariants(nameDetailsList) { const testCasesAndSuites = filterTestCasesAndSuites(nameDetailsList); return testCasesAndSuites.map(formatSkipVariant); } function formatExclusiveVariants(nameDetails) { return { ...nameDetails, path: [...nameDetails.path, 'only'], modifier: 'exclusive' }; } function buildExclusiveVariants(nameDetailsList) { const testCasesAndSuites = filterTestCasesAndSuites(nameDetailsList); return testCasesAndSuites.map(formatExclusiveVariants); } function formatConfigVariant(nameDetails, config) { return { ...nameDetails, config, type: 'config', path: [...nameDetails.path, config], normalizedPath: [...nameDetails.normalizedPath, `${config}()`] }; } function buildConfigVariantsForConfig(config, nameDetailsList) { return mapWithArgs(nameDetailsList, formatConfigVariant, config); } function buildConfigVariants(nameDetailsList) { return flatMapWithArgs(configCallNames, buildConfigVariantsForConfig, nameDetailsList); } const callExpressionSuffix = '()'; export function reformatLastPathSegmentWithCallExpressions(path, amountOfCallExpressions) { if (!isConstantPath(path)) { return path; } const mutablePath = Array.from(path); const segment = mutablePath.pop(); const suffix = callExpressionSuffix.repeat(amountOfCallExpressions); return [...mutablePath, `${segment}${suffix}`]; } export function stripCallExpressions(path) { return path.map((segment) => { if (segment.endsWith(callExpressionSuffix)) { return segment.slice(0, -callExpressionSuffix.length); } return segment; }); } function normalizePathForNameDetails(nameDetails) { return { ...nameDetails, normalizedPath: reformatLastPathSegmentWithCallExpressions(nameDetails.path, 1), path: stripCallExpressions(nameDetails.path) }; } export function buildAllNameDetailsWithVariants(baseNameDetailsList) { const allNameDetails = [ ...baseNameDetailsList, ...buildSkipVariants(baseNameDetailsList), ...buildXVariants(baseNameDetailsList), ...buildExclusiveVariants(baseNameDetailsList) ]; const allNormalizedNameDetails = allNameDetails.map(normalizePathForNameDetails); return [...allNormalizedNameDetails, ...buildConfigVariants(allNormalizedNameDetails)]; } //# sourceMappingURL=name-details.js.map