0

I'm trying to test errors throwing.

Here's my code

const validatorMethod = (data) => {
  const validationResult = Object.keys(data)
    .map((key) => {
      if (!data[key] || data[key].trim() === '') {
        return key;
      }
      return true;
    });
  if (validationResult.filter((prop) => prop === true).length !== data.length) {
    return validationResult.filter((prop) => prop !== true);
  }
  return true;
};

module.exports = {
  userObjectFactory (data) {
    console.log(data);
    const invalidKeys = validatorMethod(data);
    if (invalidKeys.length === true) {
      console.log(1);
      return data;
    }
    console.log(2);
    throw new Error('One of passed properties is empty');
  },
};

Here is my test

const userTemplate = {
  id: 1,
  email: 'a@a.a',
  password: 'zaq1@WSX',
  fullName: 'full name',
  location: 'location',
  isLookingForWork: false,
};
describe('factory should throw error on undefined, null, or ""', () => {
  it('should throw an error if some inputs are undefined', () => {
    const userWithUndefinedProperty = userTemplate;
    userWithUndefinedProperty.id = undefined;
    userWithUndefinedProperty.password = undefined;

    assert.throws(
      userObjectFactory(
        userWithUndefinedProperty, new Error('One of passed properties is empty'), // also tried "Error" and "Error('One of passed properties is empty')" without the "new"
      ),
    );
  });
});

the output

  0 passing (68ms)
  2 failing

  1) testing UserObjectFactory
       should return an object with correct data:
     Error: One of passed properties is empty
      at userObjectFactory (src\user\UserObjectFactory.js:2:1646)
      at Context.it (test\user\UserObjectFactory.test.js:33:18)

  2) testing UserObjectFactory
       factory should throw error on undefined, null, or ""
         should throw an error if some inputs are undefined:
     Error: One of passed properties is empty
      at userObjectFactory (src\user\UserObjectFactory.js:2:1646)
      at Context.it (test\user\UserObjectFactory.test.js:26:9)
Alex Ironside
  • 4,658
  • 11
  • 59
  • 119
  • With what outcome? A few things: 1. you typically need to pass a callable to methods that check errors; and 2. you're passing the error as the second argument to the factory, not `.throws`. – jonrsharpe Jun 07 '20 at 15:57
  • 1. What do you mean? 2. You're right, thank you, but correcting this one didn't solve the problem – Alex Ironside Jun 07 '20 at 16:37
  • Read the docs: https://nodejs.org/api/assert.html#assert_assert_throws_fn_error_message. You need to pass something that should throw an error *when called*, deferring execution, otherwise the error is thrown *before* calling `assert.throws`. Again, what's the *outcome* of what you've posted? – jonrsharpe Jun 07 '20 at 16:40
  • @jonrsharpe I found numerous small errors in my code, and am fixing them, but it still throws the error so it should assert it as true. I'm adding the output – Alex Ironside Jun 07 '20 at 16:51
  • @jonrsharpe how can I pass it without calling it though, if I need to pass values into the method? – Alex Ironside Jun 07 '20 at 16:56
  • Pass a no-args function that calls it, `() => userObjectFactory(...)` – jonrsharpe Jun 07 '20 at 16:56
  • @jonrsharpe Thank you! Passing it as `() => userObjectFactory(...)` worked. Would you like to add it as answer? – Alex Ironside Jun 07 '20 at 17:15
  • It's a dupe, see e.g. https://stackoverflow.com/questions/6645559/node-assert-throws-not-catching-exception. – jonrsharpe Jun 07 '20 at 17:17

1 Answers1

1

You should pass to assert.throws a reference to a function that when called throws an error

e.g.

const thisIsAFunctionThatIsSupposedToThrowWhenCalled = () =>
  userObjectFactory(
    userWithUndefinedProperty,
    new Error("One of passed properties is empty") // also tried "Error" and "Error('One of passed properties is empty')" without the "new"
  );

assert.throws(thisIsAFunctionThatIsSupposedToThrowWhenCalled);

or

assert.throws(
  () => userObjectFactory(
    userWithUndefinedProperty, new Error('One of passed properties is empty'), // also tried "Error" and "Error('One of passed properties is empty')" without the "new"
  )
)
Teneff
  • 30,564
  • 13
  • 72
  • 103