Recently I upgrade the okta-react library and have transitioned the app to use the new hooks. I am updating my tests now. useOktaAuth()
is undefined
. I want to be able to mock it out so I can test when a user is logged in.
const { authState, authService } = useOktaAuth();
// TypeError: Cannot destructure property `authState` of 'undefined' or 'null'
In order to fix that, I tried mocking the hook by doing:
jest.mock('@okta/okta-react', () => ({
useOktaAuth: () => {
return {
authState: {},
authService: {}
};
}
}));
That isn’t working. I still get Any ideas on how to test these components?
Thanks
Recently I upgrade the okta-react library and have transitioned the app to use the new hooks. I am updating my tests now. useOktaAuth()
is undefined
. I want to be able to mock it out so I can test when a user is logged in.
const { authState, authService } = useOktaAuth();
// TypeError: Cannot destructure property `authState` of 'undefined' or 'null'
In order to fix that, I tried mocking the hook by doing:
jest.mock('@okta/okta-react', () => ({
useOktaAuth: () => {
return {
authState: {},
authService: {}
};
}
}));
That isn’t working. I still get Any ideas on how to test these components?
Thanks
Share Improve this question edited Mar 27, 2020 at 7:40 skyboyer 23.7k7 gold badges61 silver badges71 bronze badges asked Mar 26, 2020 at 21:44 huihuihuihuihuihui 1894 silver badges13 bronze badges5 Answers
Reset to default 5You were close:
jest.mock('@okta/okta-react', () => ({
useOktaAuth: () => ({
authState: { isAuthenticated: true},
authService: { handleAuthentication: jest.fn() }
})
}));
After continuous research and work, this worked for me.
jest.mock("@okta/okta-react/dist/OktaContext", () => ({
useOktaAuth: () => {
return {
authState: {},
authService: {},
};
},
}));
describe("Login", () => {
test("should ", () => {
const wrapper = shallow(<Login authService={null} />);
expect(wrapper.length).toBe(1);
});
});
Note: I passed authService param to the component.
I had almost the same message saying
TypeError: Cannot destructure property 'authState' of '(0 , _oktaReact.useOktaAuth)(...)' as it is undefined.**"
Component's part:
import { useOktaAuth } from "@okta/okta-react";
const Login = () => {
const { authState } = useOktaAuth();
...
TEST:
jest.mock('@okta/okta-react', () => ({
useOktaAuth: () => {
return {
authState: {},
authService: {}
};
}
}));
describe("<Login /> component tests", () => {
it("Should render Login component", done => {
const wrapper = shallow(<Login/>);
setImmediate(() => {
wrapper.update();
try {
// Your assertion here
done();
} catch (error) {
done.fail(error);
}
});
});
});
in addition to Elmatsidis Paul answer I had to add 1 more mock to cover both useOktaAuth and withOktaAuth:
jest.mock('@okta/okta-react', () => ({
useOktaAuth: () => {
return {
authState: {},
authService: {}
};
},
withOktaAuth: (x: any) => x
}));
jest.mock('@okta/okta-react', () => ({
useOktaAuth: () => {
return {
authState: {},
authService: {}
};
}
}));
test('should render CreateDeploymentManifestPage and ShowDeploymentManifest',
() => {
const myMock= jest.fn();
const wrapper = shallow(<CreateDeploymentManifestPage />);
const basicInfo = wrapper.find(DeploymentBasicInfo);
expect(wrapper.containsAllMatchingElements([
<DeploymentBasicInfo />,
])).toBe(true);
});