jest-community / jest-editor-support

A module for handling editor to jest integration
MIT License
28 stars 21 forks source link

Does not parse function.name syntax #55

Closed aciccarello closed 3 years ago

aciccarello commented 3 years ago

Using the function.name property in describe blocks is a pattern to allow refactoring to update test names automatically. However this fails the babel parser for jest-editor-support with a "TypeError: failed to update namedBlock" error.

describe(functionDotName.name, () => {
    it('should parse', () => {});
});

function functionDotName() {}

Since this is a simple enough expression to parse, I would like to see jest-editor-support look at the member expression for a identifier property with a name of "name" and then return the name as the object name.

Inserted at src/parsers/babel_parser.js#L46:

        case 'MemberExpression':
            const property = arg.property
            if (property && property.type === 'Identifier' && property.name === 'name') {
                name = arg.object.name;
                break;
            }

This issue blocks firsttris/vscode-jest-runner#129

connectdotz commented 3 years ago

I agree the parser should not throw an error in this case. However, not sure about using the "name" property to signal for the name... Is this a generic pattern documented somewhere?

But at the minimal, instead of throwing an error, we can change the "default" to log a warning then return a "unknown" marker so it will not "block" the rest of the logic, as the test "name" is not necessarily a "must-have" for all callers...

connectdotz commented 3 years ago

@aciccarello feel free to take a look at PR #57

aciccarello commented 3 years ago

@connectdotz Thanks for taking a look and putting together a PR. I think avoiding an error is a good start to prevent it failing the whole file.

Unfortunately for vscode-jest-runner that won't allow running function.name blocks. It's a pattern I use, but I understand if you consider that unsupported.