I have an enum defined in types.ts
:
export enum Handedness {
Left,
Right,
Both,
}
export type State = {
count: number
handedness: Handedness
}
and I have an object being initialized in state.ts
:
import { State, Handedness } from './types'
export const initial: State = {
count: 0,
handedness: Handedness.Both
}
When I run tests (via jest
) for this project, state.ts
generates an error TypeError: Cannot read property 'Both' of undefined
, telling me that Handedness
isn't defined at the time it's referenced. But I'm exporting it from its module and importing it before I use it... so it should be defined.
I've found other similar questions asking about undefined enums, but they seem to all be asking about runtime. This is a compile time problem as far as I can tell.
I don't see what I would be doing wrong here. I import other types in other places without issue. But this enum simply doesn't want to work. What is going on here and how can I work around it?
Well, this isn't a way to get this to work, but this GitHub PR explains that ts-jest
won't support enums that work like this. I've changed all uses to (e.g.) ("both" as Handedness)
and it works. So, that's not an explanation, it's a workaround.
You can also try using const enums.
So change:
export enum Whatever { ... }
to
export const enum Whatever { ... }
Apparently as of ts-jest
version 23.10 you can just do this. Much better!
I encountered similar error while running my code with Jest. The issue was caused by a React component that imported an enum type from a module which was also being mocked with jest.mock. I fixed the issue by using jest.spyOn for method that I needed instead of whole module.
from:
jest.mock("example/path", () => ({ useExampleMethod: () => {...
to:
import * as exampleModule from "example/path"; jest.spyOn(exampleModule , 'useExampleMethod').mockReturnValue({...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With