Skip to content

Commit

Permalink
chore: avoid await expressions (jestjs#14815)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 30, 2023
1 parent 9ced685 commit 2f6984d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
7 changes: 4 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,10 @@ module.exports = {
'no-undef': 'off',
'no-unused-vars': 'off',
'sort-keys': 'off',
'unicorn/prefer-number-properties': 'off',
'unicorn/no-static-only-class': 'off',
'unicorn/consistent-function-scoping': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-static-only-class': 'off',
'unicorn/prefer-number-properties': 'off',
},
},
// demonstration of matchers usage
Expand Down Expand Up @@ -345,6 +346,7 @@ module.exports = {
],
rules: {
'@typescript-eslint/explicit-module-boundary-types': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/consistent-function-scoping': 'off',
},
},
Expand Down Expand Up @@ -711,7 +713,6 @@ module.exports = {
// TODO: turn on at some point
'unicorn/catch-error-name': 'off',
'unicorn/error-message': 'off',
'unicorn/no-await-expression-member': 'off',
'unicorn/no-object-as-default-parameter': 'off',
'unicorn/no-typeof-undefined': 'off',
'unicorn/prefer-object-from-entries': 'off',
Expand Down
2 changes: 1 addition & 1 deletion e2e/__tests__/hasteMapSize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const options = {

test('reports the correct file size', async () => {
const hasteMap = await HasteMap.create(options);
const hasteFS = (await hasteMap.build()).hasteFS;
const {hasteFS} = await hasteMap.build();
expect(hasteFS.getSize(path.join(DIR, 'file.js'))).toBe(5);
});

Expand Down
9 changes: 6 additions & 3 deletions packages/jest-changed-files/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ export const getChangedFilesForRoots = async (
sl.findChangedFiles(repo, changedFilesOptions),
);

const changedFiles = (
await Promise.all([...gitPromises, ...hgPromises, ...slPromises])
).reduce((allFiles, changedFilesInTheRepo) => {
const allVcs = await Promise.all([
...gitPromises,
...hgPromises,
...slPromises,
]);
const changedFiles = allVcs.reduce((allFiles, changedFilesInTheRepo) => {
for (const file of changedFilesInTheRepo) {
allFiles.add(file);
}
Expand Down
21 changes: 10 additions & 11 deletions packages/jest-core/src/runJest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,17 +274,16 @@ export default async function runJest({
const changedFilesInfo = await changedFilesPromise;
if (changedFilesInfo.changedFiles) {
testSchedulerContext.changedFiles = changedFilesInfo.changedFiles;
const sourcesRelatedToTestsInChangedFilesArray = (
await Promise.all(
contexts.map(async (_, index) => {
const searchSource = searchSources[index];

return searchSource.findRelatedSourcesFromTestsInChangedFiles(
changedFilesInfo,
);
}),
)
).flat();
const relatedFiles = await Promise.all(
contexts.map(async (_, index) => {
const searchSource = searchSources[index];

return searchSource.findRelatedSourcesFromTestsInChangedFiles(
changedFilesInfo,
);
}),
);
const sourcesRelatedToTestsInChangedFilesArray = relatedFiles.flat();
testSchedulerContext.sourcesRelatedToTestsInChangedFiles = new Set(
sourcesRelatedToTestsInChangedFilesArray,
);
Expand Down
27 changes: 17 additions & 10 deletions packages/jest-snapshot/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import {processPrettierAst} from './utils';

let prettier: typeof import('prettier');

async function getInferredParser(filepath: string) {
const fileInfo = await prettier.getFileInfo(filepath);

return fileInfo.inferredParser;
}

runAsWorker(
async (
prettierPath: string,
Expand All @@ -25,8 +31,9 @@ runAsWorker(
});

const inferredParser: string | null =
(typeof config?.parser === 'string' && config.parser) ||
(await prettier.getFileInfo(filepath)).inferredParser;
typeof config?.parser === 'string'
? config.parser
: await getInferredParser(filepath);

if (!inferredParser) {
throw new Error(`Could not infer Prettier parser for file ${filepath}`);
Expand All @@ -51,13 +58,13 @@ runAsWorker(
// after the initial format because we don't know where the call expression
// will be placed (specifically its indentation), so we have to do two
// prettier.format calls back-to-back.
return /** @ts-expect-error private API */ (
await prettier.__debug.formatAST(ast, {
...config,
filepath,
originalText: sourceFileWithSnapshots,
parser: inferredParser,
})
).formatted;
// @ts-expect-error private API
const formatAST = await prettier.__debug.formatAST(ast, {
...config,
filepath,
originalText: sourceFileWithSnapshots,
parser: inferredParser,
});
return formatAST.formatted;
},
);

0 comments on commit 2f6984d

Please sign in to comment.