IBM / slack-wrench

Tools to build and test Slack apps
Apache License 2.0
48 stars 26 forks source link

MockedWebClient not constructable #104

Closed erkand-imeri closed 2 years ago

erkand-imeri commented 3 years ago

Hello folks,

I recently updated to Slack bolt: "@slack/bolt": "^2.1.0", and upgraded

to "@slack-wrench/jest-mock-web-client": "^1.3.0",

from "@slack-wrench/jest-mock-web-client": "^1.2.0",

My old tests are failing:

import { App } from '@slack/bolt';
import { MockedWebClient } from '@slack-wrench/jest-mock-web-client';

export const slackAppFactory = (): App => {
  const slackApp = {} as App;
  slackApp.client = new MockedWebClient();
  return slackApp;
};

This expression is not constructable. Type 'MockInstance<MockWebClient, [(string | undefined)?, (WebClientOptions | undefined)?]>' has no construct signatures.ts(2351)

How can i replace/fix this issue?

barlock commented 3 years ago

Hmmm, interesting. After running some experiments I can reproduce this.

I suspect this error has something to do with this @ts-ignore and I'm not sure if it's just typed wrong or if there is a bug in Jest's typings.

There are a few "workarounds" though.

  1. Put a @ts-ignore on that line. It is constructible. Typescript just doesn't think it is.
  2. Set up jest with a __mock__. and use WebClient instead of MockWebClient.
    • You can find instructions for how to setup that automatic mock here
    • And then how to type your tests so it reads it correctly here

Why it started failing after an upgrade is a mystery to me. I can reproduce on 1.2.0 and 1.3.0

erkand-imeri commented 3 years ago

@barlock

I put the //@ts-ignore comment line and tests are passing now but i am still getting this error

node_modules/@slack-wrench/jest-mock-web-client/lib/index.d.ts(2,23): error TS2688: Cannot find type definition file for 'jest-image-snapshot'.
node_modules/@slack-wrench/jest-mock-web-client/lib/index.d.ts(19,14): error TS2416: Property 'admin' in type 'MockWebClient' is not assignable to the same property in base type 'Partial<WebClient>'.
  Type '{ apps: { approve: Mock<any, any>; approved: { list: Mock<any, any>; }; requests: { list: Mock<any, any>; }; restrict: Mock<any, any>; restricted: { list: Mock<any, any>; }; }; ... 4 more ...; users: { ...; }; }' is missing the following properties from type '{ apps: { approve: Method<AdminAppsApproveArguments, WebAPICallResult>; approved: { list: Method<AdminAppsApprovedListArguments, WebAPICallResult>; }; requests: { ...; }; restrict: Method<...>; restricted: { ...; }; }; ... 6 more ...; users: { ...; }; }': barriers, emoji
node_modules/@slack-wrench/jest-mock-web-client/lib/index.d.ts(99,14): error TS2416: Property 'auth' in type 'MockWebClient' is not assignable to the same property in base type 'Partial<WebClient>'.
  Property 'teams' is missing in type '{ revoke: Mock<any, any>; test: Mock<any, any>; }' but required in type '{ revoke: Method<AuthRevokeArguments, WebAPICallResult>; teams: { list: Method<AuthTeamsListArguments, WebAPICallResult>; }; test: Method<...>; }'.
erkand-imeri commented 3 years ago

Hello, @barlock , do you have any suggestion on how to fix the aforementioned error?

barlock commented 3 years ago

The first error, I'm not sure about, I don't know why that package is attempting to pull in type definitions for jest-image-snapshot, it's not referenced directly or indirectly anywhere in that package. That will take some digging. Perhaps installing @types/jest-image-snapshot will be a workaround.

The second one happens when you're using the newer version (6.x) of @slack/web-api which has breaking type definitions (vs ours which is 5.x). Likely you're install is somehow hoisting or overwriting that package with the newer one and causing that break.

I had started the upgrade process last friday but it got too deep for me to finish in an afternoon so I need to figure out how to do it piecemeal. In the meantime, getting your environment back to 5.x will solve that one.