Problems Unit Testing in SharePoint Framework (SPFx) with External Dependencies
-
12-01-2021 - |
Question
I'm attempting to use Elio and Velin's guidance on creating unit tests for the SharePoint framework. Both appear to work great for vanilla components, but I'm running into problems when trying to incorporate an external dependency.
Here is the error I get when trying to run a HelloWorld-type test on my SPFx web part.
This is the dependency it's complaining about, located in my config.json file.
Here is the relevant piece of my package.json. (It did not format well when I tried to post it all as code.)
And here are some npm warnings I'm getting in the console. (I think they might be related to my problem.)
Full disclosure: I'm a SharePoint dev learning the SPFx, and newbie to webpack, npm, and nodejs. From Googling, I found the following GitHub issues which indicate the problem might be webpack related?
- https://github.com/webpack-contrib/karma-webpack/issues/125
- https://github.com/airbnb/enzyme/issues/47
Thanks! :-)
Solution
you can add the jsChart to the devDependencies in package.json and also keep it in the external dependency configuration. Doing that the tests will run and in the same time the jsChart would not be bundled with the packages, but the packages will use your external dependency. Hope that clears your ask.