Giter Club home page Giter Club logo

Comments (12)

flo-sch avatar flo-sch commented on June 8, 2024 18

It seems like referencing this library directly from within the jest config causes the types not to be loaded. I guess this is how jest & TypeScript together works, not sure if there is a way to automatically fix it.

But a simple workaround is to point the script to a TypeScript file that just imports the library:

Change from this:

// jest.config.js
module.exports = {
  // [...]
  setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
};

To this:

// jest.setupFilesAfterEnv.ts
import '@testing-library/jest-native/extend-expect'
// jest.config.js
module.exports = {
  // [...]
  setupFilesAfterEnv: ['<rootDir>/jest.setupFilesAfterEnv.ts'],
};

from jest-native.

PSainz avatar PSainz commented on June 8, 2024 9

Try adding import '@testing-library/jest-dom' to your test file, it’s what got rid of the error for me.

from jest-native.

friederbluemle avatar friederbluemle commented on June 8, 2024 4

Related: #7
One more thing worth mentioning (in case it wasn't obvious):
Make sure the test setup file ends in .ts (or .tsx), and is not excluded in the default tsconfig used by your IDE (e.g. by sitting in a directory that is part of exclude in tsconfig.json).

it works, but it will cause setup.ts to be builded

That's correct. If your project is published and you want to only ship compiled source files (not tests or setup.ts), then you would need two separate tsconfig.json files. For example, one tsconfig.release.json extending tsconfig.json, which excludes tests. In your package.json you'd have a build script like: tsc -p tsconfig.release.json. Your IDE will pick up tsconfig.json which includes tests to function properly.

from jest-native.

friederbluemle avatar friederbluemle commented on June 8, 2024 3

Related: #7

One more thing worth mentioning (in case it wasn't obvious):

Make sure the test setup file ends in .ts (or .tsx), and is not excluded in the default tsconfig used by your IDE (e.g. by sitting in a directory that is part of exclude in tsconfig.json).

from jest-native.

princealirehman1 avatar princealirehman1 commented on June 8, 2024 1

Same here
image

from jest-native.

roni-castro avatar roni-castro commented on June 8, 2024 1

This problem also happened with me when I added "include": ["src/**/*"] to the tsconfig.json

from jest-native.

zhiqingchen avatar zhiqingchen commented on June 8, 2024

Related: #7

One more thing worth mentioning (in case it wasn't obvious):

Make sure the test setup file ends in .ts (or .tsx), and is not excluded in the default tsconfig used by your IDE (e.g. by sitting in a directory that is part of exclude in tsconfig.json).

it works, but it will cause setup.ts to be builded

from jest-native.

mdjastrzebski avatar mdjastrzebski commented on June 8, 2024

Closing as stale.

@jorgeruvalcaba If the issue persists with the latest release v5.0.0. Please submit a new issue with minimal repro repository. For our convenience please use examples/basic from React Native Testing Library as a base for the PR.

from jest-native.

alexamy avatar alexamy commented on June 8, 2024

Have the same problem. Using jest-expo and typescript test file with explicit jest import.
Suggested solution doesn't work.

from jest-native.

mdjastrzebski avatar mdjastrzebski commented on June 8, 2024

@alexamy Please submit a new issue with minimal repro repository. For convenience please use examples/basic from React Native Testing Library as a base for the PR.

from jest-native.

alexamy avatar alexamy commented on June 8, 2024

New issue: #136

from jest-native.

marocas avatar marocas commented on June 8, 2024

In my case, I just converted the jest.config.js to jest.config.ts and instead of required('@testing-library/jest-dom') import '@testing-library/jest-dom'.

// jest.config.js
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
  ...
  setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
  ...
}
// jest.setup.ts
import '@testing-library/jest-dom'

from jest-native.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.