Closed Mr0grog closed 2 years ago
Worth noting: I haven’t tested this extensively with an example project, and it could probably benefit from a test matrix that covers multiple major releases of Jest and Slack.
Published in 1.4.0
Sweet, just tried it out on the project where I was hitting and it works great with the latest Jest and Slack releases! 😄
Related Issue
Supports #118, #104
Related PRs
This PR is not dependent on any other PR
What does this PR do?
This is an attempt, based on the discussion in #118, to make
jest-mock-web-client
compatible with newer Slack and Jest versions, and in general with a wider array of versions by making it a bit more abstract — it builds its typings dynamically by mapping those of the installed Slack version. While I was at it, I wound up solving #104.Description of Changes
This implementation is a bit complicated, partially because I tried to preserve
MockWebClient
as a class, rather than making it just a type. (If just a type is OK, this can definitely be simplified a bit!)The main idea here is to construct the types for
MockWebClient
by mapping the types for Slack’sWebClient
class. This is mainly done with theObjectWithMocks<Type>
type. Then thedeepCopyWithMocks<T>
function is used to implement thatObjectWithMocks<Type>
by making a copy of an object and recursively replacing any functions with Jest mock functions. Finally, theMockWebClientConstructor
function is used to make a classMockWebClient
that inherits concrete versions of those new types (as applied toWebClient
), and that acts just likeMockWebClient
does today. This part is definitely a little unusual, and hopefully the comments in the code do a good job of explaining what’s happening. The function, along with its extra type coercion, are needed because you can’t map types in a class definition, and a class can’t inherit from a type.Alongside all that, I:
jest
and@slack/web-api
to be installed by users of this package.@slack/web-api
andjest
versions as peer dependencies. (Should these ranges have ends, like saying also<28
for Jest? I wasn’t really sure on how conservative or open an approach would be desired here.)MockedWebClient
type fromjest.MockInstance
tojest.MockedClass
, which fixes #104 (I also added a test for this).If
MockWebClient
doesn’t need to be a class, this can definitely be a little simpler to read and understand. See this commit: https://github.com/Mr0grog/slack-wrench/commit/48a09b8a82405e19d305660220797b22b7d442ffBasically, all the stuff from the declaration of
MockWebClientConstructor
down throughMockedWebClient
could be replaced with:I wasn’t sure if it would be OK for it no to be a class anymore, though. 🤷