wavesurfer.js: Jest error: ReferenceError: self is not defined
In the latest version of the library, we get the following error when using jest:
ReferenceError: self is not defined
4 | import { findWithType } from 'react-shallow-testutils';
5 | import ShallowRenderer from 'react-test-renderer/shallow';
> 6 | import WaveSurfer from 'wavesurfer.js';
| ^
This was caused by a change removing globalObject: 'this' from the build config in https://github.com/katspaugh/wavesurfer.js/commit/d27b75e29b16e1b0d425eefa83bd0b0e1891f067.
There is a workaround by setting testEnvironment: 'jsdom' in the jest config, but this causes issues for things in the node environment.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 16 (2 by maintainers)
Did not really work.
Anyone who is looking for a Next.js solution, use the dynamic loader to load your component that contains the wavesurfer import:
Same here with Vue/Nuxt
Thanks f-elix for this great tip. Your example works for the basic player but when I tried addding wavesurfer plugins, it doesn’t work. Have you got plugins working? Cheers.
This is brilliant, thank you so much! Worked for me in Next.js
I found the mistake error
FWIW if you’re just looking to import wavesurfer in a node context (but not run it), you can just shim
windowandself:This is useful for SSR react components that execute
WaveSurfer.createin auseEffecthook.