Note: This answer only applies if you want to follow the Richie Bendall's answer.
Short answer:
Avoid importing Blob
from 'node:buffer'
.
Instead, prefer importing it like this (as explained in the NodeJS docs. (implementation example here)):
import { Blob } from 'buffer';
Long answer:
The Richie Bendall's answer helped me a lot. But it seems that importing Blob
from node:buffer
breaks the Jest unit tests, throwing this error:
FAIL dist/tests/unit/users/getOneById.spec.js
● Test suite failed to run
ENOENT: no such file or directory, open 'node:buffer'
2 | import config from '../config/config';
3 | import { getFileExt, getFileName, removeFile } from './file';
> 4 | import { Blob } from 'node:buffer';
| ^
5 |
6 | class PdfHelpers {
7 |
at Runtime.readFile (node_modules/jest-runtime/build/index.js:2118:21)
at Object.<anonymous> (src/helpers/pdf.ts:4:1)
Instead of trying to mock the node:buffer
import with some weird/tricky code, I took a look at the NodeJS documentation examples. So it demonstrates that Blob
can be imported from 'buffer'
import { Blob } from 'buffer';
// ...
// Only the import is changing, don't change your existing implementation
const blob = new Blob([buf], { type: 'application/pdf' });
And all Jest errors have been gone away!
Buffer
.Blob
isn't available because it's defined by a DOM API and Node is not a DOM implementation. – Chessboardmime
property to theBuffer
since it's still dynamic. But, MIME types are for communicating binary data; not so much for saving to disk. – Chessboardvar bufferL = mergeBuffers(recBuffersL, recLength); var bufferR = mergeBuffers(recBuffersR, recLength); var interleaved = interleave(bufferL, bufferR); var dataview = encodeWAV(interleaved); //var audioBlob = new Blob([dataview], { type: 'audio/wav' });
Here is the example. Your suggestion is to create a buffer and then usefs.writeFile('test.wav', <buffer variable>, function(err){ if (err) console.log(err); });
? – Butcher