gildas-lormeau / zip.js

JavaScript library to zip and unzip files supporting multi-core compression, compression streams, zip64, split files and encryption.
https://gildas-lormeau.github.io/zip.js
BSD 3-Clause "New" or "Revised" License
3.42k stars 512 forks source link

Unit Testing: Blob is not defined #310

Closed smiles3983 closed 2 years ago

smiles3983 commented 2 years ago

trying to unit test when I use the BlobReader. I know Blob is a Web-API, and nodejs doesnt have access to it. Ive been able to stub it within my JSX file, but when Zip.js runs, it doesnt appear to have access to my stub. Any ideas how to unit test this?

ReferenceError: Blob is not defined at t (file:///c:/ws/Insight/wordaddin/node_modules/@zip.js/zip.js/lib/z-worker-inline.js:1:86) at file:///c:/ws/Insight/wordaddin/node_modules/@zip.js/zip.js/lib/zip-fs.js:44:1 at ModuleJob.run (node:internal/modules/esm/module_job:185:25) at async Promise.all (index 0) at ESMLoader.import (node:internal/modules/esm/loader:281:24) at importModuleDynamicallyWrapper (node:internal/vm/module:437:15)

smiles3983 commented 2 years ago

this is the line im executing: const reader = new zip.ZipReader(new zip.BlobReader(blob));

gildas-lormeau commented 2 years ago

Did you import zip.js asynchonously and inject Blob in globalThis? Here is an example showing how to use a Blob object in Node: https://github.com/gildas-lormeau/zip.js/tree/master/tests/node.

smiles3983 commented 2 years ago

I was able to make it work with blob-polyfill. And then I also had to use jsdom to replace FileReader from window.FileReader

gildas-lormeau commented 2 years ago

That's interesting, I was not expecting that you would need a FileReader implementation. What code is forcing you to polyfill it?

gildas-lormeau commented 2 years ago

I'm meaning that if FileReader is not needed by zip.js (this is what I expect) but by your code, then you could maybe use Blob#text or Blob#arrayBuffer instead.

gildas-lormeau commented 2 years ago

Note that you don't need to import zip.js asynchronously in Node.js with the version 2.4.5.

gildas-lormeau commented 2 years ago

I'm moving this issue to the Discussion tab