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

Upgrade Prettier to v3 #14311

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 0 additions & 2 deletions e2e/__tests__/crawlSymlinks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ test('Should throw if watchman used with haste.enableSymlinks', () => {
expect(stderr).toMatchInlineSnapshot(`
"Validation Error:

haste.enableSymlinks is incompatible with watchman

Either set haste.enableSymlinks to false or do not use watchman"
`);
expect(exitCode).toBe(1);
Expand Down
2 changes: 1 addition & 1 deletion e2e/babel-plugin-jest-hoist/__tests__/typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import {color} from '../entry';
import type {Color} from '../types';

jest.mock('some-module', () => ({} as Partial<{}>), {virtual: true});
jest.mock('some-module', () => ({}) as Partial<{}>, {virtual: true});

jest.mock('../entry', () => {
const color: Color = 'blue';
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"eslint-plugin-jsdoc": "^46.0.0",
"eslint-plugin-local": "link:./.eslintplugin",
"eslint-plugin-markdown": "^3.0.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-prettier": "^5.0.0",
"execa": "^5.0.0",
"find-process": "^1.4.1",
"glob": "^8.0.0",
Expand All @@ -66,7 +66,7 @@
"node-notifier": "^10.0.0",
"p-limit": "^3.1.0",
"pkg-dir": "^5.0.0",
"prettier": "^2.1.1",
"prettier": "^3.0.0",
"promise": "^8.0.2",
"read-pkg": "^5.2.0",
"resolve": "^1.20.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-plugin-jest-hoist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
"@babel/preset-typescript": "^7.0.0",
"@types/babel__template": "^7.0.2",
"@types/node": "*",
"@types/prettier": "^2.1.5",
"babel-plugin-tester": "^11.0.2",
"prettier": "^2.1.1"
"prettier": "^3.0.0"
},
"publishConfig": {
"access": "public"
Expand Down
5 changes: 4 additions & 1 deletion packages/expect/src/asymmetricMatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export abstract class AsymmetricMatcher<T>
{
$$typeof = Symbol.for('jest.asymmetricMatcher');

constructor(protected sample: T, protected inverse = false) {}
constructor(
protected sample: T,
protected inverse = false,
) {}

protected getMatcherContext(): MatcherContext {
return {
Expand Down
9 changes: 5 additions & 4 deletions packages/expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export type ExpectationResult = SyncExpectationResult | AsyncExpectationResult;

export type MatcherFunctionWithContext<
Context extends MatcherContext = MatcherContext,
Expected extends Array<any> = [] /** TODO should be: extends Array<unknown> = [] */,
Expected extends
Array<any> = [] /** TODO should be: extends Array<unknown> = [] */,
> = (
this: Context,
actual: unknown,
Expand Down Expand Up @@ -99,9 +100,9 @@ export interface BaseExpect {
}

export type Expect = {
<T = unknown>(actual: T): Matchers<void, T> &
Inverse<Matchers<void, T>> &
PromiseMatchers<T>;
<T = unknown>(
actual: T,
): Matchers<void, T> & Inverse<Matchers<void, T>> & PromiseMatchers<T>;
} & BaseExpect &
AsymmetricMatchers &
Inverse<Omit<AsymmetricMatchers, 'any' | 'anything'>>;
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-core/src/__tests__/watch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ describe('Watch mode flows', () => {
});

it.each`
ok | option
ok | option
${'✔︎'} | ${'bail'}
${'✖︎'} | ${'changedFilesWithAncestor'}
${'✔︎'} | ${'changedSince'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const getOutputStream = (resolve: (message: string) => void) =>
write(message: string) {
resolve(message);
},
} as NodeJS.WriteStream);
}) as NodeJS.WriteStream;

it('prints the jest version', async () => {
expect.assertions(1);
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-core/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ const checkForConflicts = (
.join(' and ');
error = `
Watch plugins ${plugins} both attempted to register key ${chalk.bold.red(
`<${key}>`,
)}.
`<${key}>`,
)}.
Please change the key configuration for one of the conflicting plugins to avoid overlap.`.trim();
}

Expand Down
19 changes: 11 additions & 8 deletions packages/jest-each/src/table/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,19 @@ const formatTitle = (
rowIndex: number,
): string =>
row
.reduce<string>((formattedTitle, value) => {
const [placeholder] = getMatchingPlaceholders(formattedTitle);
const normalisedValue = normalisePlaceholderValue(value);
if (!placeholder) return formattedTitle;
.reduce<string>(
(formattedTitle, value) => {
const [placeholder] = getMatchingPlaceholders(formattedTitle);
const normalisedValue = normalisePlaceholderValue(value);
if (!placeholder) return formattedTitle;

if (placeholder === PRETTY_PLACEHOLDER)
return interpolatePrettyPlaceholder(formattedTitle, normalisedValue);
if (placeholder === PRETTY_PLACEHOLDER)
return interpolatePrettyPlaceholder(formattedTitle, normalisedValue);

return util.format(formattedTitle, normalisedValue);
}, interpolateTitleIndex(interpolateEscapedPlaceholders(title), rowIndex))
return util.format(formattedTitle, normalisedValue);
},
interpolateTitleIndex(interpolateEscapedPlaceholders(title), rowIndex),
)
.replace(new RegExp(JEST_EACH_PLACEHOLDER_ESCAPE, 'g'), PLACEHOLDER_PREFIX);

const normalisePlaceholderValue = (value: unknown) =>
Expand Down
4 changes: 3 additions & 1 deletion packages/jest-expect/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import type {
} from 'jest-snapshot';

export type JestExpect = {
<T = unknown>(actual: T): JestMatchers<void, T> &
<T = unknown>(
actual: T,
): JestMatchers<void, T> &
Inverse<JestMatchers<void, T>> &
PromiseMatchers<T>;
// Duplicated due to https://github.com/microsoft/rushstack/issues/1709
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-jasmine2/src/__tests__/reporter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Jasmine2Reporter', () => {
description: 'description',
failedExpectations: [],
fullName: name,
} as any as SpecResult);
}) as any as SpecResult;
reporter.suiteStarted({description: 'parent'} as SuiteResult);
reporter.suiteStarted({description: 'child'} as SuiteResult);
reporter.specDone(makeSpec('spec 1'));
Expand Down
3 changes: 1 addition & 2 deletions packages/jest-mock/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,8 +743,7 @@ export class ModuleMocker {
}

return finalReturnValue;
},
metadata.length || 0);
}, metadata.length || 0);

const f = this._createMockFunction(metadata, mockConstructor) as Mock;
f._isMockFunction = true;
Expand Down
4 changes: 2 additions & 2 deletions packages/jest-snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@jest/expect-utils": "workspace:^",
"@jest/transform": "workspace:^",
"@jest/types": "workspace:^",
"@prettier/sync": "^0.3.0",
"babel-preset-current-node-syntax": "^1.0.0",
"chalk": "^4.0.0",
"expect": "workspace:^",
Expand All @@ -46,11 +47,10 @@
"@types/babel__core": "^7.1.14",
"@types/graceful-fs": "^4.1.3",
"@types/natural-compare": "^1.4.0",
"@types/prettier": "^2.1.5",
"@types/semver": "^7.1.0",
"ansi-regex": "^5.0.1",
"ansi-styles": "^5.0.0",
"prettier": "^2.1.1",
"prettier": "^3.0.0",
"tsd-lite": "^0.7.0"
},
"engines": {
Expand Down
9 changes: 7 additions & 2 deletions packages/jest-snapshot/src/InlineSnapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import type {
Program,
TraversalAncestors,
} from '@babel/types';
import {createSynchronizedPrettier} from '@prettier/sync';
import * as fs from 'graceful-fs';
import type {
CustomParser as PrettierCustomParser,
Expand Down Expand Up @@ -60,6 +61,10 @@ export function saveInlineSnapshots(
try {
// @ts-expect-error requireOutside Babel transform
prettier = requireOutside(prettierPath) as Prettier;

if (semver.gte(prettier.version, '3.0.0')) {
prettier = createSynchronizedPrettier({prettierEntry: prettierPath});
}
} catch {
// Continue even if prettier is not installed.
}
Expand Down Expand Up @@ -292,7 +297,7 @@ const runPrettier = (
// Resolve project configuration.
// For older versions of Prettier, do not load configuration.
const config = prettier.resolveConfig
? prettier.resolveConfig.sync(sourceFilePath, {editorconfig: true})
? prettier.resolveConfig(sourceFilePath, {editorconfig: true})
: null;

// Prioritize parser found in the project config.
Expand All @@ -302,7 +307,7 @@ const runPrettier = (
const inferredParser: PrettierParserName | null | undefined =
(config && typeof config.parser === 'string' && config.parser) ||
(prettier.getFileInfo
? prettier.getFileInfo.sync(sourceFilePath).inferredParser
? prettier.getFileInfo(sourceFilePath).inferredParser
: simpleDetectParser(sourceFilePath));

if (!inferredParser) {
Expand Down
21 changes: 16 additions & 5 deletions packages/jest-types/src/Global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,35 +57,46 @@ export type EachTestFn<EachCallback extends TestCallback> = (

interface Each<EachFn extends TestFn | BlockFn> {
// when the table is an array of object literals
<T extends Record<string, unknown>>(table: ReadonlyArray<T>): (
<T extends Record<string, unknown>>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;

// when the table is an array of tuples
<T extends readonly [unknown, ...Array<unknown>]>(table: ReadonlyArray<T>): (
<T extends readonly [unknown, ...Array<unknown>]>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (...args: T) => ReturnType<EachFn>,
timeout?: number,
) => void;

// when the table is an array of arrays
<T extends ReadonlyArray<unknown>>(table: ReadonlyArray<T>): (
<T extends ReadonlyArray<unknown>>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (...args: T) => ReturnType<EachFn>,
timeout?: number,
) => void;

// when the table is a tuple or array
<T>(table: ReadonlyArray<T>): (
<T>(
table: ReadonlyArray<T>,
): (
name: string | NameLike,
fn: (arg: T, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
) => void;

// when the table is a template literal
<T = unknown>(strings: TemplateStringsArray, ...expressions: Array<T>): (
<T = unknown>(
strings: TemplateStringsArray,
...expressions: Array<T>
): (
name: string | NameLike,
fn: (arg: Record<string, T>, done: DoneFn) => ReturnType<EachFn>,
timeout?: number,
Expand Down
20 changes: 10 additions & 10 deletions packages/jest-worker/src/base/__tests__/BaseWorkerPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ describe('BaseWorkerPool', () => {
({
forceExit: jest.fn(),
getStderr: () =>
({once() {}, pipe() {}} as unknown as NodeJS.ReadStream),
({once() {}, pipe() {}}) as unknown as NodeJS.ReadStream,
getStdout: () =>
({once() {}, pipe() {}} as unknown as NodeJS.ReadStream),
({once() {}, pipe() {}}) as unknown as NodeJS.ReadStream,
send: jest.fn(),
waitForExit: () => Promise.resolve(),
} as unknown as WorkerInterface),
}) as unknown as WorkerInterface,
);
});

Expand Down Expand Up @@ -138,15 +138,15 @@ describe('BaseWorkerPool', () => {
pipe(errStream: NodeJS.WritableStream) {
err.push(errStream);
},
} as unknown as NodeJS.ReadableStream),
}) as unknown as NodeJS.ReadableStream,
getStdout: () =>
({
once() {},
pipe(outStream: NodeJS.WritableStream) {
out.push(outStream);
},
} as unknown as NodeJS.ReadableStream),
} as WorkerInterface),
}) as unknown as NodeJS.ReadableStream,
}) as WorkerInterface,
);

const farm = new MockWorkerPool('/tmp/baz.js', {
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('BaseWorkerPool', () => {
getStderr: () => null,
getStdout: () => null,
send: () => null,
} as unknown as WorkerInterface),
}) as unknown as WorkerInterface,
);

const farm = new MockWorkerPool('/tmp/baz.js', {
Expand Down Expand Up @@ -225,7 +225,7 @@ describe('BaseWorkerPool', () => {
getStdout: () => null,
send: jest.fn(),
waitForExit: () => Promise.resolve(),
} as unknown as WorkerInterface),
}) as unknown as WorkerInterface,
);

const pool = new MockWorkerPool('/tmp/baz.js', {
Expand Down Expand Up @@ -253,7 +253,7 @@ describe('BaseWorkerPool', () => {
send: jest.fn(),
waitForExit: () =>
new Promise(resolve => (worker0Exited = resolve)),
} as unknown as WorkerInterface),
}) as unknown as WorkerInterface,
).mockImplementation(
() =>
({
Expand All @@ -262,7 +262,7 @@ describe('BaseWorkerPool', () => {
getStdout: () => null,
send: jest.fn(),
waitForExit: () => Promise.resolve(),
} as unknown as WorkerInterface),
}) as unknown as WorkerInterface,
);

const pool = new MockWorkerPool('/tmp/baz.js', {
Expand Down