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.38k stars 510 forks source link

[QUESTION] How does one mock zip.js in Jest tests? #421

Closed remarkablemark closed 1 year ago

remarkablemark commented 1 year ago

I'm using zip.js in my frontend project and I'm trying to write Jest tests but they're failing with:

    ReferenceError: TransformStream is not defined

    > 1 | import { BlobReader, BlobWriter, ZipWriter } from '@zip.js/zip.js';
        | ^

I'm using jsdom for my test environment.

Is the best way to resolve this is to mock the node module? I have seen other projects provide a mock file for Jest.

gildas-lormeau commented 1 year ago

If the web steam API is not available, then you should able to circumvent this issue by importing this module before running the tests: https://www.npmjs.com/package/web-streams-polyfill

remarkablemark commented 1 year ago

Thanks! I ended up creating a module mock for the package:

mkdir -p __mocks__/@zip.js/
echo "module.exports = {};" > __mocks__/@zip.js/zip.js.js