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

Add test scaffolding for vscode-graphql-syntax (option 2) #3273

Merged
merged 6 commits into from
Jul 23, 2023
Merged

Add test scaffolding for vscode-graphql-syntax (option 2) #3273

merged 6 commits into from
Jul 23, 2023

Conversation

AaronMoat
Copy link
Contributor

@AaronMoat AaronMoat commented Jun 24, 2023

Resolves: #2343

Alternative to #3267 using much more custom code, but I think it gives us more control. Very keen to see what people think!

@changeset-bot
Copy link

changeset-bot bot commented Jun 24, 2023

⚠️ No Changeset found

Latest commit: 1d7d118

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@acao
Copy link
Member

acao commented Jun 24, 2023

@AaronMoat oh this is very nice, and very easy to read!

In general, writing tests for anything vscode related will involve complicated test tooling, but this seems like it will really do the trick!

@acao
Copy link
Member

acao commented Jun 24, 2023

I wonder why this doesn't seem to trigger a codecov report?

@AaronMoat
Copy link
Contributor Author

image

I guess it did? but there's no new "code" tested by this change technically, "just" the package.json (from the eyes of coverage)

@acao
Copy link
Member

acao commented Jun 24, 2023

weird, i don't see the comment for some reason ! but yes looking at the diff, no new coverage data

I guess it makes sense, as istanbul (or whatever jest uses now) wouldn't have a way to instrument JSON for coverage in any way it could recognize.

That's ok though, coverage data is only a rough metric for code quality, I think we can all agree how important this test suite is!

@AaronMoat
Copy link
Contributor Author

@acao no comment - I was clicking through to the github actions 'details' :)

image

@AaronMoat
Copy link
Contributor Author

So @acao seems you're a fan of this approach over the other one. Is that right - should I close off the other one and prefer this? I might flesh it out with a few more test examples.

Do you prefer inline snapshots or proper snapshots?

There's a few options

  1. expect(fileName).toMatchSnapshot()
  2. expect(fileName).toMatchInlineSnapshot(stuff)
  3. expect(/* GraphQL */"blah").toMatchSnapshot()
  4. expect(/* GraphQL */"blah").toMatchInlineSnapshot(stuff)

Any thoughts on that as well?

@acao
Copy link
Member

acao commented Jun 25, 2023

all very good questions! I'm not sure I am ready to decide yet, and I'd be interested in what my colleagues think as well

@dimaMachina
Copy link
Collaborator

dimaMachina commented Jun 27, 2023

@AaronMoat Nicely job, thanks for putting effort into this. I prefer this PR rather than the first one. (I found here more readable)

But to be honest it's not exactly what I am searching for. The goal of this PR is to test tokenizing right? We can still keep it, it's a good job. (let me know if I am right in terms I am still new to this project and still learning 😅)

But I have in mind to have image syntax highlighting snapshots tests, and not just snapshots tests. I wanna test the right syntax highlighting and throw an error when % of pixels have a difference.

I was inspired by these image tests in satori https://github.com/vercel/satori/tree/main/test/image_snapshots
and I added tests for our open graph images https://github.com/the-guild-org/docs/blob/main/packages/og-image/src/__image_snapshots__/handler-test-ts-packages-og-image-src-handler-test-ts-handler-should-align-title-and-have-container-padding-1-snap.png and it fails (sometimes) when a new version of satori is out.

@dimaMachina
Copy link
Collaborator

There's a few options

expect(fileName).toMatchSnapshot()
expect(fileName).toMatchInlineSnapshot(stuff)
expect(/* GraphQL /"blah").toMatchSnapshot()
expect(/
GraphQL */"blah").toMatchInlineSnapshot(stuff)

I prefer the first one expect(fileName).toMatchSnapshot()

  • snapshots can be long
  • the same filename we can reuse to test mentioned previously image snapshots tests

@acao
Copy link
Member

acao commented Jun 27, 2023

@B2o5T great idea! I've built some custom image snapshot tooling before, for a geospatial project. that might be the most readable option. the underlying libraries for image diffing used by percy and these jest plugins are quite surprisingly simple and powerful

the problem herein is that we have to find a way to render an image of the syntax highlighting in the first place. monaco for example uses a different grammar than vscode, but I'm sure we can find a library to render vscode grammars somewhere

@dimaMachina
Copy link
Collaborator

that might be the most readable option.

exactly! I found image snapshots, the most maintainable option

the problem herein is that we have to find a way to render an image of the syntax highlighting in the first place

you have a point, this is most trickiest part :)

@AaronMoat
Copy link
Contributor Author

Fully agree that images would be better. The how is the big question :)

@acao
Copy link
Member

acao commented Jul 7, 2023

I think I'll just build this library and a set of runners (cypress, playwright, etc) for fun 😜

Shared Steps

  1. render dom with codemirror, codemirror textmate mode and load our grammars
  2. domToSvG(), then

test assert

pixelmatch against each SVG, exit non 0 if pixelalmatch doesn't match, write out failing pixelmatch results with res error highlighting for the diff

snap update

save svg output

@acao
Copy link
Member

acao commented Jul 7, 2023

Here is another upcoming issue, you may have mentioned already, is that there are multiple issues regarding embedded languages:
microsoft/vscode-textmate#207

and this:

Cross - grammar injections are currently not supported.

which I believe will mean that "injectionSelector" rules won't work with this approach of converting the grammars to textmate grammars

not to fret though, being able to test just our rules for graphql strings is most important, perhaps we can help contribute support for injectionSelector to vscode-textmate

update: I think I misread or the readme is out of date, I think injectionSelector support will work as of microsoft/vscode-textmate#11

@acao
Copy link
Member

acao commented Jul 7, 2023

looking to carbon for inspiration on code -> image front, and I found this! haha!

@acao
Copy link
Member

acao commented Jul 7, 2023

here is a nice rust library that seems to use modern stdout features to print code without a browser at all. the problem I anticipate is that this rendering will be inconsistent across various local and CI environments

https://github.com/trishume/syntect#screenshots

@acao
Copy link
Member

acao commented Jul 8, 2023

after some research, vscode is one of the few web IDEs that supports injectionSelector textmate rules, so it would be very difficult to do visual snapshot testing. @AaronMoat 's approach here is probably the best approach, of using the tokenizer that vscode-texmate provides. the only potential route for rendering these grammars completely in the web I can find is to use https://github.com/CodinGame/monaco-vscode-api which is quite complex to orchestrate. if it were just the graphql grammar, it wouldn't be too hard to get image snapshot diffing, but the embedded grammars are some of the most important to test

@acao
Copy link
Member

acao commented Jul 23, 2023

just added more tests cases and organized things a bit. lets go with this strategy for now so we have something to make improvements on and to use as a regression suite until we figure out a good solution for using images. i think shiki supports injection rules, which is heavily based on vscode-textmate

@acao acao merged commit d314dc7 into graphql:main Jul 23, 2023
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.

proposal: introduce VSCode Textmate grammar test for testing grammar
3 participants