1

I'm currently writing simple unit tests (using Jest) for my component dateFormat.js that contains the function formatDateGlobal. Here's a snippet of the test:

import DateFormat from '../dateFormat';

describe('dateFormat.js', () => {
  let date1;

  beforeEach(() => {
    date1 = {
      date: '',
    };
  });

  it('Then it should return an empty string', () => {
    // Act
    const returnedDate = DateFormat.formatDateGlobal(date1);
    // Assert
    expect(returnedDate).toBe('');
  });

At the bottom of dateFormat, I'm exporting the formatDateGlobal function as so:

export default formatDateGlobal;

The tests are built as expected, but I'm getting the error

TypeError: _dateFormat.default.formatDateGlobal is not a function

      27 |   it('Then it should return an empty string', () => {
      28 |     // Act
    > 29 |     const returnedDate = DateFormat.formatDateGlobal(date1);
         |                                     ^
      30 |     // Assert
      31 |     expect(returnedDate).toBe('');
      32 |   });

Not really sure why this is happening, but I'm thinking it has to do with the way I'm exporting the function.

1 Answer 1

1

You are importing formatDateGlobal into DateFormat variable. So you can use const returnedDate = DateFormat(date1);

currently you're trying to achieve equivalent of this: const returnedDate = formatDateGlobal.formatDateGlobal(date1);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.