Skip to content

Property 'toHaveStyle' does not exist on type 'JestMatchers<any>' #72

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

Closed
jorgearuv opened this issue Dec 23, 2021 · 12 comments
Closed

Property 'toHaveStyle' does not exist on type 'JestMatchers<any>' #72

jorgearuv opened this issue Dec 23, 2021 · 12 comments

Comments

@jorgearuv
Copy link

jorgearuv commented Dec 23, 2021

I've just installed jest-native and when I try to use toHaveStyle() I got this error:

Screen Shot 2021-12-23 at 4 46 12 p m

This is the complete example:

const commonStyles: TextStyle = {
  fontFamily: 'ProximaNova',
  fontSize: 14,
  lineHeight: 20,
  color: getColor('gray-100'),
}

describe('TextInfo', () => {
  it('should render the given text with default styles', () => {
    const { getByText } = render(<TextInfo>{sampleText}</TextInfo>)
    expect(getByText(sampleText)).toHaveStyle(commonStyles)
    expect(getByText(sampleText)).toBeTruthy()
  })

  it('should render the given text with custom styles', () => {
    const { getByText } = render(
      <TextInfo style={{ marginBottom: 8 }}>{sampleText}</TextInfo>,
    )
    expect(getByText(sampleText)).toHaveStyle({
      ...commonStyles,
      marginBottom: 8,
    })
    expect(getByText(sampleText)).toBeTruthy()
  })
})

How can I get rid off this error?

@princealirehman1
Copy link

Same here
image

@flo-sch
Copy link

flo-sch commented Feb 24, 2022

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'],
};

@friederbluemle
Copy link

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).

@zhiqingchen
Copy link

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

@friederbluemle
Copy link

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.

@roni-castro
Copy link

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

@mdjastrzebski
Copy link
Collaborator

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.

@alexamy
Copy link
Contributor

alexamy commented Dec 14, 2022

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

@mdjastrzebski
Copy link
Collaborator

@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.

@alexamy
Copy link
Contributor

alexamy commented Dec 15, 2022

New issue: #136

@PSainz
Copy link

PSainz commented Sep 14, 2023

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

@marocas
Copy link

marocas commented Jan 17, 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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants