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 importing jest-extended

See original GitHub issue

Bug

jest-extended version: 1.0.0

.ts file:

import * as matchers from "jest-extended";

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:

import "jest-extended";

So: Unassigned import stoped working in version 1.0.0 And wildcard import does not work in typescript because of invalid .d.ts file.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:26
  • Comments:53 (12 by maintainers)

github_iconTop GitHub Comments

29reactions
Ashlookcommented, Oct 15, 2021

Had the same issue here when migrating to (ts-)jest 27 and jext-extended 1.0.0.

Before

I have in global.d.ts :

import 'jest-extended';

In jest.config.ts :

/* ... */
setupFilesAfterEnv: ['jest-extended'],
/* ... */

After

I just changed my jest.config.ts and add /all :

/* ... */
setupFilesAfterEnv: ['jest-extended/all'],
/* ... */

Everything seems to work fine with this change, like @silverwind pointed.

11reactions
merrywhethercommented, Nov 16, 2021

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 jest-extended is all TS needs to get it to register the types, and you only have to import it once in your project. This does not however cause anything JS-related to actually happen, and you can’t really work with the exports of jest-extended because they aren’t in the type defs.

Importing jest-extended/all causes the auto-registration of all of the matchers, but doesn’t do anything types-wise. TS will allow un-typed imports like that though, since the side-effects aren’t modeled in TS’s type system.

A bit of bummer to need the two imports, but it’s easier than other solutions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Jest imports with .js extension cause error
Everything is working fine, but when I launch my tests I get an error: Cannot find module '../models/Errors/ServerError.js' from '../src/ ...
Read more >
jest-extended - npm
Start using jest-extended in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type declarations.
Read more >
jest-extended - npm Package Health Analysis - Snyk
The npm package jest-extended was scanned for known vulnerabilities and missing license, and no issues were found. Thus the package was deemed as...
Read more >
jest-extended | Yarn - Package Manager
Problem. Jest is an amazing test runner and has some awesome assertion APIs built in by default. However, there are times when having...
Read more >
Globals - Jest
The TypeScript examples from this page will only work as documented if you explicitly import Jest APIs: import {expect, jest, test} from ...
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