Skip to content

Commit

Permalink
feat(testing): allow usage of jest 30 pre-release versions
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed Aug 8, 2024
1 parent 76268b7 commit 12e6272
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
11 changes: 6 additions & 5 deletions packages/jest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@
"migrations": "./migrations.json"
},
"dependencies": {
"@jest/reporters": "^29.4.1",
"@jest/test-result": "^29.4.1",
"@jest/reporters": "^29.4.1 || ^30.0.0-alpha.6",
"@jest/test-result": "^29.4.1 || ^30.0.0-alpha.6",
"@nx/devkit": "file:../devkit",
"@nx/js": "file:../js",
"@phenomnomnominal/tsquery": "~5.0.1",
"chalk": "^4.1.0",
"identity-obj-proxy": "3.0.0",
"jest-config": "^29.4.1",
"jest-resolve": "^29.4.1",
"jest-util": "^29.4.1",
"jest-config": "^29.4.1 || ^30.0.0-alpha.6",
"jest-resolve": "^29.4.1 || ^30.0.0-alpha.6",
"jest-util": "^29.4.1 || ^30.0.0-alpha.6",
"minimatch": "9.0.3",
"resolve.exports": "1.1.0",
"semver": "^7.5.3",
"tslib": "^2.3.0",
"yargs-parser": "21.1.1"
},
Expand Down
14 changes: 2 additions & 12 deletions packages/jest/plugins/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { dirname, extname, join, resolve } from 'path';
import { resolve as resolveExports } from 'resolve.exports';
import type defaultResolver from 'jest-resolve/build/defaultResolver';

interface ResolveOptions {
rootDir: string;
basedir: string;
paths: string[];
moduleDirectory: string[];
browser: boolean;
extensions: string[];
defaultResolver: typeof defaultResolver;
}
import type { ResolverOptions } from 'jest-resolve';

let compilerSetup;
let ts;
Expand Down Expand Up @@ -38,7 +28,7 @@ function getCompilerSetup(rootDir: string) {
return { compilerOptions, host };
}

module.exports = function (path: string, options: ResolveOptions) {
module.exports = function (path: string, options: ResolverOptions) {
const ext = extname(path);
if (ext === '.css' || ext === '.scss' || ext === '.sass' || ext === '.less') {
return require.resolve('identity-obj-proxy');
Expand Down
53 changes: 35 additions & 18 deletions packages/jest/src/plugins/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Config } from '@jest/types';
import {
CreateNodes,
CreateNodesContext,
Expand All @@ -13,22 +14,20 @@ import {
TargetConfiguration,
writeJsonFile,
} from '@nx/devkit';
import { dirname, isAbsolute, join, relative, resolve } from 'path';

import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync, readFileSync } from 'fs';
import { readConfig, replaceRootDirInPath } from 'jest-config';
import jestResolve from 'jest-resolve';
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes';
import {
clearRequireCache,
loadConfigFile,
} from '@nx/devkit/src/utils/config-utils';
import { getGlobPatternsFromPackageManagerWorkspaces } from 'nx/src/plugins/package-json';
import { combineGlobPatterns } from 'nx/src/utils/globs';
import { getNamedInputs } from '@nx/devkit/src/utils/get-named-inputs';
import { existsSync, readdirSync, readFileSync } from 'fs';
import { minimatch } from 'minimatch';
import { hashObject } from 'nx/src/devkit-internals';
import { getGlobPatternsFromPackageManagerWorkspaces } from 'nx/src/plugins/package-json';
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';
import { combineGlobPatterns } from 'nx/src/utils/globs';
import { dirname, isAbsolute, join, relative, resolve } from 'path';
import { getInstalledJestMajorVersion } from '../utils/version-utils';

const pmc = getPackageManagerCommand();

Expand Down Expand Up @@ -167,6 +166,9 @@ async function buildJestTargets(
}

const rawConfig = await loadConfigFile(absConfigFilePath);

const { readConfig } =
requireJestUtil<typeof import('jest-config')>('jest-config');
const config = await readConfig(
{
_: [],
Expand Down Expand Up @@ -211,23 +213,24 @@ async function buildJestTargets(

let metadata: ProjectConfiguration['metadata'];
if (options?.ciTargetName) {
// Resolve the version of `jest-runtime` that `jest` is using.
const jestPath = require.resolve('jest');
const jest = require(jestPath) as typeof import('jest');
// nx-ignore-next-line
const { default: Runtime } = require(require.resolve('jest-runtime', {
paths: [dirname(jestPath)],
const { default: Runtime } =
// nx-ignore-next-line
})) as typeof import('jest-runtime');
requireJestUtil<typeof import('jest-runtime')>('jest-runtime');

const jestContext = await Runtime.createContext(config.projectConfig, {
maxWorkers: 1,
watchman: false,
});

const jest = require('jest') as typeof import('jest');
const source = new jest.SearchSource(jestContext);

const specs = await source.getTestPaths(config.globalConfig);
const jestVersion = getInstalledJestMajorVersion()!;
const specs =
jestVersion >= 30
? // @ts-expect-error Jest 30+ expects the project config as the second argument
await source.getTestPaths(config.globalConfig, config.projectConfig)
: await source.getTestPaths(config.globalConfig);

const testPaths = new Set(specs.tests.map(({ path }) => path));

Expand Down Expand Up @@ -343,11 +346,15 @@ function resolvePresetInput(
return null;
}

const { replaceRootDirInPath } =
requireJestUtil<typeof import('jest-config')>('jest-config');
let presetPath = replaceRootDirInPath(projectRoot, presetValue);
const isNpmPackage = !presetValue.startsWith('.') && !isAbsolute(presetPath);
presetPath = presetPath.startsWith('.')
? presetPath
: join(presetPath, 'jest-preset');
const { default: jestResolve } =
requireJestUtil<typeof import('jest-resolve')>('jest-resolve');
const presetModule = jestResolve.findNodeModule(presetPath, {
basedir: projectRoot,
extensions: ['.json', '.js', '.cjs', '.mjs'],
Expand All @@ -369,7 +376,7 @@ function resolvePresetInput(

function getOutputs(
projectRoot: string,
{ globalConfig }: Awaited<ReturnType<typeof readConfig>>,
{ globalConfig }: { globalConfig: Config.GlobalConfig },
context: CreateNodesContext
): string[] {
function getOutput(path: string): string {
Expand Down Expand Up @@ -403,3 +410,13 @@ function normalizeOptions(options: JestPluginOptions): JestPluginOptions {
options.targetName ??= 'test';
return options;
}

let jestPath: string;
/**
* Resolves a jest util package version that `jest` is using.
*/
function requireJestUtil<T>(packageName: string): T {
jestPath ??= require.resolve('jest');

return require(require.resolve(packageName, { paths: [dirname(jestPath)] }));
}
15 changes: 15 additions & 0 deletions packages/jest/src/utils/version-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { major } from 'semver';

export function getInstalledJestVersion(): string | null {
try {
return require('jest/package.json').version;
} catch {
return null;
}
}

export function getInstalledJestMajorVersion(): number | null {
const installedVersion = getInstalledJestVersion();

return installedVersion ? major(installedVersion) : null;
}

0 comments on commit 12e6272

Please sign in to comment.