#TypeScript is wrong about function signature

24 messages · Page 1 of 1 (latest)

mighty garnet
#

I am using jest with ts-jest. I have jest, ts-jest, @types/jest, typescript installed in my project. I wrote some tests & everything was working perfectly. I do a little bit more development in completely unrelated sections of my monorepo (pnpm workspaces) & after I get everything working there, I go back to my first project & TypeScript is telling me that jest.test() requires 1-2 arguments when I try to pass 3. This is wrong & I have no idea why all of a sudden after not touching anything in this package it is now broken.

In your test files, Jest puts each of these methods and objects into the global environment. You don't have to require or import anything to use them. However, if you prefer explicit imports, you can do import {describe, expect, test} from '@jest/globals'.

peak pasture
#

@mighty garnet If you control/command click on test does it bring you to the jest definition?

#

(I'm assuming you're just doing the standard jest thing where you just reference test without importing it from anywhere)

mighty garnet
#
declare var test: jest.It;
peak pasture
#

Seems right; do you see this error when you compile/type-check from the command line?

mighty garnet
#

2639 declare var beforeEach: Mocha.HookFunction;
                 ~~~~~~~~~~

  ../../../../node_modules/@types/jest/index.d.ts:2:13
    2 declare var beforeEach: jest.Lifecycle;
                  ~~~~~~~~~~
    'beforeEach' was also declared here.

../../../../node_modules/@types/mocha/index.d.ts:2657:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'Lifecycle', but here has type 'HookFunction'.

2657 declare var afterEach: Mocha.HookFunction;
                 ~~~~~~~~~

  ../../../../node_modules/@types/jest/index.d.ts:4:13
    4 declare var afterEach: jest.Lifecycle;
                  ~~~~~~~~~
    'afterEach' was also declared here.

../../../../node_modules/@types/mocha/index.d.ts:2673:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'Describe', but here has type 'SuiteFunction'.

2673 declare var describe: Mocha.SuiteFunction;
                 ~~~~~~~~

  ../../../../node_modules/@types/jest/index.d.ts:5:13
    5 declare var describe: jest.Describe;
                  ~~~~~~~~
    'describe' was also declared here.

../../../../node_modules/@types/mocha/index.d.ts:2694:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'Describe', but here has type 'PendingSuiteFunction'.

2694 declare var xdescribe: Mocha.PendingSuiteFunction;
                 ~~~~~~~~~

  ../../../../node_modules/@types/jest/index.d.ts:7:13
    7 declare var xdescribe: jest.Describe;
                  ~~~~~~~~~
    'xdescribe' was also declared here.

../../../../node_modules/@types/mocha/index.d.ts:2708:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'It', but here has type 'TestFunction'.

2708 declare var it: Mocha.TestFunction;
                 ~~

  ../../../../node_modules/@types/jest/index.d.ts:8:13
    8 declare var it: jest.It;
                  ~~
    'it' was also declared here.

../../../../node_modules/@types/mocha/index.d.ts:2722:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'It', but here has type 'TestFunction'.

2722 declare var test: Mocha.TestFunction;
                 ~~~~

  ../../../../node_modules/@types/jest/index.d.ts:11:13
    11 declare var test: jest.It;
                   ~~~~
    'test' was also declared here.

../../../../node_modules/@types/mocha/index.d.ts:2729:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'It', but here has type 'PendingTestFunction'.

2729 declare var xit: Mocha.PendingTestFunction;
                 ~~~

  ../../../../node_modules/@types/jest/index.d.ts:10:13
    10 declare var xit: jest.It;
                   ~~~
    'xit' was also declared here.


Found 7 errors in the same file, starting at: ../../../../node_modules/@types/mocha/index.d.ts:2639
mighty garnet
#

I've tried re-installing node_modules pretty much everywhere in the monorepo with no success

#

I am getting the same error in all projects in monorepo using jest

cosmic viper
#

Do you have Mocha installed globally?

mighty garnet
#

I never explicitly installed mocha at all

cosmic viper
#

Might be worth looking at npm why @types/mocha... something installed it

#

You can tell TS to not load it's types by specifying "types": ["node"] in your tsconfig compiler options, probably need more than just node depending on what libraries you use

mighty garnet
#

Yeah a dependency installed mocha.

cosmic viper
#

Open an issue for that library's maintainers asking them to remove it?

TS is doing "the" right thing here, jest and mocha both declare globals, so there's a conflict here...

#

Alternatively importing the jest test functions rather than relying on globals might work...

mighty garnet
#

thanks a lot @cosmic viper @peak pasture

peak pasture
#

@mighty garnet FWIW, specifying "types" in tsconfig is something I'd recommend doing anyway.

#

It's good to avoid having random global types bleeding in.

#

You'd want "jest" in that list, but you don't need to specify every dependency, just the ones with global types. (Which is usually just the test runner + node or webpack or vite or something)