ts-detox-example icon indicating copy to clipboard operation
ts-detox-example copied to clipboard

Fix Detox typings to not clash with Jest's

Open TheBlinkMan opened this issue 5 years ago • 18 comments

TheBlinkMan avatar Feb 12 '19 16:02 TheBlinkMan

Which version are you using? and could you post an example repo?

solkaz avatar Feb 12 '19 17:02 solkaz

@types/detox was updated to fix that issue in https://github.com/DefinitelyTyped/DefinitelyTyped/pull/32248

solkaz avatar Feb 12 '19 17:02 solkaz

https://github.com/TheBlinkMan/detox-jest-typescript-error

TheBlinkMan avatar Feb 12 '19 20:02 TheBlinkMan

Am i missing something?

TheBlinkMan avatar Feb 12 '19 20:02 TheBlinkMan

I updated the dependencies in the master branch and still got the same result.

TheBlinkMan avatar Feb 12 '19 21:02 TheBlinkMan

Thanks for providing a repo! I am seeing the error when I run the tsc command:

bash-3.2$ yarn tsc index.ts --lib ES2015 --noEmit
yarn run v1.13.0
$ /.../detox-jest-typescript-error/node_modules/.bin/tsc index.ts --lib ES2015 --noEmit
node_modules/@types/detox/index.d.ts:11:11 - error TS2451: Cannot redeclare block-scoped variable 'expect'.

11     const expect: Detox.Expect<Detox.Expect<any>>;
             ~~~~~~

  node_modules/@types/jest/index.d.ts:37:15
    37 declare const expect: jest.Expect;
                     ~~~~~~
    'expect' was also declared here.

node_modules/@types/jest/index.d.ts:37:15 - error TS2451: Cannot redeclare block-scoped variable 'expect'.

37 declare const expect: jest.Expect;
                 ~~~~~~

  node_modules/@types/detox/index.d.ts:11:11
    11     const expect: Detox.Expect<Detox.Expect<any>>;
                 ~~~~~~
    'expect' was also declared here.


Found 2 errors.

error Command failed with exit code 1.

This is an issue with the type definitions for Detox and Jest (they both declare expect as a global constant), and not your code. You can disable type-checking the typings themselves using --skipLibCheck

solkaz avatar Feb 13 '19 00:02 solkaz

That will skip type checking of all node_modules/@types libraries and node_modules d.ts declaration libraries. This is not the ideal solution. I think the only way is to use your detox + jest + typescript guide https://github.com/wix/Detox/pull/1001 and create a new package inside e2e folder.

TheBlinkMan avatar Feb 13 '19 07:02 TheBlinkMan

I understand that and also don't appreciate having to do that; however I don't consider to be a major issue since it still checks if you're using the types from @types correctly. The ideal solution would be to fix the Detox typings to not clash with Jest's, which I've not had time to do

solkaz avatar Feb 13 '19 14:02 solkaz

@solkaz I may be seeing a similar issue whilst attempting to get this great example to run from CI.

Before I open a new issue though, which could be CI specific environment, I just wanted to know if you've tried this repo on any CI service such as appcenter.ms or circleci.com or travis-ci.org or bitrise.io yet?

sebastianmalcolm avatar Feb 13 '19 15:02 sebastianmalcolm

I've not run this specific example on those specific services, no; but the project that I based this example on runs on GitlabCI.

Feel free to create an issue and we'll determine if it's a duplicate or not!

solkaz avatar Feb 13 '19 16:02 solkaz

After the most recent fixes to type Detox typing, as noted on #3, tests are running without errors. However, I agree that running yarn tsc in this project will reveal errors.

So @solkaz, perhaps rename this issue to "Fix Detox typings to not clash with Jest's" and leave it open?

sebastianmalcolm avatar Feb 18 '19 03:02 sebastianmalcolm

Changed title of issue from Still getting "Cannot redeclare block-scoped variable 'expect"' ERROR to Fix Detox typings to not clash with Jest's

solkaz avatar Feb 18 '19 14:02 solkaz

Just encountered this - my quick fix was to include the following at the top of each test file:

import {expect} from 'detox';

Not ideal - but it works.

mtford90 avatar Nov 07 '19 10:11 mtford90

Is there a real solution though? 🙂

developer239 avatar Feb 09 '20 00:02 developer239

I run the next coomand on CI to check TS for all code base

npx tsc --noEmit

and it fail after adding a @types/detox module

node_modules@types/detox/index.d.ts
Error:(15, 11) TS2451: Cannot redeclare block-scoped variable 'expect'.

node_modules/@types/jest/index.d.ts
Error:(47, 15) TS2451: Cannot redeclare block-scoped variable 'expect'.

retyui avatar Dec 09 '20 20:12 retyui

@retyui I have same problem! Anyone resolve it? Please, don't suggest --skipLibCheck.

pirroman avatar Jan 10 '21 17:01 pirroman

As much as pirroman is gonna hate me here, I've put skipLibcheck into my tsconfig and I have no regrets. It made a bunch of node_modules type errors get hidden. Hidden, not fixed, but, you know, hidden.

maludwig avatar Feb 05 '21 03:02 maludwig

I create a copy of detox types and fix conflict locally

retyui avatar Feb 05 '21 19:02 retyui