-
-
Notifications
You must be signed in to change notification settings - Fork 225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript error when importing jest-extended #367
Comments
I'm also seeing this issue |
I've been using:
To get 1.0.0 working in my TypeScript app. Nothing else seemed to work! edit: nope that doesn't seem to work after adding some more tests... now I have this error everywhere
But all the previously-passing tests using |
I have the same issue too. My tests used to run fine when doing: import "jest-extended" But after updating to v1.0.0, every function I used gives me the error such as:
I tried the full typescript setup as documented (using global.d.ts, include), but did not help. |
May be related to a2904bd which includes a breaking change when |
I was caught out by this too. I previous had this at the top of each file that used import "jest-extended"; To get things working again with v1.0.0 I removed all these imports and now have: In import "jest-extended/all"; In /// <reference types="jest-extended" /> Now everything is working again properly and it's simpler as |
Had the same issue here when migrating to (ts-)jest 27 and jext-extended 1.0.0. BeforeI have in import 'jest-extended'; In /* ... */
setupFilesAfterEnv: ['jest-extended'],
/* ... */ AfterI just changed my /* ... */
setupFilesAfterEnv: ['jest-extended/all'],
/* ... */ Everything seems to work fine with this change, like @silverwind pointed. |
I tried every option here and on the TS setup in the docs with v1.0.0 and I also get |
Missing functions might just be misses in the type file (https://github.com/jest-community/jest-extended/blob/main/types/index.d.ts). Please send PRs for any missing! 🙂 We should have tests for the types so we're sure we don't miss any... |
here is a fix for that specific missing type |
It seems to me to following commit is the culprit for this -- the default entry point for (i.e. anywhere I guess since it was mentioned off-handedly in the release notes, it was expected, though painful for consumers. 🤷 https://github.com/jest-community/jest-extended/releases/tag/v1.0.0 |
Yes, not extending (without |
I have tried everything here and on the README, and I'm not having typescript compilation errors but instead I am having TypeErrors during the JS execution while using Specifically I'm receiving issues with
|
My workaround for this is as follows: in
|
DO NOT USE
|
Thanks for the fine recommendation and explanation, @codejedi365. Sounds like you could easily fix the root issue in this relatively new project. I'm still quite new to TypeScript, so using Cheers |
not a problem, @replete, thanks for the compliment. My apologies as it seems I came "off the top rope" on your comment. I was trying to make a point for all others in the channel but I don't think it came off that way as I re-read. My apologies. Yes, I'm hoping my Gist post does solve the most of the issues here, I was thankful it worked after hours of messing with it. I think the complexity of Typescript configurations is most to blame and the unknown need of all of us to update our jest config to the new naming convention used by this library. If there is a root issue that needs fixing, I might have time in a few weeks. |
Updated solution, removes need for
|
@ilyub Does the solution in comment above solve your problem? |
@mattphillips I'm not super familiar with this library, or jest, but it seems to me like adding this to the Unless this is only happening because of a bug...? |
None of the above fixed Ultimately the only fix was to downgrade jest-extended to v0.11.5 |
@deuscapturus can you provide more details? Post a link to a Gist that you upload the relevant files, and I'll take a look. Based on your error message it looks like you are importing types directly and that is likely going to have issues. In the meantime, I recommend reviewing my post above with the Gist link. |
This was my fix: // tsconfig.json (note that this shouldn't also be your build config)
"include": [
...,
"setupTestsAfterEnv.ts"
], // jest.config.js
{
...,
setupFilesAfterEnv: ['./setupTestsAfterEnv.ts'],
} // setupTestsAfterEnv.ts
// import main for types & /all for matcher auto-registration
import 'jest-extended';
import 'jest-extended/all'; Importing Importing A bit of bummer to need the two imports, but it's easier than other solutions. |
@merrywhether, how are you compiling your distribution code? I don't recommend adding your setup.ts file to This is why I recommend a To clarify, what actually imports the types for |
@codejedi365 Good callout on the distribution config, as ours is not standard:
We also use babel for actual compilation (beyond type generation), so there's a lot of custom stuff going on that I should've specified. But it was late and I'd spent a bunch of time trying other fixes in the package to see if there was an easy PR; those all would've required a similar setup or restructuring of the package (TS does not make it easy to type raw imports without resorting to relying on have adjacent type files). But I updated my example above with a note. As far as what actually imports the types, it is definitely the |
Hi guys! To resolve the issue of being able to do the named import even though it doesn't cause a runtime issue, the problem is in declaration file don't have named exports for jest-extended module. We can resolve adding each matcher to a module and exports. declare module "jest-extended" {
/**
* Use .toBeEmpty when checking if a String '', Array [], Object {} or Iterable (i.e. Map, Set) is empty.
*/
export function toBeEmpty(): jest.CustomMatcherResult;
} But we have a problem, I couldn't figure out how to make typescript just import the types into the imported method and merge with jest types, e.g: import { toBeEmpty } from 'jest-extended';
expect.extend({
toBeEmpty,
}); This will still import all matchers types, merge with jest and cause runtime issues for matchers don't extended. |
I managed to make the typings for the typescript work when we only use some matchers. //@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; And use in setup.ts import { pass } from 'jest-extended/dist/matchers/pass'
expect.extend({
pass
})
@file: tsconfig.json
{
...,
include: ["./setup.ts", ...]
} With this we can import only the declaration of this specific file, without importing everything else. I believe that it is currently impossible to have exports named with the jest matcher, why if we use for example the strategy of re-exporting all files in an index.js, and import only we need, typescript will follow all the re-exported files and find the declaration files (*.d.ts), which will have method typing that you didn't import. I believe it is better to remove the named exports. |
Hey all I've just opened #407 with an example project that demos how to setup Please let me know if this resolves the issues. Note: From what I can tell there is a separate issue / confusion around manually importing the matchers and the types lining up. I think it may be worth separating this into a different issue. |
My work around:
then created a new file called
Now typescript/vscode can find the types for intellisense. |
Thanks for taking the time to post this, @shahinghasemi, your solution worked great for me! |
why is this such a pain with this library? I don't have problems with other libraries including generally just the jest types, this seems like a real issue still, using 3.0.1 it's not clear how to use a |
I'm using yarn with pnp, ts-jest, and a jest-config.ts, not sure if anything else is relevent, but if sinon-chai for example can do this without significant configuration, then this doesn't impossible |
one thing I see different in the code between the sinon-chai definitely typed and this is
vs just
I'm going to play with this localy to see if it matters |
answers here may also be relevant (sorry for the comment spam, work firewall) https://stackoverflow.com/q/57132428/206466 |
I'm thinking of removing that import. One of the complications is |
yeah, I'm not using |
after doing some more experimentation and thinking, I would think the file would need to have import { MockInstance } from 'jest-mock'
declare module '@jest/globals' { ... haven't gotten that working yet either though. |
can we reopen this regarding |
@xenoterracide have you tried the example setup outlined in #407? |
@mattphillips yes, in what world do you think that will work with
|
@xenoterracide I really don’t like the way you are choosing to communicate on this issue and the confrontational language being used - I’d ask you to show a little more respect when asking for help. I’ve read your comments and am still none the wiser what you’re trying to do, what version of jest you’re using or the issue you’re actually having. So far you’ve just mentioned various technologies that have nothing to do with this project. For us to be able to help you, you would need to write a clear issue of your problem with plenty of context and ideally attach a repro (a repository containing a reproduction of the issue as close to your setup as possible). FYI in the event you cannot reframe from a negative response, please feel free to click the fork button at the top of this repo and solve your problem yourself and maintain a fork. |
System:
Binaries:
|
|
{
"compilerOptions": {
"esModuleInterop": true,
"target": "es2015",
"moduleResolution": "node",
"sourceMap": true,
"lib": ["es2018"],
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"declaration": true,
"baseUrl": ".",
"module": "CommonJS",
"allowJs": false,
"checkJs": false,
"noImplicitAny": true,
"declarationMap": true,
"strict": true,
"skipLibCheck": false,
"strictNullChecks": true,
"resolveJsonModule": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"noEmit": true
},
"exclude": ["**/node_modules", "**/dist", "**/.test"],
"files": []
} |
@xenoterracide Is what you are saying that if you don't use import { expect } from '@jest/globals';
import * as matchers from 'jest-extended';
expect.extend(matchers); Then the import
in VS Code? |
@keeganwitt well firstly, I don't think |
I'll just mention I use ts-jest with @types/jest all the time without importing jest-extended into every file, but that is using As for the usage of @types/jest in general. Eventually, yes, I think it will go away. But I'm not sure that's recommended just yet. They are not identical currently and last I heard there were some things needing improvement (though that might be out of date information now). |
It's recommended by jest, see the first sentence of that documentation. |
Also my problem is the test won't compile... Because it doesn't recognize the extra matchers. It'd be great if the problem was runtime. |
I was thinking of https://jestjs.io/docs/getting-started#type-definitions. I'm not sure which page you're referring to. If you don't want compile time checks of your tests, don't use ts-jest. |
After much frustration and experimentation with the above, I found I only had to add the package and then add:
to my jest config in package.json and then restarting vscode Without the last step, you play a game of whack-a-mole with the various different errors above and the suggested fixes. |
Opened #559 to resolve the original error. |
On my end my issue was I wasn't adding the
to all my jest configs One of those config were hidden inline the |
Bug
jest-extended version: 1.0.0
.ts file:
I get error:
File '.../node_modules/jest-extended/types/index.d.ts' is not a module.
Also, after upgrading to 1.0.0 unassigned import does not work any more:
So:
Unassigned import stoped working in version 1.0.0
And wildcard import does not work in typescript because of invalid .d.ts file.
The text was updated successfully, but these errors were encountered: