vscode-jest icon indicating copy to clipboard operation
vscode-jest copied to clipboard

Add snippets

Open garyking opened this issue 6 years ago • 3 comments

Any chance snippets could be added to this library? Some of the Jest methods are really long, and dropdowns for them don't appear for mocks.

So for instance, a snippet could be like this:

"mock implementation once": {
  "scope": "javascript,javascriptreact,typescript,typescriptreact",
  "prefix": "mio",
  "body": "${1:mock}.mockImplementationOnce($0)",
  "description": "Mock implementation once",
}

garyking avatar Aug 24 '18 21:08 garyking

This repo seems like a great spot for snippets. Are there any existing standards we should embrace?

seanpoulter avatar Aug 24 '18 21:08 seanpoulter

Not sure about standards, but the snippets docs are a great place to start. Personally, I'd only need snippets for all the Jest mock functions (there are about 20 of them). The expect methods aren't needed, since VSC IntelliSense should already handle them.

And we don't want to pollute the snippets too much, otherwise they might start conflicting with other snippets that users already have installed. Maybe prefix each snippet with j, so rather than mio like in my example above, it could be jmio. And then mockFn.mockClear() could be jmc instead, etc. Because a two-letter prefix is more likely to conflict with something else.

One thing that I notice snippet authors often exclude, is that they don't have $0 in the snippet, to denote the final cursor position. Personally I prefer if it's there though, especially for some types of snippets.

To include snippets in an extension, create a json file containing them, then indicate their path with this. As seen here, it's recommended to apply the snippets to four languages: javascript, javascriptreact, typescript, and typescriptreact.

One big problem is that IntelliSense will not appear within snippets, so you can't use it to auto-fill the mockFn value, unfortunately. And they sometimes have long names, etc. This default behavior can be changed with the editor.suggest.snippetsPreventQuickSuggestions setting.

garyking avatar Aug 24 '18 23:08 garyking

https://marketplace.visualstudio.com/items?itemName=andys8.jest-snippets :)

andys8 avatar Feb 01 '20 12:02 andys8