PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

Getting errors when used with es modules in karma -- "root" is undefined

Open donecarlo opened this issue 5 years ago • 1 comments

Currently using web-components (LitElement, open-wc) and I have one component using Papaparse. When running tests via karma, it defaults to the "Browser globals" scenario, though in this case, "root" is undefined. Do you have any suggestions on what can be a possible workaround for this?

The karma config is Using @open-wc/testing-karma as the base config, which uses the @open-wc/karma-esm plugin.

donecarlo avatar Jul 30 '20 00:07 donecarlo

Same problem,

just importing it as a module will result in an error: Uncaught TypeError: root is undefined

Reproducible:

<!DOCTYPE html>
<head>
    <script type="module" src="./lib/papaparse.js"></script>
</head>
</html>

Or

<!DOCTYPE html>
<head>
    <script type="module">
        import * as papaparse  from "./lib/papaparse.js"
    </script>
</head>
</html>

EDIT: fixed the problem for now by adding export default and root=window to the first line:

/* @license
Papa Parse
v5.4.1
https://github.com/mholt/PapaParse
License: MIT
*/

export default (function(root=window, factory)
{
	/* globals define */
	if (typeof define === 'function' && define.amd)
	{
		// AMD. Register as an anonymous module.
		define([], factory);
	}
	else if (typeof module === 'object' && typeof exports !== 'undefined')
	{
		// Node. Does not work with strict CommonJS, but
		// only CommonJS-like environments that support module.exports,
		// like Node.
		module.exports = factory();
	}
	else
	{
		// Browser globals (root is window)
		root.Papa = factory();
	}
	// in strict mode we cannot access arguments.callee, so we need a named reference to
	// stringify the factory method for the blob worker
	// eslint-disable-next-line func-name
}
...

MrMiracles avatar May 18 '23 10:05 MrMiracles