question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript error when manually import the matchers

See original GitHub issue

Bug

jest-extended version: 1.2.0

Problem

Currently jest-extended types don’t export any of the matchers, so it ends up causing an error with the typescript. Captura de tela de 2022-01-26 10-40-31

We managed to solve this by declaring a module in the types

declare module "jest-extended" {
	export function toBeAfter(received: Date, after: Date): jest.CustomMatcherResult;
}

But doing it this way brings another problem that would be related to types, all manual imports that we are not using will be typed and will cause runtime issues Captura de tela de 2022-01-26 10-52-37 Captura de tela de 2022-01-26 10-52-51

I did some tests to try to solve this problem and a possible solution, but it still wouldn’t be a good solution for this, it would be to move each declaration type to its own folder and import the file directly from the dist

  1. get declaration type for matcher
      //@file: types/index.d.ts
      declare namespace jest {
        // noinspection JSUnusedGlobalSymbols
        interface Matchers<R> {
          /**
           * Note: Currently unimplemented
           * Passing assertion
           *
           * @param {String} message
           */
          pass(message: string): R;
         ...
        }
      }
    
  2. moved to src/matchers/pass/index.d.ts:
    // @file: src/matchers/pass/index.d.ts <- not need this line
    /// <reference types="jest" />
    
    declare global {
      namespace jest {
        interface Matchers<R> {
          /**
           * Note: Currently unimplemented
           * Passing assertion
           *
           * @param {String} message
           */
          pass(message?: string): never;
        }
        interface Expect {
          /**
           * Note: Currently unimplemented
           * Passing assertion
           *
           * @param {String} message
           */
          pass(message?: string): void;
        }
      }
    }
    
    export declare function pass(expected: string, message?: string): jest.CustomMatcherResult;
    
  3. use in setup.ts
    import { pass } from 'jest-extended/dist/matchers/pass'
    
    expect.extend({
      pass
    })
    

I couldn’t find another solution for this, as the behavior of the typescript consists of every js file must accompany its declaration file, if we use for example the strategy of re-exporting all files in an index.js, and importing only what we need, The typescript will follow all re-exported files and find declaration files (*.d.ts), which will have declaration type that you didn’t import and merges with global jest -> expect/matchers.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
davidlavcommented, Feb 15, 2022

I have a minimal setup with no Jest config file and only one file of tests, and—going off the link from the comment above—I was able to get it running today by only:

  1. npm i -D jest-extended (v2.0.0)
  2. Adding the line "setupFilesAfterEnv": ["jest-extended/all"] to the jest object in my package.json
  3. Adding the line import 'jest-extended'; to the top of my tests.ts file

Given my setup, I did not need a global.d.ts file at all.

3reactions
dospunkcommented, Feb 14, 2022

I’m seeing this issue too, hope to see it resolved soon!

Edit: I’ve found that this work-around is working for me

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Module Resolution - TypeScript
This tells the compiler for any module import that matches the pattern "*" (i.e. all values), to look in two locations: "*" :...
Read more >
Typescript error while calling axios - no overload matches this ...
This is a very straightforward Answer. add. AxiosRequestConfig. in your import statement like this import axios, { AxiosRequestConfig ...
Read more >
Assert | Node.js v19.3.0 Documentation
In legacy assertion mode, error messages for objects display the objects, often truncated. To use strict assertion mode: import { strict as assert...
Read more >
Angular compiler options
When true , the compiler does not look at the TypeScript version and does not report an error when an unsupported version of...
Read more >
Control flow and error handling - JavaScript - MDN Web Docs
If a match is found, the program executes the associated statement. A switch statement looks like this: switch (expression) { case ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found