Код: Выделить всё
const userSchema = mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
minLength: 3
},
name: String,
passwordHash: String
})
const User = mongoose.model('User', userSchema)
Код: Выделить всё
minLength: [3, 'username must be at least 3-character long']
Код: Выделить всё
test('a user with too short username cannot be added into database', async () => {
try {
const user = new User({
username: 'x'
})
await user.save()
} catch (error) {
expect(error.username.message).toBe('username must be at least 3-character long')
}
})
Код: Выделить всё
const ERRORS = {
USER: {
USERNAME: {
USERNAME_TOO_SHORT: {
code: 'USERNAME_TOO_SHORT',
message: 'Username is too short.'
}
}
}
}
Код: Выделить всё
expect(error.username.code).toBe(ERRORS.USER.USERNAME.USERNAME_TOO_SHORT.code)
Код: Выделить всё
minLength: [3, ERRORS.USER.USERNAME.USERNAME_TOO_SHORT]
Я ищу способ протестировать ошибки проверки mongoose на основе кода ошибки, а не сообщения. Как упоминалось выше, mongoose не допускает объекты ошибок для пути схемы, он ожидает только строки.
Подробнее здесь: https://stackoverflow.com/questions/798 ... hema-paths
Мобильная версия