Skip to content
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

EuiComboBox failing in Jest environment #7505

Closed
jlove-dev opened this issue Feb 2, 2024 · 3 comments
Closed

EuiComboBox failing in Jest environment #7505

jlove-dev opened this issue Feb 2, 2024 · 3 comments
Labels
answered Issues answered by the EUI team - auto-closes open issues in 7 days if no follow-up response question

Comments

@jlove-dev
Copy link

jlove-dev commented Feb 2, 2024

Describe the bug
I've run into an issue where, even in a brand new CRA, EuiComboBox throws errors when trying to test with Jest. Specifically, here's what I see:

TypeError: Cannot set properties of undefined (setting 'font')

Which is coming from:

at new CanvasTextUtils (node_modules/@elastic/eui/lib/services/canvas/canvas_text_utils.js:59:25)
at node_modules/@elastic/eui/lib/components/combo_box/combo_box_input/combo_box_input.js:61:26

It looks like this is primarily used for the combobox, I can only find 8 files in Elastic/eui which mostly seem related.

I've also created a reproduction repo which shows this.

Impact and severity
Medium

I haven't been able to find a workaround however it does cause any components which use EuiComboBox to not have test cases. I tried to mock HTMLCanvasElement however that didn't seem to help.

Environment and versions

  • EUI version: 92.2.1
  • React version: 18.2.0
  • Kibana version (if applicable):
  • Browser: Chrome/Firefox
  • Operating System: Windows

To Reproduce

Reproduction Repo

^ I made this in roughly five minutes using just CRA and installing Jest. I don't believe this is a Jest issue since it only happens with this component out of EUI but I'm open to being corrected.

Expected behavior
I would expect this error to not happen. It results in any Jest test which includes EuiComboBox to fail.

Minimum reproducible sandbox
Reproduction Repo

I'm not sure how I can add Jest to sandbox, if anyone has a suggestion on that then please let me know.

@jlove-dev jlove-dev added bug ⚠️ needs validation For bugs that need confirmation as to whether they're reproducible labels Feb 2, 2024
@cee-chen
Copy link
Contributor

cee-chen commented Feb 2, 2024

Heya @jlove-dev! The answer to this is that you need to mock the underlying CanvasTextUtils file. We've created a mock for this here:

https://github.com/elastic/eui/blob/main/src/services/canvas/canvas_text_utils.testenv.ts

You can import or require this file via:

const { CanvasTextUtils } = require('@elastic/eui/test-env/services/canvas/canvas_text_utils');

You'll then need to set up your Jest environment with a jest.mock(), which you can do either in the file(s) where you're testing EuiComboBox, or that you can pass into your Jest setupFiles config to apply to all tests. Here's EUI's own example usage:

jest.mock('./../../../src/services/canvas', () => {
const rest = jest.requireActual('./../../../src/services/canvas');
const {
CanvasTextUtils,
} = require('./../../../src/services/canvas/canvas_text_utils.testenv');
return { ...rest, CanvasTextUtils };
});

setupFiles: [
'<rootDir>/scripts/jest/setup/enzyme.js',
'<rootDir>/scripts/jest/setup/throw_on_console_error.js',
'<rootDir>/scripts/jest/setup/mocks.js',
],

@cee-chen cee-chen added question answered Issues answered by the EUI team - auto-closes open issues in 7 days if no follow-up response and removed bug ⚠️ needs validation For bugs that need confirmation as to whether they're reproducible labels Feb 2, 2024
@jlove-dev
Copy link
Author

Thank you so much @cee-chen! I really appreciate the support here and it makes sense to go about it this way.

@cee-chen
Copy link
Contributor

cee-chen commented Feb 2, 2024

Absolutely, anytime!! ❤️ Thanks for taking the trouble to clearly fill out the issue template and make a reproduction repo - it made answering a breeze!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Issues answered by the EUI team - auto-closes open issues in 7 days if no follow-up response question
Projects
None yet
Development

No branches or pull requests

2 participants