question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot run jsdom unit tests for files that import Plotly due to missing browser functions

See original GitHub issue

We 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:open
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
bnbarakcommented, Jan 24, 2019

@etpinard

We use Plotly with its React component .

import Plotly from 'plotly.js/dist/plotly.min.js';
import Plot from 'react-plotly.js';

<Plot {...props} />

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 😃

2reactions
etpinardcommented, Dec 29, 2018

@Camru in order to simply run plotly.js in jsdom, looks like you only need to mock two things:

// mock a few things that JSDOM doesn't support out-of-the-box
w.HTMLCanvasElement.prototype.getContext = function() { return null; };
w.URL.createObjectURL = function() { return null; };

See full example -> https://gist.github.com/etpinard/58a9e054b9ca7c0ca4c39976fc8bbf8a

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found