Skip to content

fix oneline tests are not parsed #1

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

Closed

Conversation

TSMMark
Copy link

@TSMMark TSMMark commented Aug 15, 2022

Thanks for this library! It looks really handy and I'm definitely going to use it for what I'm trying to do.

I noticed it does not notice test functions which are one line. I reproduced the issue by updating the test fixtures with one-line tests to illustrate that the test still passes, even tho the snapshot does not reflect the one-line tests


I haven't even looked at the source code to try to fix this yet, but I will do that next. Any suggestions would be appreciated

@TSMMark
Copy link
Author

TSMMark commented Aug 15, 2022

After looking at the source for this package, unfortunately it looks like a bunch of regex, which I believe is a suboptimal way to parse source code. Has there been any consideration for an AST-based implementation?

@devtin
Copy link
Owner

devtin commented Aug 19, 2022

Hi @TSMMark thanks for being interested in contributing.

Well I did this long ago to create documentation in a micro-services stack we were using. By the time I was creating this I looked into jest's internals, see if I could take advantage of any AST parser, though I was unable to spot anything and I figured a RegExp implementation was perfect for my needs.

I'm gonna add this known issue to the documentation. Not expecting to solve it soon though as it seems odd to me having a test implementation in one line.

Thank you!

@devtin devtin closed this Aug 19, 2022
@TSMMark
Copy link
Author

TSMMark commented Aug 19, 2022

It's not only one line functions, it's also for test functions that have implicit return (no curly braces)

@devtin
Copy link
Owner

devtin commented Aug 19, 2022

Yeah @TSMMark I noticed that and added a disclaimer here referencing your examples.

The implicit return multiline is fixable though. I think all we need to do is change here the RegExp to something like:

export const getCodeBlockPattern = (fnName) => {
    return new RegExp(`^${fnName}(?:\\.([a-z]+))?\\((["'\`])(.*?)\\2[^{]*{?[\\n](.*?)[\\n]}?\\)`, 'gims')
}

And add tests. Maybe for a v1.2.0.

@TSMMark
Copy link
Author

TSMMark commented Aug 19, 2022

How valuable is it actually to capture the it function contents though? E.g.

"code": "// code of what it does
expect(true).toBeTruthy()",

Because this PR works — it fixes the parsing to handle those cases. It just comes at the sacrifice of the ability to capture of the code property for it tests (which I do not need)

@devtin
Copy link
Owner

devtin commented Aug 20, 2022

let me re-take a look during the weekend with fresh eyes!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants