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

fix(testing): update jest batch mode #12764

Merged
merged 5 commits into from
Oct 27, 2022

Conversation

barbados-clemens
Copy link
Contributor

@barbados-clemens barbados-clemens commented Oct 21, 2022

jest go burrrr now

Current Behavior

jest tests are slower in Nx than the jest CLI

(shout out @Coly010 for finding the config changes)

Note:

  • requires using the NX_BATCH_MODE=true env var to be set.
  • using --output-style=stream looks best/is easier to see what is going on (this should be the default for batch mode)
  • ISSUE: if one project fails all projects are marked as failure. this is a bug that needs to be fixed in a follow up PR)

Expected Behavior

test are the same speed as jest cli

Related Issue(s)

TODO

  • add tests for basic usage of batchJest
    Fixes #

@vercel
Copy link

vercel bot commented Oct 21, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
nx-dev ✅ Ready (Inspect) Visit Preview Oct 27, 2022 at 2:35PM (UTC)

@barbados-clemens barbados-clemens added the scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx label Oct 21, 2022
Copy link
Contributor

@Coly010 Coly010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a bunch of comments just signalling where I saw console logs we should remove

packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
packages/jest/src/executors/jest/jest.impl.ts Outdated Show resolved Hide resolved
@Coly010 Coly010 self-requested a review October 24, 2022 15:02
Copy link
Contributor

@Coly010 Coly010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment on lines 165 to 184
/* The display name in the jest.config.js is the correct project name jest
* uses to determine projects. It is usually the same as the Nx projectName
* but it can be changed. The safest method is to extract the displayName
* from the config file, but skip the project if it does not exist. */
const displayNameValueRegex = new RegExp(
/(['"]+.*['"])(?<=displayName+.*)/,
'g'
);
const fileContents = readFileSync(configPath, { encoding: 'utf-8' });
if (!displayNameValueRegex.test(fileContents)) {
console.warn(stripIndents`Could not find "displayName" in ${configPath}.
Please add a "displayName" to the Jest Config File.
Skipping this project (${task.split(':')[0]})...`);
continue;
}

const displayName = fileContents
.match(displayNameValueRegex)
.map((value) => value.substring(1, value.length - 1))[0];
selectedProjects.push(displayName);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barbados-clemens Here is the code to find the actual jest project 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be fine for now, but there is a snowballs chance in hell that someone is using a var to reference the displayName. i.e.

const myName = 'something-cool';

export default {
  displayName: myName
}

also more likely,

export default {
  displayName: {
    name: 'something-cool',
    color: 'blue'
  }
}

in which case we'd error/warn about it missing when it was just configured differently.

I think for an alpha it's fine, but we should make sure we handle this at some point.

so maybe we should be more explicit in the error we are expecting the displayName to be a string for now?

/(['"]+.*['"])(?<=displayName+.*)/,
'g'
);
const fileContents = readFileSync(configPath, { encoding: 'utf-8' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd look at not using sync here. We should try and optimize reading the files, because that was the original issue with jest 😅

Or do some measurements and see if we can get away with sync

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree. we should perf this and maybe read in parallel with the a promise. though idk how that will scale either with say 1000 projects trying to read in parallel.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the concern about potentially having too many processes in parallel, but we should definitely perf it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could limit how many parallel calls are allowed

@FrozenPandaz FrozenPandaz merged commit cd4e983 into nrwl:master Oct 27, 2022
@barbados-clemens barbados-clemens deleted the fix/jest-batch-go-brrrr branch October 28, 2022 13:31
@github-actions
Copy link

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
scope: testing tools Issues related to Cypress / Jest / Playwright / Vitest support in Nx
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants