From fec4b4616d516112e90fb34a62688745cef55ff2 Mon Sep 17 00:00:00 2001 From: Nandor Kraszlan Date: Thu, 11 Feb 2021 21:27:33 +0000 Subject: [PATCH] Fixed test typings --- src/createDomEvent.ts | 2 +- src/vueWrapper.ts | 6 +++--- tests/config.spec.ts | 10 +++++----- tests/emit.spec.ts | 12 ++++++------ tests/features/async-components.spec.ts | 6 +++--- tests/functionalComponents.spec.ts | 7 ++++--- tests/isVisible.spec.ts | 2 ++ tests/lifecycle.spec.ts | 2 +- tests/mountingOptions/attachTo.spec.ts | 4 ++-- tests/mountingOptions/data.spec.ts | 3 ++- tests/mountingOptions/global.plugins.spec.ts | 4 ++-- tests/mountingOptions/mocks.spec.ts | 7 +++++-- tests/mountingOptions/slots.spec.ts | 2 +- tests/mountingOptions/stubs.global.spec.ts | 2 +- tests/setData.spec.ts | 1 + tests/setProps.spec.ts | 1 + tests/trigger.spec.ts | 5 +++-- 17 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/createDomEvent.ts b/src/createDomEvent.ts index f4955f8c8..8b04bec2e 100644 --- a/src/createDomEvent.ts +++ b/src/createDomEvent.ts @@ -1,4 +1,4 @@ -// @ts-expect-error No DefinitelyTyped package exists +// @ts-ignore No DefinitelyTyped package exists import eventTypes from 'dom-event-types' interface TriggerOptions { diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 94d8c846e..23b3f91f4 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -61,20 +61,20 @@ export class VueWrapper { classes(): string[] classes(className: string): boolean classes(className?: string): string[] | boolean { - return new DOMWrapper(this.element).classes(className) + return new DOMWrapper(this.element).classes(className!) } attributes(): { [key: string]: string } attributes(key: string): string attributes(key?: string): { [key: string]: string } | string { - return new DOMWrapper(this.element).attributes(key) + return new DOMWrapper(this.element).attributes(key!) } exists() { return true } - emitted(): undefined | Record + emitted(): Record emitted(eventName?: string): undefined | T[] emitted( eventName?: string diff --git a/tests/config.spec.ts b/tests/config.spec.ts index 6338a3843..762816571 100644 --- a/tests/config.spec.ts +++ b/tests/config.spec.ts @@ -1,4 +1,4 @@ -import { h, inject } from 'vue' +import { ComponentPublicInstance, h, inject } from 'vue' import { config, mount } from '../src' import Hello from './components/Hello.vue' @@ -114,7 +114,7 @@ describe('config', () => { describe('provide', () => { const Comp = { setup() { - const theme = inject('theme') + const theme = inject('theme') return () => h('div', theme) } } @@ -163,14 +163,14 @@ describe('config', () => { expect(createdHook).toHaveBeenCalledTimes(2) }) - it('concats with locally defined mixins', () => { + it('concat with locally defined mixins', () => { config.global.mixins = [mixin] const localHook = jest.fn() const localMixin = { created() { - localHook(this.$options.name) + localHook(this.$options!.name) } - } + } as Partial mount(Component, { global: { diff --git a/tests/emit.spec.ts b/tests/emit.spec.ts index 19403ab12..893f9e625 100644 --- a/tests/emit.spec.ts +++ b/tests/emit.spec.ts @@ -93,7 +93,7 @@ describe('emitted', () => { name: 'Parent', setup(props, { emit }) { return () => - h(Child, { onHello: (...events) => emit('parent', ...events) }) + h(Child, { onHello: (...events: any[]) => emit('parent', ...events) }) } }) const wrapper = mount(Parent) @@ -130,10 +130,10 @@ describe('emitted', () => { expect(wrapper.emitted('hello')).toEqual(undefined) wrapper.find('button').trigger('click') - expect(wrapper.emitted('hello')[0]).toEqual(['foo', 'bar']) + expect((wrapper.emitted('hello') as unknown[])[0]).toEqual(['foo', 'bar']) wrapper.find('button').trigger('click') - expect(wrapper.emitted('hello')[1]).toEqual(['foo', 'bar']) + expect((wrapper.emitted('hello') as unknown[])[1]).toEqual(['foo', 'bar']) expect(wrapper.emitted('hello')).toHaveLength(2) }) @@ -157,7 +157,7 @@ describe('emitted', () => { wrapper.find('h1').trigger('click') expect(wrapper.emitted('hello')).toHaveLength(1) - expect(wrapper.emitted('hello')[0]).toEqual(['foo', 'bar']) + expect((wrapper.emitted('hello') as unknown[])[0]).toEqual(['foo', 'bar']) }) it('captures events emitted by class-style components', () => { @@ -175,12 +175,12 @@ describe('emitted', () => { wrapper.find('h1').trigger('click') expect(wrapper.emitted('hello')).toHaveLength(1) - expect(wrapper.emitted('hello')[0]).toEqual(['foo', 'bar']) + expect((wrapper.emitted('hello') as unknown[])[0]).toEqual(['foo', 'bar']) }) it('captures an event emitted in setup', () => { const Comp = { - setup(_, { emit }) { + setup(_: Record, { emit }: { emit: SetupContext['emit'] }) { emit('foo') } } diff --git a/tests/features/async-components.spec.ts b/tests/features/async-components.spec.ts index 4c99e3d11..010beab4d 100644 --- a/tests/features/async-components.spec.ts +++ b/tests/features/async-components.spec.ts @@ -1,17 +1,17 @@ import { defineAsyncComponent, defineComponent, h, AppConfig } from 'vue' import { mount, flushPromises } from '../../src' +import { ComponentPublicInstance } from '@vue/runtime-core' const config: AppConfig = { optionMergeStrategies: {}, globalProperties: {}, isCustomElement: (tag: string) => false, performance: false, - errorHandler: (error: Error) => { - if (error.message.match(/Async component failed to load./)) { + errorHandler: (error: unknown) => { + if ((error as Error).message.match(/Async component failed to load./)) { return } - throw error } } diff --git a/tests/functionalComponents.spec.ts b/tests/functionalComponents.spec.ts index b5bb6647d..2b1f6ea9f 100644 --- a/tests/functionalComponents.spec.ts +++ b/tests/functionalComponents.spec.ts @@ -1,5 +1,5 @@ import { mount } from '../src' -import { h } from 'vue' +import { h, Slots } from 'vue' import Hello from './components/Hello.vue' describe('functionalComponents', () => { @@ -17,7 +17,8 @@ describe('functionalComponents', () => { }) it('renders the slots of a functional component', () => { - const Foo = (props, { slots }) => h('div', { class: 'foo' }, slots) + const Foo = (props: Record, { slots }: { slots: Slots }) => + h('div', { class: 'foo' }, slots) const wrapper = mount(Foo, { slots: { @@ -29,7 +30,7 @@ describe('functionalComponents', () => { }) it('asserts classes', () => { - const Foo = (props, { slots }) => h('div', { class: 'foo' }, slots) + const Foo = () => h('div', { class: 'foo' }) const wrapper = mount(Foo, { attrs: { diff --git a/tests/isVisible.spec.ts b/tests/isVisible.spec.ts index bce409303..559ed68ba 100644 --- a/tests/isVisible.spec.ts +++ b/tests/isVisible.spec.ts @@ -93,9 +93,11 @@ describe('isVisible', () => { `, methods: { add() { + // @ts-expect-error this.items.push(2) }, remove() { + // @ts-expect-error this.items.splice(1) // back to [1] } }, diff --git a/tests/lifecycle.spec.ts b/tests/lifecycle.spec.ts index da1c5deaa..0a2debd72 100644 --- a/tests/lifecycle.spec.ts +++ b/tests/lifecycle.spec.ts @@ -55,7 +55,7 @@ describe('lifecycles', () => { expect(onUnmountFn).not.toHaveBeenCalled() const removeChildSpy = jest.spyOn( - wrapper.element.parentElement, + wrapper.element.parentElement!, 'removeChild' ) diff --git a/tests/mountingOptions/attachTo.spec.ts b/tests/mountingOptions/attachTo.spec.ts index 6bc40029c..3dc5ce33a 100644 --- a/tests/mountingOptions/attachTo.spec.ts +++ b/tests/mountingOptions/attachTo.spec.ts @@ -17,7 +17,7 @@ describe('options.attachTo', () => { }) const root = document.getElementById('root') - const rendered = document.getElementById('attach-to') + const rendered = document.getElementById('attach-to')! expect(wrapper.vm.$el.parentNode).not.toBeNull() expect(root).not.toBeNull() expect(rendered).not.toBeNull() @@ -36,7 +36,7 @@ describe('options.attachTo', () => { }) const root = document.getElementById('root') - const rendered = document.getElementById('attach-to') + const rendered = document.getElementById('attach-to')! expect(wrapper.vm.$el.parentNode).not.toBeNull() expect(root).not.toBeNull() expect(rendered).not.toBeNull() diff --git a/tests/mountingOptions/data.spec.ts b/tests/mountingOptions/data.spec.ts index 3e44210a2..0e54c3502 100644 --- a/tests/mountingOptions/data.spec.ts +++ b/tests/mountingOptions/data.spec.ts @@ -11,7 +11,8 @@ describe('mounting options: data', () => { bar: 'bar' } }, - render() { + render(): Function { + // @ts-expect-error return h('div', `Foo is ${this.foo} bar is ${this.bar}`) } } diff --git a/tests/mountingOptions/global.plugins.spec.ts b/tests/mountingOptions/global.plugins.spec.ts index ed47721c6..854ff5f20 100644 --- a/tests/mountingOptions/global.plugins.spec.ts +++ b/tests/mountingOptions/global.plugins.spec.ts @@ -30,7 +30,7 @@ describe('mounting options: plugins', () => { const installed = jest.fn() class Plugin { - static install(_app: App, ...options) { + static install(_app: App, ...options: any[]) { installed(...options) } } @@ -62,7 +62,7 @@ test('installs plugins with and without options', () => { const installedWithOptions = jest.fn() class PluginWithOptions { - static install(_app: App, ...args) { + static install(_app: App, ...args: any[]) { installedWithOptions(...args) } } diff --git a/tests/mountingOptions/mocks.spec.ts b/tests/mountingOptions/mocks.spec.ts index da47e89e6..be8c96fe3 100644 --- a/tests/mountingOptions/mocks.spec.ts +++ b/tests/mountingOptions/mocks.spec.ts @@ -37,15 +37,18 @@ describe('mocks', () => { `, computed: { - url() { + url(): string { + // @ts-expect-error return `/posts/${this.$route.params.id}` }, - id() { + id(): number { + // @ts-expect-error return this.$route.params.id } }, methods: { submit() { + // @ts-expect-error this.$router.push(`/posts/${this.id}`) } } diff --git a/tests/mountingOptions/slots.spec.ts b/tests/mountingOptions/slots.spec.ts index 4e773a90d..3d76bb78c 100644 --- a/tests/mountingOptions/slots.spec.ts +++ b/tests/mountingOptions/slots.spec.ts @@ -15,7 +15,7 @@ describe('slots', () => { named: namedString } }) - expect(wrapper.vm.$slots.default()[0].children).toBe(defaultString) + expect(wrapper.vm.$slots.default!()[0].children).toBe(defaultString) expect(wrapper.find('.default').text()).toBe(defaultString) expect(wrapper.find('.named').text()).toBe(namedString) }) diff --git a/tests/mountingOptions/stubs.global.spec.ts b/tests/mountingOptions/stubs.global.spec.ts index 6acfefbb5..0597f05ab 100644 --- a/tests/mountingOptions/stubs.global.spec.ts +++ b/tests/mountingOptions/stubs.global.spec.ts @@ -63,7 +63,7 @@ describe('mounting options: stubs', () => { }) it('stubs a functional component by its variable declaration name', () => { - const FunctionalFoo = (props) => h('p', props, 'Foo Text') + const FunctionalFoo = (props: any) => h('p', props, 'Foo Text') const Component = { template: '
', diff --git a/tests/setData.spec.ts b/tests/setData.spec.ts index b74b894a9..ee5dca858 100644 --- a/tests/setData.spec.ts +++ b/tests/setData.spec.ts @@ -34,6 +34,7 @@ describe('setData', () => { myObject: { immediate: true, handler() { + // @ts-expect-error this.watchCounter += 1 } } diff --git a/tests/setProps.spec.ts b/tests/setProps.spec.ts index cc6241cdf..b7d54faf2 100644 --- a/tests/setProps.spec.ts +++ b/tests/setProps.spec.ts @@ -65,6 +65,7 @@ describe('setProps', () => { }, watch: { foo(val: string) { + // @ts-expect-error this.bar = val } }, diff --git a/tests/trigger.spec.ts b/tests/trigger.spec.ts index 7761edddd..4a3a0de12 100644 --- a/tests/trigger.spec.ts +++ b/tests/trigger.spec.ts @@ -1,7 +1,7 @@ import { defineComponent, h, ref } from 'vue' import { mount } from '../src' -import { keyCodesByKeyName } from '../src/createDomEvent' +import { keyCodesByKeyName, KeyNameArray } from '../src/createDomEvent' describe('trigger', () => { describe('on click', () => { @@ -233,7 +233,8 @@ describe('trigger', () => { const wrapper = mount(Component, {}) for (const keyName in keyCodesByKeyName) { - const keyCode = keyCodesByKeyName[keyName] + const keyCode = + keyCodesByKeyName[keyName as keyof typeof keyCodesByKeyName] wrapper.trigger(`keydown.${keyName}`) const calls = keydownHandler.mock.calls