From 7e7ec092200afb08ddf0be8cb8ea72e1742969a5 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 7 Feb 2022 00:33:18 +0100 Subject: [PATCH 1/2] chore: avoid using anonymous default exports --- .eslintrc.js | 12 ++ packages/babel-plugin-jest-hoist/src/index.ts | 172 +++++++++--------- packages/diff-sequences/src/index.ts | 6 +- .../legacy-code-todo-rewrite/jestExpect.ts | 6 +- packages/jest-cli/src/init/index.ts | 6 +- packages/jest-config/src/resolveConfigPath.ts | 6 +- packages/jest-console/src/getConsoleOutput.ts | 6 +- .../jest-core/src/getChangedFilesPromise.ts | 6 +- packages/jest-core/src/lib/createContext.ts | 16 +- .../src/lib/handleDeprecationWarnings.ts | 7 +- .../jest-core/src/lib/updateGlobalConfig.ts | 6 +- packages/jest-core/src/runGlobalHook.ts | 6 +- .../src/index.ts | 6 +- packages/jest-each/src/bind.ts | 12 +- packages/jest-each/src/table/array.ts | 7 +- packages/jest-each/src/table/template.ts | 6 +- packages/jest-jasmine2/src/each.ts | 4 +- packages/jest-jasmine2/src/jasmine/Env.ts | 2 +- packages/jest-jasmine2/src/jestExpect.ts | 4 +- .../jest-jasmine2/src/setup_jest_globals.ts | 6 +- packages/jest-leak-detector/src/index.ts | 2 +- .../src/generateEmptyCoverage.ts | 2 +- .../jest-reporters/src/getResultHeader.ts | 6 +- .../jest-reporters/src/getSnapshotStatus.ts | 6 +- .../jest-reporters/src/getSnapshotSummary.ts | 6 +- packages/jest-source-map/src/getCallsite.ts | 6 +- packages/jest-util/src/createProcessObject.ts | 2 +- .../jest-util/src/installCommonGlobals.ts | 2 +- packages/jest-util/src/setGlobal.ts | 6 +- .../jest-util/src/testPathPatternToRegExp.ts | 6 +- packages/jest-watcher/src/lib/colorize.ts | 15 +- .../src/lib/formatTestNameByPattern.ts | 8 +- 32 files changed, 206 insertions(+), 163 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 3608aed77d00..4fb5161b3ab1 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -185,6 +185,18 @@ module.exports = { files: 'packages/**/*.ts', rules: { '@typescript-eslint/explicit-module-boundary-types': 'error', + 'import/no-anonymous-default-export': [ + 'error', + { + allowAnonymousClass: false, + allowAnonymousFunction: false, + allowArray: false, + allowArrowFunction: false, + allowCallExpression: false, + allowLiteral: false, + allowObject: true, + }, + ], }, }, { diff --git a/packages/babel-plugin-jest-hoist/src/index.ts b/packages/babel-plugin-jest-hoist/src/index.ts index cc3aeba8431a..0d47bb6ac05b 100644 --- a/packages/babel-plugin-jest-hoist/src/index.ts +++ b/packages/babel-plugin-jest-hoist/src/index.ts @@ -268,102 +268,106 @@ const extractJestObjExprIfHoistable = ( }; /* eslint-disable sort-keys */ -export default (): PluginObj<{ +export default function jestHoist(): PluginObj<{ declareJestObjGetterIdentifier: () => Identifier; jestObjGetterIdentifier?: Identifier; -}> => ({ - pre({path: program}) { - this.declareJestObjGetterIdentifier = () => { - if (this.jestObjGetterIdentifier) { - return this.jestObjGetterIdentifier; - } +}> { + return { + pre({path: program}) { + this.declareJestObjGetterIdentifier = () => { + if (this.jestObjGetterIdentifier) { + return this.jestObjGetterIdentifier; + } - this.jestObjGetterIdentifier = - program.scope.generateUidIdentifier('getJestObj'); + this.jestObjGetterIdentifier = + program.scope.generateUidIdentifier('getJestObj'); - program.unshiftContainer('body', [ - createJestObjectGetter({ - GETTER_NAME: this.jestObjGetterIdentifier.name, - JEST_GLOBALS_MODULE_JEST_EXPORT_NAME, - JEST_GLOBALS_MODULE_NAME, - }), - ]); + program.unshiftContainer('body', [ + createJestObjectGetter({ + GETTER_NAME: this.jestObjGetterIdentifier.name, + JEST_GLOBALS_MODULE_JEST_EXPORT_NAME, + JEST_GLOBALS_MODULE_NAME, + }), + ]); - return this.jestObjGetterIdentifier; - }; - }, - visitor: { - ExpressionStatement(exprStmt) { - const jestObjExpr = extractJestObjExprIfHoistable( - exprStmt.get('expression'), - ); - if (jestObjExpr) { - jestObjExpr.replaceWith( - callExpression(this.declareJestObjGetterIdentifier(), []), + return this.jestObjGetterIdentifier; + }; + }, + visitor: { + ExpressionStatement(exprStmt) { + const jestObjExpr = extractJestObjExprIfHoistable( + exprStmt.get('expression'), ); - } + if (jestObjExpr) { + jestObjExpr.replaceWith( + callExpression(this.declareJestObjGetterIdentifier(), []), + ); + } + }, }, - }, - // in `post` to make sure we come after an import transform and can unshift above the `require`s - post({path: program}) { - const self = this; - - visitBlock(program); - program.traverse({BlockStatement: visitBlock}); - - function visitBlock(block: NodePath | NodePath) { - // use a temporary empty statement instead of the real first statement, which may itself be hoisted - const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer('body', [ - emptyStatement(), - emptyStatement(), - ]); - block.traverse({ - CallExpression: visitCallExpr, - VariableDeclarator: visitVariableDeclarator, - // do not traverse into nested blocks, or we'll hoist calls in there out to this block - blacklist: ['BlockStatement'], - }); - callsHoistPoint.remove(); - varsHoistPoint.remove(); - - function visitCallExpr(callExpr: NodePath) { - const { - node: {callee}, - } = callExpr; - if ( - isIdentifier(callee) && - callee.name === self.jestObjGetterIdentifier?.name - ) { - const mockStmt = callExpr.getStatementParent(); - - if (mockStmt) { - const mockStmtParent = mockStmt.parentPath; - if (mockStmtParent.isBlock()) { - const mockStmtNode = mockStmt.node; - mockStmt.remove(); - callsHoistPoint.insertBefore(mockStmtNode); + // in `post` to make sure we come after an import transform and can unshift above the `require`s + post({path: program}) { + const self = this; + + visitBlock(program); + program.traverse({BlockStatement: visitBlock}); + + function visitBlock(block: NodePath | NodePath) { + // use a temporary empty statement instead of the real first statement, which may itself be hoisted + const [varsHoistPoint, callsHoistPoint] = block.unshiftContainer( + 'body', + [emptyStatement(), emptyStatement()], + ); + block.traverse({ + CallExpression: visitCallExpr, + VariableDeclarator: visitVariableDeclarator, + // do not traverse into nested blocks, or we'll hoist calls in there out to this block + blacklist: ['BlockStatement'], + }); + callsHoistPoint.remove(); + varsHoistPoint.remove(); + + function visitCallExpr(callExpr: NodePath) { + const { + node: {callee}, + } = callExpr; + if ( + isIdentifier(callee) && + callee.name === self.jestObjGetterIdentifier?.name + ) { + const mockStmt = callExpr.getStatementParent(); + + if (mockStmt) { + const mockStmtParent = mockStmt.parentPath; + if (mockStmtParent.isBlock()) { + const mockStmtNode = mockStmt.node; + mockStmt.remove(); + callsHoistPoint.insertBefore(mockStmtNode); + } } } } - } - function visitVariableDeclarator(varDecl: NodePath) { - if (hoistedVariables.has(varDecl.node)) { - // should be assert function, but it's not. So let's cast below - varDecl.parentPath.assertVariableDeclaration(); - - const {kind, declarations} = varDecl.parent as VariableDeclaration; - if (declarations.length === 1) { - varDecl.parentPath.remove(); - } else { - varDecl.remove(); + function visitVariableDeclarator( + varDecl: NodePath, + ) { + if (hoistedVariables.has(varDecl.node)) { + // should be assert function, but it's not. So let's cast below + varDecl.parentPath.assertVariableDeclaration(); + + const {kind, declarations} = varDecl.parent as VariableDeclaration; + if (declarations.length === 1) { + varDecl.parentPath.remove(); + } else { + varDecl.remove(); + } + varsHoistPoint.insertBefore( + variableDeclaration(kind, [varDecl.node]), + ); } - varsHoistPoint.insertBefore( - variableDeclaration(kind, [varDecl.node]), - ); } } - } - }, -}); + }, + }; +} /* eslint-enable */ diff --git a/packages/diff-sequences/src/index.ts b/packages/diff-sequences/src/index.ts index cdbfe305b574..deabc2a13fc8 100644 --- a/packages/diff-sequences/src/index.ts +++ b/packages/diff-sequences/src/index.ts @@ -779,12 +779,12 @@ const validateCallback = (name: string, arg: unknown) => { // Given lengths of sequences and input function to compare items at indexes, // return by output function the number of adjacent items and starting indexes // of each common subsequence. -export default ( +export default function diffSequence( aLength: number, bLength: number, isCommon: IsCommon, foundSubsequence: FoundSubsequence, -): void => { +): void { validateLength('aLength', aLength); validateLength('bLength', bLength); validateCallback('isCommon', isCommon); @@ -869,4 +869,4 @@ export default ( foundSubsequence(nCommonR, aEnd, bEnd); } } -}; +} diff --git a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts index a13ce01bd3a8..aacd1208b954 100644 --- a/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts +++ b/packages/jest-circus/src/legacy-code-todo-rewrite/jestExpect.ts @@ -17,7 +17,9 @@ import { export type Expect = typeof expect; -export default (config: Pick): Expect => { +export default function jestExpect( + config: Pick, +): Expect { expect.setState({expand: config.expand}); expect.extend({ toMatchInlineSnapshot, @@ -29,4 +31,4 @@ export default (config: Pick): Expect => { expect.addSnapshotSerializer = addSerializer; return expect; -}; +} diff --git a/packages/jest-cli/src/init/index.ts b/packages/jest-cli/src/init/index.ts index 700be87b8728..1d7a6e382c26 100644 --- a/packages/jest-cli/src/init/index.ts +++ b/packages/jest-cli/src/init/index.ts @@ -37,9 +37,9 @@ type PromptsResults = { const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; -export default async ( +export default async function init( rootDir: string = tryRealpath(process.cwd()), -): Promise => { +): Promise { // prerequisite checks const projectPackageJsonPath: string = path.join(rootDir, PACKAGE_JSON); @@ -152,4 +152,4 @@ export default async ( console.log( `📝 Configuration file created at ${chalk.cyan(jestConfigPath)}`, ); -}; +} diff --git a/packages/jest-config/src/resolveConfigPath.ts b/packages/jest-config/src/resolveConfigPath.ts index 243a3616ec81..d47cd879aa92 100644 --- a/packages/jest-config/src/resolveConfigPath.ts +++ b/packages/jest-config/src/resolveConfigPath.ts @@ -21,11 +21,11 @@ const isFile = (filePath: Config.Path) => const getConfigFilename = (ext: string) => JEST_CONFIG_BASE_NAME + ext; -export default ( +export default function resolveConfigPath( pathToResolve: Config.Path, cwd: Config.Path, skipMultipleConfigWarning = false, -): Config.Path => { +): Config.Path { if (!path.isAbsolute(cwd)) { throw new Error(`"cwd" must be an absolute path. cwd: ${cwd}`); } @@ -61,7 +61,7 @@ export default ( cwd, skipMultipleConfigWarning, ); -}; +} const resolveConfigPathByTraversing = ( pathToResolve: Config.Path, diff --git a/packages/jest-console/src/getConsoleOutput.ts b/packages/jest-console/src/getConsoleOutput.ts index 9ff9d15ab95c..98764ca5ab60 100644 --- a/packages/jest-console/src/getConsoleOutput.ts +++ b/packages/jest-console/src/getConsoleOutput.ts @@ -14,11 +14,11 @@ import { } from 'jest-message-util'; import type {ConsoleBuffer} from './types'; -export default ( +export default function getConsoleOutput( buffer: ConsoleBuffer, config: StackTraceConfig, globalConfig: Config.GlobalConfig, -): string => { +): string { const TITLE_INDENT = globalConfig.verbose ? ' ' : ' '; const CONSOLE_INDENT = TITLE_INDENT + ' '; @@ -64,4 +64,4 @@ export default ( }, ''); return logEntries.trimRight() + '\n'; -}; +} diff --git a/packages/jest-core/src/getChangedFilesPromise.ts b/packages/jest-core/src/getChangedFilesPromise.ts index c96ea986be26..b628bfee04b7 100644 --- a/packages/jest-core/src/getChangedFilesPromise.ts +++ b/packages/jest-core/src/getChangedFilesPromise.ts @@ -10,10 +10,10 @@ import type {Config} from '@jest/types'; import {ChangedFilesPromise, getChangedFilesForRoots} from 'jest-changed-files'; import {formatExecError} from 'jest-message-util'; -export default ( +export default function getChangedFilesPromise( globalConfig: Config.GlobalConfig, configs: Array, -): ChangedFilesPromise | undefined => { +): ChangedFilesPromise | undefined { if (globalConfig.onlyChanged) { const allRootsForAllProjects = configs.reduce>( (roots, config) => { @@ -41,4 +41,4 @@ export default ( } return undefined; -}; +} diff --git a/packages/jest-core/src/lib/createContext.ts b/packages/jest-core/src/lib/createContext.ts index 0cd6c5b23677..0b5e3a7c8444 100644 --- a/packages/jest-core/src/lib/createContext.ts +++ b/packages/jest-core/src/lib/createContext.ts @@ -9,12 +9,14 @@ import type {Config} from '@jest/types'; import type {HasteMapObject} from 'jest-haste-map'; import Runtime, {Context} from 'jest-runtime'; -export default ( +export default function createContext( config: Config.ProjectConfig, {hasteFS, moduleMap}: HasteMapObject, -): Context => ({ - config, - hasteFS, - moduleMap, - resolver: Runtime.createResolver(config, moduleMap), -}); +): Context { + return { + config, + hasteFS, + moduleMap, + resolver: Runtime.createResolver(config, moduleMap), + }; +} diff --git a/packages/jest-core/src/lib/handleDeprecationWarnings.ts b/packages/jest-core/src/lib/handleDeprecationWarnings.ts index 83eea7aaf967..20571ba4cfd1 100644 --- a/packages/jest-core/src/lib/handleDeprecationWarnings.ts +++ b/packages/jest-core/src/lib/handleDeprecationWarnings.ts @@ -8,11 +8,11 @@ import chalk = require('chalk'); import {KEYS} from 'jest-watcher'; -export default ( +export default function handleDeprecationWarnings( pipe: NodeJS.WriteStream, stdin: NodeJS.ReadStream = process.stdin, -): Promise => - new Promise((resolve, reject) => { +): Promise { + return new Promise((resolve, reject) => { if (typeof stdin.setRawMode === 'function') { const messages = [ chalk.red('There are deprecation warnings.\n'), @@ -39,3 +39,4 @@ export default ( resolve(); } }); +} diff --git a/packages/jest-core/src/lib/updateGlobalConfig.ts b/packages/jest-core/src/lib/updateGlobalConfig.ts index 3e28231108da..4ba644d6bee9 100644 --- a/packages/jest-core/src/lib/updateGlobalConfig.ts +++ b/packages/jest-core/src/lib/updateGlobalConfig.ts @@ -13,10 +13,10 @@ type ExtraConfigOptions = Partial< Pick >; -export default ( +export default function updateGlobalConfig( globalConfig: Config.GlobalConfig, options: AllowedConfigOptions & ExtraConfigOptions = {}, -): Config.GlobalConfig => { +): Config.GlobalConfig { const newConfig: Config.GlobalConfig = {...globalConfig}; if (options.mode === 'watch') { @@ -112,4 +112,4 @@ export default ( } return Object.freeze(newConfig); -}; +} diff --git a/packages/jest-core/src/runGlobalHook.ts b/packages/jest-core/src/runGlobalHook.ts index 2c635a2ecdb6..60de3085592e 100644 --- a/packages/jest-core/src/runGlobalHook.ts +++ b/packages/jest-core/src/runGlobalHook.ts @@ -11,7 +11,7 @@ import {createScriptTransformer} from '@jest/transform'; import type {Config} from '@jest/types'; import prettyFormat from 'pretty-format'; -export default async ({ +export default async function runGlobalHook({ allTests, globalConfig, moduleName, @@ -19,7 +19,7 @@ export default async ({ allTests: Array; globalConfig: Config.GlobalConfig; moduleName: 'globalSetup' | 'globalTeardown'; -}): Promise => { +}): Promise { const globalModulePaths = new Set( allTests.map(test => test.context.config[moduleName]), ); @@ -74,4 +74,4 @@ export default async ({ } } } -}; +} diff --git a/packages/jest-create-cache-key-function/src/index.ts b/packages/jest-create-cache-key-function/src/index.ts index 63f9fbd3add8..fe042dfccb89 100644 --- a/packages/jest-create-cache-key-function/src/index.ts +++ b/packages/jest-create-cache-key-function/src/index.ts @@ -73,7 +73,9 @@ function getCacheKeyFunction(globalCacheKey: string): GetCacheKeyFunction { }; } -export default ( +export default function createCacheKey( files: Array = [], values: Array = [], -): GetCacheKeyFunction => getCacheKeyFunction(getGlobalCacheKey(files, values)); +): GetCacheKeyFunction { + return getCacheKeyFunction(getGlobalCacheKey(files, values)); +} diff --git a/packages/jest-each/src/bind.ts b/packages/jest-each/src/bind.ts index 81e65ce6d14d..6b6a7b8b27a4 100644 --- a/packages/jest-each/src/bind.ts +++ b/packages/jest-each/src/bind.ts @@ -28,11 +28,14 @@ type GlobalCallback = ( timeout?: number, ) => void; -export default ( - cb: GlobalCallback, - supportsDone: boolean = true, +export default function bind( + cb: GlobalCallback, + supportsDone: boolean = true, +) { + return ( + table: Global.EachTable, + ...taggedTemplateData: Global.TemplateData ) => - (table: Global.EachTable, ...taggedTemplateData: Global.TemplateData) => function eachBind( title: string, test: Global.EachTestFn, @@ -57,6 +60,7 @@ export default ( }); } }; +} const isArrayTable = (data: Global.TemplateData) => data.length === 0; diff --git a/packages/jest-each/src/table/array.ts b/packages/jest-each/src/table/array.ts index deef22c46e38..a4fb9d5d0c63 100644 --- a/packages/jest-each/src/table/array.ts +++ b/packages/jest-each/src/table/array.ts @@ -20,7 +20,10 @@ const PLACEHOLDER_PREFIX = '%'; const ESCAPED_PLACEHOLDER_PREFIX = /%%/g; const JEST_EACH_PLACEHOLDER_ESCAPE = '@@__JEST_EACH_PLACEHOLDER_ESCAPE__@@'; -export default (title: string, arrayTable: Global.ArrayTable): EachTests => { +export default function array( + title: string, + arrayTable: Global.ArrayTable, +): EachTests { if (isTemplates(title, arrayTable)) { return arrayTable.map((template, index) => ({ arguments: [template], @@ -34,7 +37,7 @@ export default (title: string, arrayTable: Global.ArrayTable): EachTests => { arguments: row, title: formatTitle(title, row, index), })); -}; +} const isTemplates = ( title: string, diff --git a/packages/jest-each/src/table/template.ts b/packages/jest-each/src/table/template.ts index 491845a12186..a7d90a163a8d 100644 --- a/packages/jest-each/src/table/template.ts +++ b/packages/jest-each/src/table/template.ts @@ -11,18 +11,18 @@ import type {EachTests} from '../bind'; import type {Headings, Template, Templates} from './interpolation'; import {interpolateVariables} from './interpolation'; -export default ( +export default function template( title: string, headings: Headings, row: Global.Row, -): EachTests => { +): EachTests { const table = convertRowToTable(row, headings); const templates = convertTableToTemplates(table, headings); return templates.map((template, index) => ({ arguments: [template], title: interpolateVariables(title, template, index), })); -}; +} const convertRowToTable = (row: Global.Row, headings: Headings): Global.Table => Array.from({length: row.length / headings.length}).map((_, index) => diff --git a/packages/jest-jasmine2/src/each.ts b/packages/jest-jasmine2/src/each.ts index 1b022929cf9a..b53c7efeb962 100644 --- a/packages/jest-jasmine2/src/each.ts +++ b/packages/jest-jasmine2/src/each.ts @@ -8,7 +8,7 @@ import type {JestEnvironment} from '@jest/environment'; import {bind as bindEach} from 'jest-each'; -export default (environment: JestEnvironment): void => { +export default function each(environment: JestEnvironment): void { environment.global.it.each = bindEach(environment.global.it); environment.global.fit.each = bindEach(environment.global.fit); environment.global.xit.each = bindEach(environment.global.xit); @@ -36,4 +36,4 @@ export default (environment: JestEnvironment): void => { environment.global.it.concurrent.skip, false, ); -}; +} diff --git a/packages/jest-jasmine2/src/jasmine/Env.ts b/packages/jest-jasmine2/src/jasmine/Env.ts index 20975e1e9550..a68491ca40c3 100644 --- a/packages/jest-jasmine2/src/jasmine/Env.ts +++ b/packages/jest-jasmine2/src/jasmine/Env.ts @@ -49,7 +49,7 @@ import type { import type {default as Spec, SpecResult} from './Spec'; import type Suite from './Suite'; -export default function (j$: Jasmine) { +export default function jasmineEnv(j$: Jasmine) { return class Env { specFilter: (spec: Spec) => boolean; catchExceptions: (value: unknown) => boolean; diff --git a/packages/jest-jasmine2/src/jestExpect.ts b/packages/jest-jasmine2/src/jestExpect.ts index 6b586a2fbeb2..30e32567f5f7 100644 --- a/packages/jest-jasmine2/src/jestExpect.ts +++ b/packages/jest-jasmine2/src/jestExpect.ts @@ -20,7 +20,7 @@ import type {Jasmine, JasmineMatchersObject, RawMatcherFn} from './types'; declare const global: Global.Global; -export default (config: {expand: boolean}): void => { +export default function jestExpect(config: {expand: boolean}): void { global.expect = expect; expect.setState({expand: config.expand}); expect.extend({ @@ -66,4 +66,4 @@ export default (config: {expand: boolean}): void => { expect.extend(jestMatchersObject); }; -}; +} diff --git a/packages/jest-jasmine2/src/setup_jest_globals.ts b/packages/jest-jasmine2/src/setup_jest_globals.ts index 7fc6329aa317..fbf00602d4fd 100644 --- a/packages/jest-jasmine2/src/setup_jest_globals.ts +++ b/packages/jest-jasmine2/src/setup_jest_globals.ts @@ -89,12 +89,12 @@ const patchJasmine = () => { })((global.jasmine as Jasmine).Spec); }; -export default async ({ +export default async function setupJestGlobals({ config, globalConfig, localRequire, testPath, -}: SetupOptions): Promise => { +}: SetupOptions): Promise { // Jest tests snapshotSerializers in order preceding built-in serializers. // Therefore, add in reverse because the last added is the first tested. config.snapshotSerializers @@ -119,4 +119,4 @@ export default async ({ setState({snapshotState, testPath}); // Return it back to the outer scope (test runner outside the VM). return snapshotState; -}; +} diff --git a/packages/jest-leak-detector/src/index.ts b/packages/jest-leak-detector/src/index.ts index ba6f4eb358e8..2f7bb318bb3d 100644 --- a/packages/jest-leak-detector/src/index.ts +++ b/packages/jest-leak-detector/src/index.ts @@ -15,7 +15,7 @@ import {format as prettyFormat} from 'pretty-format'; const tick = promisify(setImmediate); -export default class { +export default class LeakDetector { private _isReferenceBeingHeld: boolean; constructor(value: unknown) { diff --git a/packages/jest-reporters/src/generateEmptyCoverage.ts b/packages/jest-reporters/src/generateEmptyCoverage.ts index 3d7f13a08db8..af9effddbc93 100644 --- a/packages/jest-reporters/src/generateEmptyCoverage.ts +++ b/packages/jest-reporters/src/generateEmptyCoverage.ts @@ -24,7 +24,7 @@ export type CoverageWorkerResult = result: SingleV8Coverage; }; -export default async function ( +export default async function generateEmptyCoverage( source: string, filename: Config.Path, globalConfig: Config.GlobalConfig, diff --git a/packages/jest-reporters/src/getResultHeader.ts b/packages/jest-reporters/src/getResultHeader.ts index 68f6dc82bfd5..ce7e1f82cb6f 100644 --- a/packages/jest-reporters/src/getResultHeader.ts +++ b/packages/jest-reporters/src/getResultHeader.ts @@ -26,11 +26,11 @@ const PASS = chalk.supportsColor ? chalk.reset.inverse.bold.green(` ${PASS_TEXT} `) : PASS_TEXT; -export default ( +export default function getResultHeader( result: TestResult, globalConfig: Config.GlobalConfig, projectConfig?: Config.ProjectConfig, -): string => { +): string { const testPath = result.testFilePath; const formattedTestPath = formatTestPath( projectConfig ? projectConfig : globalConfig, @@ -64,4 +64,4 @@ export default ( `${status} ${projectDisplayName}${fileLink}` + (testDetail.length ? ` (${testDetail.join(', ')})` : '') ); -}; +} diff --git a/packages/jest-reporters/src/getSnapshotStatus.ts b/packages/jest-reporters/src/getSnapshotStatus.ts index 3c19f9ca3406..53bc5be069fb 100644 --- a/packages/jest-reporters/src/getSnapshotStatus.ts +++ b/packages/jest-reporters/src/getSnapshotStatus.ts @@ -16,10 +16,10 @@ const SNAPSHOT_ADDED = chalk.bold.green; const SNAPSHOT_UPDATED = chalk.bold.green; const SNAPSHOT_OUTDATED = chalk.bold.yellow; -export default ( +export default function getSnapshotStatus( snapshot: TestResult['snapshot'], afterUpdate: boolean, -): Array => { +): Array { const statuses = []; if (snapshot.added) { @@ -71,4 +71,4 @@ export default ( } return statuses; -}; +} diff --git a/packages/jest-reporters/src/getSnapshotSummary.ts b/packages/jest-reporters/src/getSnapshotSummary.ts index 66bf7cd4227f..eb54902c7209 100644 --- a/packages/jest-reporters/src/getSnapshotSummary.ts +++ b/packages/jest-reporters/src/getSnapshotSummary.ts @@ -22,11 +22,11 @@ const SNAPSHOT_REMOVED = chalk.bold.green; const SNAPSHOT_SUMMARY = chalk.bold; const SNAPSHOT_UPDATED = chalk.bold.green; -export default ( +export default function getSnapshotSummary( snapshots: SnapshotSummary, globalConfig: Config.GlobalConfig, updateCommand: string, -): Array => { +): Array { const summary = []; summary.push(SNAPSHOT_SUMMARY('Snapshot Summary')); if (snapshots.added) { @@ -136,4 +136,4 @@ export default ( } return summary; -}; +} diff --git a/packages/jest-source-map/src/getCallsite.ts b/packages/jest-source-map/src/getCallsite.ts index 5a0f74a5bdfb..44d4420fb201 100644 --- a/packages/jest-source-map/src/getCallsite.ts +++ b/packages/jest-source-map/src/getCallsite.ts @@ -46,10 +46,10 @@ const addSourceMapConsumer = ( }); }; -export default ( +export default function getCallsite( level: number, sourceMaps?: SourceMapRegistry | null, -): callsites.CallSite => { +): callsites.CallSite { const levelAfterThisCall = level + 1; const stack = callsites()[levelAfterThisCall]; const sourceMapFileName = sourceMaps?.get(stack.getFileName() || ''); @@ -65,4 +65,4 @@ export default ( } return stack; -}; +} diff --git a/packages/jest-util/src/createProcessObject.ts b/packages/jest-util/src/createProcessObject.ts index 5623a6ba082d..32661b47d44a 100644 --- a/packages/jest-util/src/createProcessObject.ts +++ b/packages/jest-util/src/createProcessObject.ts @@ -79,7 +79,7 @@ function createProcessEnv(): NodeJS.ProcessEnv { return Object.assign(proxy, process.env); } -export default function (): NodeJS.Process { +export default function createProcessObject(): NodeJS.Process { const process = require('process'); const newProcess = deepCyclicCopy(process, { blacklist: BLACKLIST, diff --git a/packages/jest-util/src/installCommonGlobals.ts b/packages/jest-util/src/installCommonGlobals.ts index 5931f672e3e8..617974443fcf 100644 --- a/packages/jest-util/src/installCommonGlobals.ts +++ b/packages/jest-util/src/installCommonGlobals.ts @@ -12,7 +12,7 @@ import deepCyclicCopy from './deepCyclicCopy'; const DTRACE = Object.keys(global).filter(key => key.startsWith('DTRACE')); -export default function ( +export default function installCommonGlobals( globalObject: typeof globalThis, globals: Config.ConfigGlobals, ): typeof globalThis & Config.ConfigGlobals { diff --git a/packages/jest-util/src/setGlobal.ts b/packages/jest-util/src/setGlobal.ts index bbce818ddac0..18f6cf4c1067 100644 --- a/packages/jest-util/src/setGlobal.ts +++ b/packages/jest-util/src/setGlobal.ts @@ -5,11 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -export default ( +export default function setGlobal( globalToMutate: typeof globalThis, key: string, value: unknown, -): void => { +): void { // @ts-expect-error: no index globalToMutate[key] = value; -}; +} diff --git a/packages/jest-util/src/testPathPatternToRegExp.ts b/packages/jest-util/src/testPathPatternToRegExp.ts index ccdc16d0d771..8b3d67f79b14 100644 --- a/packages/jest-util/src/testPathPatternToRegExp.ts +++ b/packages/jest-util/src/testPathPatternToRegExp.ts @@ -10,6 +10,8 @@ import type {Config} from '@jest/types'; // Because we serialize/deserialize globalConfig when we spawn workers, // we can't pass regular expression. Using this shared function on both sides // will ensure that we produce consistent regexp for testPathPattern. -export default ( +export default function testPathPatternToRegExp( testPathPattern: Config.GlobalConfig['testPathPattern'], -): RegExp => new RegExp(testPathPattern, 'i'); +): RegExp { + return new RegExp(testPathPattern, 'i'); +} diff --git a/packages/jest-watcher/src/lib/colorize.ts b/packages/jest-watcher/src/lib/colorize.ts index 056078699bb9..680cd75c8934 100644 --- a/packages/jest-watcher/src/lib/colorize.ts +++ b/packages/jest-watcher/src/lib/colorize.ts @@ -7,7 +7,14 @@ import chalk = require('chalk'); -export default (str: string, start: number, end: number): string => - chalk.dim(str.slice(0, start)) + - chalk.reset(str.slice(start, end)) + - chalk.dim(str.slice(end)); +export default function colorize( + str: string, + start: number, + end: number, +): string { + return ( + chalk.dim(str.slice(0, start)) + + chalk.reset(str.slice(start, end)) + + chalk.dim(str.slice(end)) + ); +} diff --git a/packages/jest-watcher/src/lib/formatTestNameByPattern.ts b/packages/jest-watcher/src/lib/formatTestNameByPattern.ts index 2690e9dd25a4..7ee3334bc548 100644 --- a/packages/jest-watcher/src/lib/formatTestNameByPattern.ts +++ b/packages/jest-watcher/src/lib/formatTestNameByPattern.ts @@ -11,7 +11,11 @@ import colorize from './colorize'; const DOTS = '...'; const ENTER = '⏎'; -export default (testName: string, pattern: string, width: number): string => { +export default function formatTestNameByPattern( + testName: string, + pattern: string, + width: number, +): string { const inlineTestName = testName.replace(/(\r\n|\n|\r)/gm, ENTER); let regexp; @@ -54,4 +58,4 @@ export default (testName: string, pattern: string, width: number): string => { } return `${chalk.dim(slicedTestName)}${chalk.reset(DOTS)}`; -}; +} From a5eb9beb6878705677b4cd77aa133d3a5ff16df8 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Mon, 7 Feb 2022 00:34:51 +0100 Subject: [PATCH 2/2] changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eb466ffdd77..e1f8cec7abe9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ ### Chore & Maintenance +- `[*]` Avoid anonymous default exports ([#12313](https://github.com/facebook/jest/pull/12313)) + ### Performance ## 27.5.0