我有UserContext
一个useUser
从src/app/context/user-context.tsx
. 此外,我有一个 index.tsx 文件,src/app/context
其中导出所有子模块。
如果我 spyOnsrc/app/context/user-context
它可以工作,但将导入更改为src/app/context
我得到:
TypeError: Cannot redefine property: useUser at Function.defineProperty (<anonymous>)
这是为什么?
源代码:
// src/app/context/user-context.tsx
export const UserContext = React.createContext({});
export function useUser() {
return useContext(UserContext);;
}
// src/app/context/index.tsx
export * from "./user-context";
// *.spec.tsx
// This works:
import * as UserContext from "src/app/context/user-context";
// This does not work:
// import * as UserContext from "src/app/context";
it("should render complete navigation when user is logged in", () => {
jest.spyOn(UserContext, "useUser").mockReturnValue({
user: mockUser,
update: (user) => null,
initialized: true,
});
})