jestjs / jest

Delightful JavaScript Testing.
https://jestjs.io
MIT License
44.06k stars 6.44k forks source link

feat: Name test callbacks for profiling #10099

Open eps1lon opened 4 years ago

eps1lon commented 4 years ago

πŸš€ Feature Proposal

Assign the test name to the test callback if the test callback is not named.

Naively add before https://github.com/facebook/jest/blob/eff3eaaad2bde223365da6522cfac7960267fb46/packages/jest-circus/src/index.ts#L182

let actualName = testName;
if (fn.name === "") {
    Object.defineProperty(fn, 'name', {
        get() {
            return actualName
        },
        set(name) {
            actualName = name
        }
    })
}

Motivation

The passed callback to e.g. test('my test description', () => { /* the callback */ }) is hard to find when profiling tests. Right now we have to explicitly name them via test('my test description', function mySlowTest() { /* the callback */ }).

Example

@kentcdodds encountered the same issue and made a video explaining the issue: https://www.youtube.com/watch?v=RB2g-o39upo.

Pitch

Reduces barrier to entry into profiling jest tests. While I would consider naming functions a tool you should know about for profiling, I would always prefer to not change code when profiling/debugging.

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 14 days.

eps1lon commented 2 years ago

Comment

github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

eps1lon commented 1 year ago

Bump

github-actions[bot] commented 6 months ago

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

eps1lon commented 6 months ago

Bump

SimenB commented 6 months ago

Yeah, I don't think this would hurt anything - happy to take a PR πŸ‘