I'm trying to write integration tests with Mocha and Chai but Chai doesn't seem to catch the errors.
Code being tested:
export async function createUser(
username: string,
password: string,
): Promise < {} > {
const user = await User.findOne({
username
})
if (user) {
throw new UserInputError('Username is taken', {
errors: {
username: 'Username is taken',
},
})
}
if (username.trim() === '' || null) {
throw new UserInputError('Must not be empty', {
errors: {
username: 'Must not be empty',
},
})
}
const hash = crypto.createHash('sha512').update(password).digest('hex')
const newUser = new User({
username,
password: hash,
})
return newUser.save()
}
And the test code :
it('Fails when no username is provided', () => {
const password = uuid()
expect(async() => {
await client.mutate({
mutation: gql `
mutation($username: String!, $password: String!){
createUser(username: $username, password: $password) {
id
username
}
}
`,
variables: {
username: '',
password,
},
})
}).to.throw()
})
I expect the test to pass, but the code I have fails with the following error message:
AssertionError: expected [Function] to throw an error