Typescript error when manually import the matchers
See original GitHub issueBug
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.
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
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
- 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; ... } }
- 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;
- 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:
- Created 2 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top GitHub Comments
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:
npm i -D jest-extended
(v2.0.0)"setupFilesAfterEnv": ["jest-extended/all"]
to thejest
object in mypackage.json
import 'jest-extended';
to the top of mytests.ts
fileGiven my setup, I did not need a
global.d.ts
file at all.I’m seeing this issue too, hope to see it resolved soon!
Edit: I’ve found that this work-around is working for me