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

Refactor to improve tests #37

Merged
merged 10 commits into from
Mar 24, 2022
Merged

Refactor to improve tests #37

merged 10 commits into from
Mar 24, 2022

Conversation

remcohaszing
Copy link
Member

@remcohaszing remcohaszing commented Mar 14, 2022

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)
  • If applicable, I’ve added docs and tests

Description of changes

This check was previously deemed unreachable, but it can be reached.

This is based on #35.

This way of testing doesn’t require to use any delays or timeouts.

This also works by sending a response, awaiting a reply, then asserting
the reply, instead of sending a number of requests only to assert all
responses together later.

Also any logging messages sent from the connection.console are logged to
the console in tests.

All of these changes together make troubleshooting any tests much nicer
to deal with.
One more test is remaining. It got stuck after rewriting.
This check was previously deemed unreachable, but it can be reached.
@github-actions github-actions bot added the 👋 phase/new Post is being triaged automatically label Mar 14, 2022
@github-actions

This comment has been minimized.

@github-actions github-actions bot added 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Mar 14, 2022
@ChristianMurphy ChristianMurphy added the 🕸️ area/tests This affects tests label Mar 14, 2022
@wooorm
Copy link
Member

wooorm commented Mar 14, 2022

What's up with CI?

Copy link
Member

@wooorm wooorm left a comment

Choose a reason for hiding this comment

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

Couple Qs! Looks pretty good. What do you make of the CI failure?

lib/index.js Show resolved Hide resolved
test/index.js Outdated Show resolved Hide resolved
test/index.js Outdated Show resolved Hide resolved
test/index.js Outdated Show resolved Hide resolved
test/index.js Outdated Show resolved Hide resolved
@wooorm wooorm mentioned this pull request Mar 15, 2022
5 tasks
@remcohaszing
Copy link
Member Author

I think I’ve seen this CI failure locally once across running the tests dozens of times locally. It must be a rare race condition. I’ll investigate.

@codecov-commenter

This comment was marked as resolved.

@@ -82,6 +82,7 @@
"typeCoverage": {
"atLeast": 100,
"detail": true,
"ignoreNested": true,
Copy link
Member Author

Choose a reason for hiding this comment

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

I added this, because InitializeRequest.type is of type ProtocolRequestType<_InitializeParams & WorkspaceFoldersInitializeParams & WorkDoneProgressParams, InitializeResult<any>, never, InitializeError, void>.

In other words: An upstream type includes an any type, which we don’t even use.

Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting. I didn’t know about this field. It sounds like it might be useful in other projects too

})
const connection = startLanguageServer(t, 'remark.js', '.')
const initializeResponse = await connection.sendRequest(
InitializeRequest.type,
Copy link
Member

Choose a reason for hiding this comment

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

I’m not a fan of these *.types (e.g., InitializeRequest.types). It requires knowing this VS Code project that doesn’t have docs. I am not a fan of depending on projects that are not documented (and whose code is unreadable).

Can we somehow pass, say, 'initialize', to, well, send an initialize event?

Rest looks good!

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of *.type is what allows to infer the request and response types. It’s not a string, but an object which is typed in a way that allows TypeScript to infer types. Using a string removes type safety, meaning we need to manually add type annotations instead.

Personally I’m not a fan of how this has been implemented by vscode-languageserver-protocol either, but I do think this is the way to go. This protects us from using wrong request and response types, and it closely resembles how a real client would implement it.

As for the project being undocumented: A basic usage example is missing from their readme, but once you get that figured out, the rest is both pretty self-explanatory and well documented through jsdoc.

@wooorm
Copy link
Member

wooorm commented Mar 24, 2022

Anything particar to check otherwise? Stuff you're 🤔🤷‍♂️ about?

@remcohaszing
Copy link
Member Author

I’m not fond of the last commit (4e7e25d), but it’s an upstream issue.

It might be note-worthy to state I reran npm run test-api in a for loop 100 times several times to troubleshoot that, and there are no flaky tests anymore.

@wooorm wooorm changed the title Add test for formatting unsynchronized documents Refactor to improve tests Mar 24, 2022
@wooorm wooorm merged commit 2f5c4af into unifiedjs:main Mar 24, 2022
@wooorm wooorm added the 💪 phase/solved Post is done label Mar 24, 2022
@wooorm
Copy link
Member

wooorm commented Mar 24, 2022

thanks!

@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Mar 24, 2022
@wooorm
Copy link
Member

wooorm commented Mar 30, 2022

https://github.com/unifiedjs/unified-language-server/blame/8efe2bc5bb9a2867e9033b269c7351dce79f37d5/test/index.js#L753 seems to indicate that console.logs should work, but when I add one to the server the tests crash?

On L1 in lib/index.js:

console.log('xxx:')

Yields:

/Users/tilde/Projects/oss/unified-language-server/node_modules/vscode-jsonrpc/lib/common/messageReader.js:138
                    throw new Error('Header must provide a Content-Length property.');
                    ^

Error: Header must provide a Content-Length property.
    at StreamMessageReader.onData (/Users/tilde/Projects/oss/unified-language-server/node_modules/vscode-jsonrpc/lib/common/messageReader.js:138:27)
    at Socket.<anonymous> (/Users/tilde/Projects/oss/unified-language-server/node_modules/vscode-jsonrpc/lib/common/messageReader.js:122:18)
    at Socket.emit (node:events:520:28)

@remcohaszing
Copy link
Member Author

You need to write connection.console.log. I do think hijacking the console to support this would be a nice feature too.

@wooorm
Copy link
Member

wooorm commented Mar 30, 2022

Ohhh right pff.
Do you mean hijacking in the tests, to make our life easier, or to hijack in the server, so that debugging in plugins would be sent across the protocol as well?

@remcohaszing
Copy link
Member Author

I mean for the server to hijack the global console in the server process, so any console output is also sent to the language client. I don’t think it makes sense for the language server to crash, just because some third party package calls console.log

@wooorm
Copy link
Member

wooorm commented Mar 30, 2022

it’s theoretically a bit weird how to do it if multiple servers are started, although I don’t see that practically happening?

I was under the impression this error only occurred in the tests but now I’m not sure, as you say, I guess it occurs for anything that writes to stdout? Alternatively then we could point console.* to console.error so that it writes to stderr? 🤷‍♂️

@remcohaszing
Copy link
Member Author

I was under the impression this error only occurred in the tests but now I’m not sure, as you say, I guess it occurs for anything that writes to stdout?

Only if the server is started using the stdio protocol. So for vim-lsp, nvim-lspconfig, and emacs lsp-mode: yes, for vscode-remark: no.

I created #41 for further discussion.

@remcohaszing remcohaszing deleted the add-missing-test branch August 6, 2022 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🕸️ area/tests This affects tests 💪 phase/solved Post is done
Development

Successfully merging this pull request may close these issues.

4 participants