Skip to content

Broken setting of Date-Objects by setData #1717

@cokuna-pavelkosolapov

Description

@cokuna-pavelkosolapov

Subject of the issue

If you try to set a Date to any data-variable it becomes an object.

Steps to reproduce

package.json

"devDependencies": {
    "@babel/core": "^7.18.10",
    "@babel/preset-env": "^7.18.10",
    "@vue/cli-plugin-babel": "^5.0.8",
    "@vue/cli-plugin-eslint": "~5.0.8",
    "@vue/cli-service": "~5.0.8",
    "@vue/test-utils": "^2.0.2",
    "@vue/vue3-jest": "^28.0.1",
    "babel-core": "^7.0.0-bridge.0",
    "babel-jest": "^28.1.3",
    "jest-environment-jsdom": "^28.1.3",
    "jest-transform-stub": "^2.0.0"
  },

test.spec.js

import {shallowMount} from '@vue/test-utils'

describe('App', () => {
   
    const DateInput = {
        template: `<div><input type="text" :value="value" @input="emit" /></div>`,
        props: { modelValue: Date },
        computed: {
            value() {
                return '' + this.modelValue + ''
            }
        },
        methods: {
            emit(value) {
                this.$emit('update:modelValue', new Date(value))
            }
        }
    }
  
    const App = {
        template: ` <div><DateInput ref="input" v-model="modelValue" /></div> `, 
        components: { DateInput },
        props: {modelValue: Date},
        data() { return { defaultValue: this.modelValue}}
    }    

    it('set Date to data', async () => {
        const wrapper = shallowMount(App, {
            props: {
                modelValue: new Date('2022-08-10T12:15:54Z'),
            },
            global: {
                stubs: {
                    DateInput
                }
            }
        })
        expect(wrapper.vm.defaultValue + '').toBe((new Date('2022-08-10T12:15:54Z').toString()))

        await wrapper.setData({defaultValue: new Date('2022-09-10T12:15:54Z')})

        expect(wrapper.vm.defaultValue+ '').toBe((new Date('2022-09-10T12:15:54Z').toString()))
    })
})

Expected behaviour

Test "set Date to data" should be successful.

Actual behaviour

The test fails:

    Expected: "Sat Sep 10 2022 14:15:54 GMT+0200 (Central European Summer Time)"
    Received: "[object Object]"

Possible Solution

The Date should stay a Date!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions