Cannot run jsdom unit tests for files that import Plotly due to missing browser functions
See original GitHub issueWe are using Plotly in our react application and are currently experiencing issues while unit testing components that import the Plotly library either directly, or further down the component tree.
Currently we are using the latest versions of jsdom and mocha to run our unit tests.
It appears the issue is in the Plotly node module, because our unit test suite breaks due to undefined functions in node_modules/plotly.js/dist/plotly.js
For example we get:
ReferenceError: getComputedStyle is not defined
We have attempted to stub that function (as mentioned in the closed issue #1675), but more undefined functions arise and it becomes a bit of a rabbit hole trying to stub all of the missing functions.
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Karma not running unit tests due to "No captured browser" ...
This error can mean that the browser can't find the server. Check if you can reach the server at the URL it mentions....
Read more >Dash Testing | Dash for Python Documentation
End-to-end tests run programmatically, start a real browser session, and click through the Dash app UI. They are slower to run than unit...
Read more >Testing with Node, Jest, and JSDOM - Manning Publications
Differently from the browser, Node can't run that script. Try executing it with node main. js and Node will immediately tell you that...
Read more >Configuring Vitest
import { defineConfig } from 'vitest/config' export default defineConfig({ test: ... Files to include in the test run, using glob pattern.
Read more >DOM Manipulation
Also, the function being tested adds an event listener on the #button DOM element, so we need to set up our DOM correctly...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@etpinard
We use Plotly with its React component .
So I’m not sure how your solution applies here. If you can please give an example.
Any way, If you’ll please give me some guidance on where to start, I can work on that issue. We really need to be able to test our app 😃
@Camru in order to simply run plotly.js in jsdom, looks like you only need to mock two things:
See full example -> https://gist.github.com/etpinard/58a9e054b9ca7c0ca4c39976fc8bbf8a