1

I'm using the following test to pass a value of 2 inside my vue component

const wrapper = factory({ value: 2 })
const input = wrapper.find('input[type=number]')
expect(input.element.value).toBe(2)

But the received is always a string, even though I'm passing it as an integer in the form input field This is the error message:

expect(received).toBe(expected) // Object.is equality

Expected: 2
Received: "2"

This is my vue code, which basically just sets the value. I have to allow String and Number, since my database can return either, hence the dynamic prop allowance.

<template>
  <div class="form-group row">
      <input
        :value="value"
        :min="min"
        :max="max"
        class="form-control"
        type="number"
        @input="$emit('input', $event.target.value)"
      />
</template>

<script>

export default {

  props: {
    value: {
      type: [String, Number],
      default: ''
    },
  }
}
</script>

skyboyer
  • 22,209
  • 7
  • 57
  • 64
Miguel Stevens
  • 8,631
  • 18
  • 66
  • 125
  • 1
    That's to be expected, see e.g. https://stackoverflow.com/q/35791767/3001761 – jonrsharpe Apr 24 '20 at 13:17
  • More, since you're accepting both `String` and `Number` for the test you could wrap the check functionality in a function e.g. `function checkNumberEquality(a, b) { /* Here you don't need to check for the type too */ and return a == b}` and then `expect(checkForEquality('2', 2)).toBe(true)` – Nico Vignola Apr 24 '20 at 13:24

0 Answers0