How to include external library in testing library/storybook

Viewed 117

I'm using Storybook to create stories. I want to create tests of the stories using @storybook/testing-react. However, I'm also using materialize-react which requires that materialize.min.js is loaded.

I am loading this in .storybook/preview-head.html and it works for storybook.

But when I run a test (created as in https://storybook.js.org/addons/@storybook/testing-react), I'm getting Error: Uncaught [ReferenceError: M is not defined] which I'm pretty sure means that materialize.min.js is not loaded.

How do I load an external JS file in testing library?

1 Answers

I can achieve the above by appending the following to package.json:

  "scripts": {
    "test": "react-scripts test --setupFiles ./public/materialize.min.js",
  },

I'm not sure if this is the "best" way.

Related