From 4e432b7c1c0699c27dafe2282d9178056c888e96 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Wed, 26 Jul 2023 23:47:53 +0800 Subject: [PATCH 1/8] chore: upgrade jest-util --- packages/jest-changed-files/package.json | 1 + packages/jest-changed-files/src/index.ts | 5 +---- packages/jest-changed-files/tsconfig.json | 3 ++- packages/jest-circus/src/eventHandler.ts | 2 +- packages/jest-circus/src/run.ts | 2 +- packages/jest-circus/src/utils.ts | 10 +--------- packages/jest-console/src/BufferedConsole.ts | 8 +------- packages/jest-core/src/TestScheduler.ts | 8 +------- packages/jest-core/src/lib/watchPluginsHelpers.ts | 5 +---- packages/jest-haste-map/src/index.ts | 8 +------- packages/jest-runtime/src/index.ts | 12 +----------- packages/jest-transform/src/ScriptTransformer.ts | 7 +------ packages/jest-util/src/index.ts | 2 ++ packages/jest-util/src/invariant.ts | 15 +++++++++++++++ packages/jest-util/src/notEmpty.ts | 10 ++++++++++ yarn.lock | 1 + 16 files changed, 41 insertions(+), 58 deletions(-) create mode 100644 packages/jest-util/src/invariant.ts create mode 100644 packages/jest-util/src/notEmpty.ts diff --git a/packages/jest-changed-files/package.json b/packages/jest-changed-files/package.json index 866a23a3992e..755db641b01a 100644 --- a/packages/jest-changed-files/package.json +++ b/packages/jest-changed-files/package.json @@ -18,6 +18,7 @@ }, "dependencies": { "execa": "^5.0.0", + "jest-util": "workspace:^", "p-limit": "^3.1.0" }, "engines": { diff --git a/packages/jest-changed-files/src/index.ts b/packages/jest-changed-files/src/index.ts index df3cb0723c34..c90ee0a1e837 100644 --- a/packages/jest-changed-files/src/index.ts +++ b/packages/jest-changed-files/src/index.ts @@ -7,6 +7,7 @@ */ import pLimit = require('p-limit'); +import {notEmpty} from 'jest-util'; import git from './git'; import hg from './hg'; import sl from './sl'; @@ -16,10 +17,6 @@ type RootPromise = ReturnType; export type {ChangedFiles, ChangedFilesPromise} from './types'; -function notEmpty(value: T | null | undefined): value is T { - return value != null; -} - // This is an arbitrary number. The main goal is to prevent projects with // many roots (50+) from spawning too many processes at once. const mutex = pLimit(5); diff --git a/packages/jest-changed-files/tsconfig.json b/packages/jest-changed-files/tsconfig.json index 12688a2879d6..aad1b761e910 100644 --- a/packages/jest-changed-files/tsconfig.json +++ b/packages/jest-changed-files/tsconfig.json @@ -4,5 +4,6 @@ "rootDir": "src", "outDir": "build" }, - "include": ["./src/**/*"] + "include": ["./src/**/*"], + "references": [{"path": "../jest-util"}] } diff --git a/packages/jest-circus/src/eventHandler.ts b/packages/jest-circus/src/eventHandler.ts index 0a437df1ed9a..4c401cada760 100644 --- a/packages/jest-circus/src/eventHandler.ts +++ b/packages/jest-circus/src/eventHandler.ts @@ -6,6 +6,7 @@ */ import type {Circus} from '@jest/types'; +import {invariant} from 'jest-util'; import { injectGlobalErrorHandlers, restoreGlobalErrorHandlers, @@ -15,7 +16,6 @@ import { addErrorToEachTestUnderDescribe, describeBlockHasTests, getTestDuration, - invariant, makeDescribe, makeTest, } from './utils'; diff --git a/packages/jest-circus/src/run.ts b/packages/jest-circus/src/run.ts index 6564b92e8c45..cd4973b59216 100644 --- a/packages/jest-circus/src/run.ts +++ b/packages/jest-circus/src/run.ts @@ -9,6 +9,7 @@ import {AsyncLocalStorage} from 'async_hooks'; import pLimit = require('p-limit'); import {jestExpect} from '@jest/expect'; import type {Circus} from '@jest/types'; +import {invariant} from 'jest-util'; import shuffleArray, {RandomNumberGenerator, rngBuilder} from './shuffleArray'; import {dispatch, getState} from './state'; import {RETRY_TIMES} from './types'; @@ -17,7 +18,6 @@ import { getAllHooksForDescribe, getEachHooksForTest, getTestID, - invariant, makeRunResult, } from './utils'; diff --git a/packages/jest-circus/src/utils.ts b/packages/jest-circus/src/utils.ts index 9e1c8d74838b..fa45f4fbb744 100644 --- a/packages/jest-circus/src/utils.ts +++ b/packages/jest-circus/src/utils.ts @@ -17,6 +17,7 @@ import { ErrorWithStack, convertDescriptorToString, formatTime, + invariant, isPromise, } from 'jest-util'; import {format as prettyFormat} from 'pretty-format'; @@ -456,15 +457,6 @@ export const addErrorToEachTestUnderDescribe = ( } }; -export function invariant( - condition: unknown, - message?: string, -): asserts condition { - if (!condition) { - throw new Error(message); - } -} - type TestDescription = { ancestorTitles: Array; fullName: string; diff --git a/packages/jest-console/src/BufferedConsole.ts b/packages/jest-console/src/BufferedConsole.ts index b91117bf36e6..88440bb73c83 100644 --- a/packages/jest-console/src/BufferedConsole.ts +++ b/packages/jest-console/src/BufferedConsole.ts @@ -9,7 +9,7 @@ import {AssertionError, strict as assert} from 'assert'; import {Console} from 'console'; import {InspectOptions, format, formatWithOptions, inspect} from 'util'; import chalk = require('chalk'); -import {ErrorWithStack, formatTime} from 'jest-util'; +import {ErrorWithStack, formatTime, invariant} from 'jest-util'; import type { ConsoleBuffer, LogCounters, @@ -180,9 +180,3 @@ export default class BufferedConsole extends Console { return this._buffer.length ? this._buffer : undefined; } } - -function invariant(condition: boolean, message?: string): asserts condition { - if (!condition) { - throw new Error(message); - } -} diff --git a/packages/jest-core/src/TestScheduler.ts b/packages/jest-core/src/TestScheduler.ts index 2c53db5a41fc..77caa9a95c3f 100644 --- a/packages/jest-core/src/TestScheduler.ts +++ b/packages/jest-core/src/TestScheduler.ts @@ -38,7 +38,7 @@ import { buildSnapshotResolver, cleanup as cleanupSnapshots, } from 'jest-snapshot'; -import {ErrorWithStack, requireOrImportModule} from 'jest-util'; +import {ErrorWithStack, invariant, requireOrImportModule} from 'jest-util'; import type {TestWatcher} from 'jest-watcher'; import ReporterDispatcher from './ReporterDispatcher'; import {shouldRunInBand} from './testSchedulerHelper'; @@ -425,12 +425,6 @@ class TestScheduler { } } -function invariant(condition: unknown, message?: string): asserts condition { - if (!condition) { - throw new Error(message); - } -} - const createAggregatedResults = (numTotalTestSuites: number) => { const result = makeEmptyAggregatedTestResult(); result.numTotalTestSuites = numTotalTestSuites; diff --git a/packages/jest-core/src/lib/watchPluginsHelpers.ts b/packages/jest-core/src/lib/watchPluginsHelpers.ts index 08cf4cae06ad..08020b10843c 100644 --- a/packages/jest-core/src/lib/watchPluginsHelpers.ts +++ b/packages/jest-core/src/lib/watchPluginsHelpers.ts @@ -6,6 +6,7 @@ */ import type {Config} from '@jest/types'; +import {notEmpty} from 'jest-util'; import type {UsageData, WatchPlugin} from 'jest-watcher'; export const filterInteractivePlugins = ( @@ -27,10 +28,6 @@ export const filterInteractivePlugins = ( }); }; -function notEmpty(value: T | null | undefined): value is T { - return value != null; -} - export const getSortedUsageRows = ( watchPlugins: Array, globalConfig: Config.GlobalConfig, diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index bc65149445bf..57536026fe86 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -13,7 +13,7 @@ import {deserialize, serialize} from 'v8'; import {Stats, readFileSync, writeFileSync} from 'graceful-fs'; import type {Config} from '@jest/types'; import {escapePathForRegex} from 'jest-regex-util'; -import {requireOrImportModule} from 'jest-util'; +import {invariant, requireOrImportModule} from 'jest-util'; import {JestWorkerFarm, Worker} from 'jest-worker'; import HasteFS from './HasteFS'; import HasteModuleMap from './ModuleMap'; @@ -131,12 +131,6 @@ const VCS_DIRECTORIES = ['.git', '.hg', '.sl'] .map(vcs => escapePathForRegex(path.sep + vcs + path.sep)) .join('|'); -function invariant(condition: unknown, message?: string): asserts condition { - if (!condition) { - throw new Error(message); - } -} - /** * HasteMap is a JavaScript implementation of Facebook's haste module system. * diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index b34ecdfe3890..f9c5e05187ed 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -53,7 +53,7 @@ import type {MockMetadata, ModuleMocker} from 'jest-mock'; import {escapePathForRegex} from 'jest-regex-util'; import Resolver, {ResolveModuleConfig} from 'jest-resolve'; import {EXTENSION as SnapshotExtension} from 'jest-snapshot'; -import {createDirectory, deepCyclicCopy} from 'jest-util'; +import {createDirectory, deepCyclicCopy, invariant, notEmpty} from 'jest-util'; import { createOutsideJestVmPath, decodePossibleOutsideJestVmPath, @@ -2539,16 +2539,6 @@ export default class Runtime { } } -function invariant(condition: unknown, message?: string): asserts condition { - if (!condition) { - throw new Error(message); - } -} - -function notEmpty(value: T | null | undefined): value is T { - return value !== null && value !== undefined; -} - async function evaluateSyntheticModule(module: SyntheticModule) { await module.link(() => { throw new Error('This should never happen'); diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index a73015488b07..06ed08f42ea8 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -20,6 +20,7 @@ import type {Config} from '@jest/types'; import HasteMap from 'jest-haste-map'; import { createDirectory, + invariant, isPromise, requireOrImportModule, tryRealpath, @@ -1034,12 +1035,6 @@ const calcTransformRegExp = (config: Config.ProjectConfig) => { return transformRegexp; }; -function invariant(condition: unknown, message?: string): asserts condition { - if (condition == null || condition === false || condition === '') { - throw new Error(message); - } -} - function assertSyncTransformer( transformer: Transformer, name: string | undefined, diff --git a/packages/jest-util/src/index.ts b/packages/jest-util/src/index.ts index 1e73c92e0d18..ce21e6a4ad5c 100644 --- a/packages/jest-util/src/index.ts +++ b/packages/jest-util/src/index.ts @@ -28,3 +28,5 @@ export {default as pluralize} from './pluralize'; export {default as formatTime} from './formatTime'; export {default as tryRealpath} from './tryRealpath'; export {default as requireOrImportModule} from './requireOrImportModule'; +export {default as invariant} from './invariant'; +export {default as notEmpty} from './notEmpty'; diff --git a/packages/jest-util/src/invariant.ts b/packages/jest-util/src/invariant.ts new file mode 100644 index 000000000000..24e0cebedbf3 --- /dev/null +++ b/packages/jest-util/src/invariant.ts @@ -0,0 +1,15 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default function invariant( + condition: unknown, + message?: string, +): asserts condition { + if (!condition) { + throw new Error(message); + } +} diff --git a/packages/jest-util/src/notEmpty.ts b/packages/jest-util/src/notEmpty.ts new file mode 100644 index 000000000000..3862e11b35e3 --- /dev/null +++ b/packages/jest-util/src/notEmpty.ts @@ -0,0 +1,10 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +export default function notEmpty(value: T | null | undefined): value is T { + return value !== null && value !== undefined; +} diff --git a/yarn.lock b/yarn.lock index 1a618ceaeeef..ad070edc6c4a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -12328,6 +12328,7 @@ __metadata: resolution: "jest-changed-files@workspace:packages/jest-changed-files" dependencies: execa: ^5.0.0 + jest-util: "workspace:^" p-limit: ^3.1.0 languageName: unknown linkType: soft From ce84d9a6e54aa2fe1f0bbde0578489519eec7739 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Wed, 26 Jul 2023 23:52:32 +0800 Subject: [PATCH 2/8] feat: upgrade CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf47e4302d60..065a77a20006 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Chore & Maintenance +- `[jest-changed-files, jest-circus, jest-console, @jest/core, @jest/runtime, @jest/transform]` Use `invariant` and `notEmpty` from `jest-util` rather than own internal ([#14366](https://github.com/jestjs/jest/pull/14366)) - `[@jest/core]` Use `pluralize` from `jest-util` rather than own internal ([#14322](https://github.com/jestjs/jest/pull/14322)) ### Performance From 509805688522872e13b24183e539b4788bbf3c02 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Thu, 27 Jul 2023 22:25:09 +0800 Subject: [PATCH 3/8] fix: ci problem --- .../__snapshots__/circusDeclarationErrors.test.ts.snap | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap b/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap index 59acd31aae2f..0e49b62527c6 100644 --- a/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap +++ b/e2e/__tests__/__snapshots__/circusDeclarationErrors.test.ts.snap @@ -16,7 +16,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 14 | }); 15 | }); - at eventHandler (../../packages/jest-circus/build/eventHandler.js:140:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:141:11) at test (__tests__/asyncDefinition.test.js:12:5) ● Test suite failed to run @@ -31,7 +31,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 15 | }); 16 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:103:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:104:11) at afterAll (__tests__/asyncDefinition.test.js:13:5) ● Test suite failed to run @@ -46,7 +46,7 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 20 | }); 21 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:140:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:141:11) at test (__tests__/asyncDefinition.test.js:18:3) ● Test suite failed to run @@ -60,6 +60,6 @@ exports[`defining tests and hooks asynchronously throws 1`] = ` 20 | }); 21 | - at eventHandler (../../packages/jest-circus/build/eventHandler.js:103:11) + at eventHandler (../../packages/jest-circus/build/eventHandler.js:104:11) at afterAll (__tests__/asyncDefinition.test.js:19:3)" `; From 0327adc24fb3add1f3621753da69a41f90204720 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Mon, 31 Jul 2023 17:32:35 +0800 Subject: [PATCH 4/8] fix: cr --- packages/jest-changed-files/src/index.ts | 8 ++++---- .../jest-core/src/lib/watchPluginsHelpers.ts | 4 ++-- packages/jest-environment-node/src/index.ts | 8 ++------ packages/jest-resolve/src/defaultResolver.ts | 19 ++++--------------- packages/jest-runtime/src/index.ts | 18 ++++++++++-------- packages/jest-util/src/index.ts | 2 +- packages/jest-util/src/invariant.ts | 8 ++++++-- .../src/{notEmpty.ts => isNonNullable.ts} | 4 ++-- 8 files changed, 31 insertions(+), 40 deletions(-) rename packages/jest-util/src/{notEmpty.ts => isNonNullable.ts} (60%) diff --git a/packages/jest-changed-files/src/index.ts b/packages/jest-changed-files/src/index.ts index c90ee0a1e837..3577d49e05ee 100644 --- a/packages/jest-changed-files/src/index.ts +++ b/packages/jest-changed-files/src/index.ts @@ -7,7 +7,7 @@ */ import pLimit = require('p-limit'); -import {notEmpty} from 'jest-util'; +import {isNonNullable} from 'jest-util'; import git from './git'; import hg from './hg'; import sl from './sl'; @@ -75,8 +75,8 @@ export const findRepos = async (roots: Array): Promise => { const slRepos = await Promise.all(roots.map(findSlRoot)); return { - git: new Set(gitRepos.filter(notEmpty)), - hg: new Set(hgRepos.filter(notEmpty)), - sl: new Set(slRepos.filter(notEmpty)), + git: new Set(gitRepos.filter(isNonNullable)), + hg: new Set(hgRepos.filter(isNonNullable)), + sl: new Set(slRepos.filter(isNonNullable)), }; }; diff --git a/packages/jest-core/src/lib/watchPluginsHelpers.ts b/packages/jest-core/src/lib/watchPluginsHelpers.ts index 08020b10843c..0479f544beb4 100644 --- a/packages/jest-core/src/lib/watchPluginsHelpers.ts +++ b/packages/jest-core/src/lib/watchPluginsHelpers.ts @@ -6,7 +6,7 @@ */ import type {Config} from '@jest/types'; -import {notEmpty} from 'jest-util'; +import {isNonNullable} from 'jest-util'; import type {UsageData, WatchPlugin} from 'jest-watcher'; export const filterInteractivePlugins = ( @@ -53,4 +53,4 @@ export const getSortedUsageRows = ( return 0; }) .map(p => p.getUsageInfo && p.getUsageInfo(globalConfig)) - .filter(notEmpty); + .filter(isNonNullable); diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 1b387aaffee5..88bdd71d5875 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -14,7 +14,7 @@ import type { import {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers'; import type {Global} from '@jest/types'; import {ModuleMocker} from 'jest-mock'; -import {installCommonGlobals} from 'jest-util'; +import {installCommonGlobals, invariant} from 'jest-util'; type Timer = { id: number; @@ -43,11 +43,7 @@ const nodeGlobals = new Map( nodeGlobalsKey, ); - if (!descriptor) { - throw new Error( - `No property descriptor for ${nodeGlobalsKey}, this is a bug in Jest.`, - ); - } + invariant(descriptor, `No property descriptor for ${nodeGlobalsKey}.`); return [nodeGlobalsKey, descriptor]; }), diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index c15e608ca5d5..f5c7219b4d22 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -9,6 +9,7 @@ import {dirname, isAbsolute, resolve as pathResolve} from 'path'; import pnpResolver from 'jest-pnp-resolver'; import {SyncOpts as UpstreamResolveOptions, sync as resolveSync} from 'resolve'; import * as resolve from 'resolve.exports'; +import {invariant} from 'jest-util'; import { findClosestPackageJson, isDirectory, @@ -159,11 +160,7 @@ function getPathInModule( defaultResolver(target, options); } - if (pkg.imports) { - throw new Error( - '`imports` exists, but no results - this is a bug in Jest. Please report an issue', - ); - } + invariant(!pkg.imports, '`imports` exists, but no results.'); } const segments = path.split('/'); @@ -191,11 +188,7 @@ function getPathInModule( return pathResolve(dirname(closestPackageJson), resolved[0]); } - if (pkg.exports) { - throw new Error( - '`exports` exists, but no results - this is a bug in Jest. Please report an issue', - ); - } + invariant(!pkg.exports, '`exports` exists, but no results.'); } } @@ -220,11 +213,7 @@ function getPathInModule( return pathResolve(dirname(packageJsonPath), resolved[0]); } - if (pkg.exports) { - throw new Error( - '`exports` exists, but no results - this is a bug in Jest. Please report an issue', - ); - } + invariant(!pkg.exports, '`exports` exists, but no results.'); } } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index f9c5e05187ed..007444b31e23 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -53,7 +53,12 @@ import type {MockMetadata, ModuleMocker} from 'jest-mock'; import {escapePathForRegex} from 'jest-regex-util'; import Resolver, {ResolveModuleConfig} from 'jest-resolve'; import {EXTENSION as SnapshotExtension} from 'jest-snapshot'; -import {createDirectory, deepCyclicCopy, invariant, notEmpty} from 'jest-util'; +import { + createDirectory, + deepCyclicCopy, + invariant, + isNonNullable, +} from 'jest-util'; import { createOutsideJestVmPath, decodePossibleOutsideJestVmPath, @@ -527,7 +532,7 @@ export default class Runtime { invariant( !this._esmoduleRegistry.has(cacheKey), - `Module cache already has entry ${cacheKey}. This is a bug in Jest, please report it!`, + `Module cache already has entry ${cacheKey}.`, ); this._esmoduleRegistry.set(cacheKey, module); @@ -541,10 +546,7 @@ export default class Runtime { const module = this._esmoduleRegistry.get(cacheKey); - invariant( - module, - 'Module cache does not contain module. This is a bug in Jest, please open up an issue', - ); + invariant(module, 'Module cache does not contain module.'); return module; } @@ -1580,7 +1582,7 @@ export default class Runtime { module.path, // __dirname module.filename, // __filename lastArgs[0], - ...lastArgs.slice(1).filter(notEmpty), + ...lastArgs.slice(1).filter(isNonNullable), ); } catch (error: any) { this.handleExecutionError(error, module); @@ -2427,7 +2429,7 @@ export default class Runtime { '__filename', this._config.injectGlobals ? 'jest' : undefined, ...this._config.sandboxInjectedGlobals, - ].filter(notEmpty); + ].filter(isNonNullable); } private handleExecutionError(e: Error, module: Module): never { diff --git a/packages/jest-util/src/index.ts b/packages/jest-util/src/index.ts index ce21e6a4ad5c..531acfe4aa3a 100644 --- a/packages/jest-util/src/index.ts +++ b/packages/jest-util/src/index.ts @@ -29,4 +29,4 @@ export {default as formatTime} from './formatTime'; export {default as tryRealpath} from './tryRealpath'; export {default as requireOrImportModule} from './requireOrImportModule'; export {default as invariant} from './invariant'; -export {default as notEmpty} from './notEmpty'; +export {default as isNonNullable} from './isNonNullable'; diff --git a/packages/jest-util/src/invariant.ts b/packages/jest-util/src/invariant.ts index 24e0cebedbf3..5bf57b673988 100644 --- a/packages/jest-util/src/invariant.ts +++ b/packages/jest-util/src/invariant.ts @@ -7,9 +7,13 @@ export default function invariant( condition: unknown, - message?: string, + message = '', ): asserts condition { if (!condition) { - throw new Error(message); + throw new Error( + `${ + message ? `${message} ` : '' + }This is a bug in Jest, please report an issue!`, + ); } } diff --git a/packages/jest-util/src/notEmpty.ts b/packages/jest-util/src/isNonNullable.ts similarity index 60% rename from packages/jest-util/src/notEmpty.ts rename to packages/jest-util/src/isNonNullable.ts index 3862e11b35e3..34eddf095ef5 100644 --- a/packages/jest-util/src/notEmpty.ts +++ b/packages/jest-util/src/isNonNullable.ts @@ -5,6 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -export default function notEmpty(value: T | null | undefined): value is T { - return value !== null && value !== undefined; +export default function isNonNullable(value: T): value is NonNullable { + return value != null; } From 593d4a4d1d783dcabe6152bca40f2061d0e206af Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Tue, 1 Aug 2023 00:00:04 +0800 Subject: [PATCH 5/8] fix: ci snapshot --- packages/jest-environment-node/src/index.ts | 5 ++++- packages/jest-resolve/src/defaultResolver.ts | 15 ++++++++++++--- packages/jest-util/src/invariant.ts | 6 +----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index 88bdd71d5875..bad40e133087 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -43,7 +43,10 @@ const nodeGlobals = new Map( nodeGlobalsKey, ); - invariant(descriptor, `No property descriptor for ${nodeGlobalsKey}.`); + invariant( + descriptor, + `No property descriptor for ${nodeGlobalsKey}, this is a bug in Jest.`, + ); return [nodeGlobalsKey, descriptor]; }), diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index f5c7219b4d22..aeb78717485b 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -160,7 +160,10 @@ function getPathInModule( defaultResolver(target, options); } - invariant(!pkg.imports, '`imports` exists, but no results.'); + invariant( + !pkg.imports, + '`imports` exists, but no results - this is a bug in Jest. Please report an issue', + ); } const segments = path.split('/'); @@ -188,7 +191,10 @@ function getPathInModule( return pathResolve(dirname(closestPackageJson), resolved[0]); } - invariant(!pkg.exports, '`exports` exists, but no results.'); + invariant( + !pkg.exports, + '`exports` exists, but no results - this is a bug in Jest. Please report an issue', + ); } } @@ -213,7 +219,10 @@ function getPathInModule( return pathResolve(dirname(packageJsonPath), resolved[0]); } - invariant(!pkg.exports, '`exports` exists, but no results.'); + invariant( + !pkg.exports, + '`exports` exists, but no results - this is a bug in Jest. Please report an issue', + ); } } diff --git a/packages/jest-util/src/invariant.ts b/packages/jest-util/src/invariant.ts index 5bf57b673988..2e86440ae791 100644 --- a/packages/jest-util/src/invariant.ts +++ b/packages/jest-util/src/invariant.ts @@ -10,10 +10,6 @@ export default function invariant( message = '', ): asserts condition { if (!condition) { - throw new Error( - `${ - message ? `${message} ` : '' - }This is a bug in Jest, please report an issue!`, - ); + throw new Error(message); } } From 000d8ba19cc928b8d1749393ca217970e0481ba7 Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Tue, 1 Aug 2023 11:27:15 +0800 Subject: [PATCH 6/8] fix: ci error --- packages/jest-environment-node/src/index.ts | 11 ++++---- packages/jest-resolve/src/defaultResolver.ts | 28 +++++++++++--------- packages/jest-runtime/src/index.ts | 7 +++-- 3 files changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/jest-environment-node/src/index.ts b/packages/jest-environment-node/src/index.ts index bad40e133087..1b387aaffee5 100644 --- a/packages/jest-environment-node/src/index.ts +++ b/packages/jest-environment-node/src/index.ts @@ -14,7 +14,7 @@ import type { import {LegacyFakeTimers, ModernFakeTimers} from '@jest/fake-timers'; import type {Global} from '@jest/types'; import {ModuleMocker} from 'jest-mock'; -import {installCommonGlobals, invariant} from 'jest-util'; +import {installCommonGlobals} from 'jest-util'; type Timer = { id: number; @@ -43,10 +43,11 @@ const nodeGlobals = new Map( nodeGlobalsKey, ); - invariant( - descriptor, - `No property descriptor for ${nodeGlobalsKey}, this is a bug in Jest.`, - ); + if (!descriptor) { + throw new Error( + `No property descriptor for ${nodeGlobalsKey}, this is a bug in Jest.`, + ); + } return [nodeGlobalsKey, descriptor]; }), diff --git a/packages/jest-resolve/src/defaultResolver.ts b/packages/jest-resolve/src/defaultResolver.ts index aeb78717485b..c15e608ca5d5 100644 --- a/packages/jest-resolve/src/defaultResolver.ts +++ b/packages/jest-resolve/src/defaultResolver.ts @@ -9,7 +9,6 @@ import {dirname, isAbsolute, resolve as pathResolve} from 'path'; import pnpResolver from 'jest-pnp-resolver'; import {SyncOpts as UpstreamResolveOptions, sync as resolveSync} from 'resolve'; import * as resolve from 'resolve.exports'; -import {invariant} from 'jest-util'; import { findClosestPackageJson, isDirectory, @@ -160,10 +159,11 @@ function getPathInModule( defaultResolver(target, options); } - invariant( - !pkg.imports, - '`imports` exists, but no results - this is a bug in Jest. Please report an issue', - ); + if (pkg.imports) { + throw new Error( + '`imports` exists, but no results - this is a bug in Jest. Please report an issue', + ); + } } const segments = path.split('/'); @@ -191,10 +191,11 @@ function getPathInModule( return pathResolve(dirname(closestPackageJson), resolved[0]); } - invariant( - !pkg.exports, - '`exports` exists, but no results - this is a bug in Jest. Please report an issue', - ); + if (pkg.exports) { + throw new Error( + '`exports` exists, but no results - this is a bug in Jest. Please report an issue', + ); + } } } @@ -219,10 +220,11 @@ function getPathInModule( return pathResolve(dirname(packageJsonPath), resolved[0]); } - invariant( - !pkg.exports, - '`exports` exists, but no results - this is a bug in Jest. Please report an issue', - ); + if (pkg.exports) { + throw new Error( + '`exports` exists, but no results - this is a bug in Jest. Please report an issue', + ); + } } } diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 007444b31e23..642de2e4250d 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -532,7 +532,7 @@ export default class Runtime { invariant( !this._esmoduleRegistry.has(cacheKey), - `Module cache already has entry ${cacheKey}.`, + `Module cache already has entry ${cacheKey}. This is a bug in Jest, please report it!`, ); this._esmoduleRegistry.set(cacheKey, module); @@ -546,7 +546,10 @@ export default class Runtime { const module = this._esmoduleRegistry.get(cacheKey); - invariant(module, 'Module cache does not contain module.'); + invariant( + module, + 'Module cache does not contain module. This is a bug in Jest, please open up an issue', + ); return module; } From 5bcc09ca3db3241fefba594d8e9506fdb7288a4b Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Tue, 1 Aug 2023 23:52:14 +0800 Subject: [PATCH 7/8] feat: try to fix ci --- packages/jest-util/src/isNonNullable.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/jest-util/src/isNonNullable.ts b/packages/jest-util/src/isNonNullable.ts index 34eddf095ef5..0720667abba2 100644 --- a/packages/jest-util/src/isNonNullable.ts +++ b/packages/jest-util/src/isNonNullable.ts @@ -5,6 +5,8 @@ * LICENSE file in the root directory of this source tree. */ -export default function isNonNullable(value: T): value is NonNullable { - return value != null; +export default function isNonNullable( + value: T | null | undefined, +): value is T { + return value !== null || value !== undefined; } From f9369e4dde2b0a2ad81608a685986a0bed4ca19c Mon Sep 17 00:00:00 2001 From: eryue0220 Date: Wed, 2 Aug 2023 15:39:44 +0800 Subject: [PATCH 8/8] fix: try to fix ci --- packages/jest-util/src/isNonNullable.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/jest-util/src/isNonNullable.ts b/packages/jest-util/src/isNonNullable.ts index 0720667abba2..34eddf095ef5 100644 --- a/packages/jest-util/src/isNonNullable.ts +++ b/packages/jest-util/src/isNonNullable.ts @@ -5,8 +5,6 @@ * LICENSE file in the root directory of this source tree. */ -export default function isNonNullable( - value: T | null | undefined, -): value is T { - return value !== null || value !== undefined; +export default function isNonNullable(value: T): value is NonNullable { + return value != null; }