Search code examples
javascriptnode.jstestingrequirejsfaker.js

TypeError: Cannot read property 'uuid' of undefined faker-js faker


I am trying to use faker-js package, but I unexpectedly get a TypeError: Cannot read property 'uuid' of undefined for my variable businessId which try to use faker.datatype.uuid()

It usually happen when I forget to install faker-js, but that's not the case here, and I check that I imported this package. Therefore I am clueless on what I am doing wrong here.

// eslint-disable-next-line import/order
const { initConfig } = require('../../../config');

initConfig();

const sinon = require('sinon');
const faker = require('@faker-js/faker');
const { retryAsyncCall } = require('../../../src/common/helpers/retry-async');
const { createFacebookAdVideoFromUrl } = require('../../../src/common/controllers/facebook/api');

function createPayloadDataBuilder(payload = {}) {
  const template = {
    accountId: faker.datatype.uuid(),
    publicLink: faker.internet.url(),
    videoName: faker.lorem.word(),
    facebookToken: undefined,
    params: null,
    businessId: faker.datatype.uuid(),
  };
  return { ...payload, ...template };
}

describe('Facebook Gateway', () => {
  describe('createFacebookAdVideoFromUrl', () => {
    describe('Given businessId', () => {
      it.only("should create the video calling business's Facebook ids", async () => {
        const businessId = faker.datatype.uuid();
        console.log(faker, businessId);
        const createFacebookAdVideoPayload = createPayloadDataBuilder({
          businessId,
        });

        await createFacebookAdVideoFromUrl(...createFacebookAdVideoPayload);

        // sinon.assert.called(retryAsyncCall);
      });
    });
  });
});

Solution

  • Found it! I need to destructure the import:

    const { faker } = require('@faker-js/faker');