using: import "jest-styled-components/native";
message: Property 'toHaveStylerule' does not exist on type 'Matchers
Had the same problem, fixed it with this:
create a folder in your root with this structure
./@types/jest-styled-components/native/index.d.ts
declare namespace jest {
interface AsymmetricMatcher {
$$typeof: Symbol
sample?: string | RegExp | object | Array<any> | Function
}
// throws an error because the non native version has it also defined, so just comment it
// type Value = string | number | RegExp | AsymmetricMatcher | undefined
interface Options {
media?: string
modifier?: string
supports?: string
}
interface Matchers<R> {
toHaveStyleRule(property: string, value?: Value, options?: Options): R
}
}
Then add that folder to be read by typescript in your tsconfig.json
{
"compilerOptions": {
"typeRoots": ["./@types"]
}
}
Thanks, @p-sebastian! One question though, as it resolved the toHaveStyleRule issue, it seems like I lost my jest types (fails on things like expect, describe, it etc). Any suggestions on how to proceed?
Same issue here, looks like typescript is also looking for a /native typing when importing with /native. It also might be worth to take a look how styled components is fixing this 馃槃
For now, I used the same approach as @p-sebastian but also reusing the existing types.
types folderjest-styled-components.d.tsexport * from 'jest-styled-components';You might need to configure your TypeScript to include your
typesfolder.
None of the above worked for me 馃槥, however I figured out how to make a workaround.
Create extend-expect.d.ts file and place it in project's root (or wherever you want) with the following content:
// tslint:disable:no-namespace
declare namespace jest {
interface Matchers<R> {
toHaveStyleRule: import("node_modules/jest-styled-components/typings/index.d.ts").jest.Matchers["toHaveStyleRule"]
}
}
The
// tslint:disable:no-namespaceline depends on your tslint settings (if you're using it).
Add another path in tsconfig.json's include array, which will be a relative path to the file created in pt. 1, e.g.:
"include": ["./src", "./test", "./mocks", "./extend-expect.d.ts"],
You can also put the
extend-expect.d.tsin existing and already included folder likesrcso you could omit adding a dedicated path for this particular file.
@mkurczewski thanks a lot, that solution works to me!
@mkurczewski Thanks, works for me as well with the following fix (the R generic needs to be forwarded to Matchers):
declare namespace jest {
interface Matchers<R> {
toHaveStyleRule: import("jest-styled-components").jest.Matchers<R>["toHaveStyleRule"]
}
}
toHaveStyleRule definition seems now to be located in node_modules/jest-styled-components/typings/index.d.ts.
in the end, I just needed to add
import 'jest-styled-components';
to my existing src/types/index.d.ts file.
Most helpful comment
None of the above worked for me 馃槥, however I figured out how to make a workaround.
Create
extend-expect.d.tsfile and place it in project's root (or wherever you want) with the following content:Add another path in tsconfig.json's
includearray, which will be a relative path to the file created in pt. 1, e.g.: