0

I'm learning Jest. I am trying to test that when I create a class and the passed parameter isn't a string to throw an Error.

// class.js
export default class FooClass {
    constructor(value) {
        if (typeof value !== 'string') throw new Error('Value should be a String.')
        this.value = value
    }
}

// class.test.js
import FooClass from './class'

it('should throw an Error if the value is not a String', () => {
    expect(new FooClass(123)).toThrowError('Value should be a String.')
})

Jest can pass this test, just returning Test failed.

2
  • Welcome to StackOverflow. Could you make this a bit clearer by editing the question please. The community needs to understand a) what you expect to see, b) what you are seeing in practice, and c) any error messages you are getting. Thanks. Commented Aug 4, 2018 at 17:30
  • You haven't actually described what the issue with your code is. Commented Aug 4, 2018 at 17:56

1 Answer 1

0

The problem was, that I called a Class instead of a Function.

// class.js
export default class FooClass {
    constructor(value) {
        if (typeof value !== 'string') throw new Error('Value should be a String.')
        this.value = value
    }
}

// class.test.js
import FooClass from './class'

it('should throw an Error if the value is not a String', () => {
    expect(() => new FooClass(123)).toThrowError('Value should be a String.')
})

Now it's working. Thank you.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.