1

This answer has the following way to download a file using the node-fetch library https://stackoverflow.com/a/51302466:

const downloadFile = (async (url, path) => {
  const res = await fetch(url);
  const fileStream = fs.createWriteStream(path);
  await new Promise((resolve, reject) => {
      res.body.pipe(fileStream);
      res.body.on("error", reject);
      fileStream.on("finish", resolve);
    });
});

How to test this? I'm running into weird problems:

unit.test.js

import { PassThrough } from 'stream';
import { createWriteStream, WriteStream } from 'fs';
import fetch, { Response } from 'node-fetch';

import mocked = jest.mocked;


jest.mock('node-fetch');
jest.mock('fs');

describe('downloadfile', () => {
  const mockedFetch = fetch as jest.MockedFunction<typeof fetch>;
  const response = Promise.resolve({
    ok: true,
    status: 200,
    body: {
      pipe: jest.fn(),
      on: jest.fn(),
    },
  });

  const mockWriteable = new PassThrough();
  mocked(createWriteStream).mockReturnValueOnce(mockWriteable as unknown as WriteStream);

  mockedFetch.mockImplementation(() => response as unknown as Promise<Response>);
  it('should work', async () => {
      await downloadfile();
    }),
  );
});

Throws:

Cannot read property 'length' of undefined
TypeError: Cannot read property 'length' of undefined
    at GlobSync.Object.<anonymous>.GlobSync._readdirEntries (//node_modules/glob/sync.js:300:33)
    at GlobSync.Object.<anonymous>.GlobSync._readdir (//node_modules/glob/sync.js:288:17)
    at GlobSync.Object.<anonymous>.GlobSync._processReaddir (//node_modules/glob/sync.js:137:22)
    at GlobSync.Object.<anonymous>.GlobSync._process (/api/node_modules/glob/sync.js:132:10)

What could the solutions be?

user3761308
  • 662
  • 2
  • 14
  • 27
  • 1
    Can you please share also the configuration that you have for jest? – hpfs May 25 '22 at 21:23
  • There isn't really any jest specific config except the default typescript one. – user3761308 May 26 '22 at 15:52
  • 2
    Please provide a minimum reproducible example of your issue. In your code "createWriteStream" and "mockWriteable" aren't defined. – Kyle May 26 '22 at 21:15
  • Maybe i'm wrong, but your function downloadFile awaits the response, but it's not returning the promise that should be awaited... ¿that could be the problem? – Diego N. May 30 '22 at 19:50

1 Answers1

1

I see that the error is not related to tests. It is more about your test runner. Glob is a library to search files in your project. Check your test run command. Try to escape quotes \ double quotes.

Your package JSON will give more information that your test files.

There are some people that report this problem can be fixed by reinstalling node modules Try to

  1. Remove node_modules folder
  2. Remove the file package-lock.json
  3. Run npm install to re-install package dependencies.
zamuka
  • 665
  • 1
  • 7
  • 19
  • I dont think so because changing the test code changes the error, if it was about the test runner it would always throw the glob error. – user3761308 Jun 03 '22 at 22:34