Skip to content

Commit 31f66b9

Browse files
authored
test: migrate to vitest (#1637)
1 parent 6bab48a commit 31f66b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1268
-2782
lines changed

.vscode/launch.json

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,17 @@
1-
{
2-
// Use IntelliSense to learn about possible attributes.
3-
// Hover to view descriptions of existing attributes.
4-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5-
"version": "0.2.0",
6-
"configurations": [
7-
{
8-
"name": "Jest",
9-
"type": "node",
10-
"request": "launch",
11-
"program": "${workspaceFolder}/node_modules/.bin/jest",
12-
"stopOnEntry": false,
13-
"args": ["${fileBasename}", "--runInBand", "--detectOpenHandles"],
14-
"cwd": "${workspaceFolder}",
15-
"preLaunchTask": null,
16-
"runtimeExecutable": null,
17-
"runtimeArgs": ["--nolazy"],
18-
"env": {
19-
"NODE_ENV": "development"
20-
},
21-
"console": "integratedTerminal",
22-
"sourceMaps": true,
23-
"windows": {
24-
"program": "${workspaceFolder}/node_modules/jest/bin/jest",
25-
}
26-
}
27-
]
28-
}
1+
{
2+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
3+
"version": "0.2.0",
4+
"configurations": [
5+
{
6+
"name": "vitest",
7+
"type": "node",
8+
"request": "launch",
9+
"autoAttachChildProcesses": true,
10+
"skipFiles": ["<node_internals>/**", "**/node_modules/**"],
11+
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
12+
"args": ["run", "${relativeFile}"],
13+
"smartStep": true,
14+
"console": "integratedTerminal"
15+
}
16+
]
17+
}

jest.config.js

-20
This file was deleted.

package.json

+8-9
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,21 @@
2929
"@rollup/plugin-json": "^4.1.0",
3030
"@rollup/plugin-node-resolve": "^13.3.0",
3131
"@rollup/plugin-replace": "^4.0.0",
32-
"@types/jest": "27.5.0",
3332
"@types/node": "18.0.0",
3433
"@types/pretty": "^2.0.1",
3534
"@typescript-eslint/eslint-plugin": "^5.30.3",
3635
"@typescript-eslint/parser": "^5.30.3",
36+
"@vitejs/plugin-vue": "^2.3.3",
37+
"@vitejs/plugin-vue-jsx": "^1.3.10",
3738
"@vue/babel-plugin-jsx": "^1.1.1",
3839
"@vue/compat": "3.2.37",
3940
"@vue/compiler-dom": "3.2.37",
4041
"@vue/compiler-sfc": "3.2.37",
41-
"@vue/vue3-jest": "27.0.0-alpha.4",
42-
"babel-jest": "27.5.1",
43-
"babel-preset-jest": "28.1.1",
42+
"c8": "^7.11.3",
4443
"eslint": "^8.18.0",
4544
"eslint-config-prettier": "^8.5.0",
4645
"eslint-plugin-prettier": "4.2.1",
4746
"husky": "^8.0.1",
48-
"jest": "27.5.1",
4947
"jsdom": "^20.0.0",
5048
"jsdom-global": "^3.0.2",
5149
"lint-staged": "^13.0.3",
@@ -54,13 +52,12 @@
5452
"reflect-metadata": "^0.1.13",
5553
"rollup": "^2.75.7",
5654
"rollup-plugin-typescript2": "^0.32.1",
57-
"ts-jest": "27.1.5",
5855
"tslib": "2.4.0",
5956
"typescript": "4.7.4",
6057
"vitepress": "^0.22.4",
58+
"vitest": "^0.16.0",
6159
"vue": "3.2.37",
6260
"vue-class-component": "^8.0.0-rc.1",
63-
"vue-jest": "^5.0.0-alpha.10",
6461
"vue-router": "^4.0.16",
6562
"vue-tsc": "0.38.2",
6663
"vuex": "^4.0.2"
@@ -73,8 +70,10 @@
7370
"email": "[email protected]"
7471
},
7572
"scripts": {
76-
"test": "yarn jest --runInBand tests/",
77-
"test:build": "yarn jest --runInBand tests/ -use-build",
73+
"test": "yarn vitest",
74+
"test:coverage": "yarn vitest --coverage",
75+
"test:watch": "yarn vitest --watch",
76+
"test:build": "yarn vitest --mode test-build",
7877
"tsd": "tsc -p test-dts/tsconfig.tsd.json",
7978
"build": "yarn rollup -c rollup.config.js",
8079
"lint": "eslint --ext .ts src/ tests/",

setup.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { vi } from 'vitest'
2+
13
const originalConsole = console.info
24

35
console.info = (...args) => {
@@ -12,6 +14,10 @@ console.info = (...args) => {
1214
originalConsole(...args)
1315
}
1416

15-
if (__USE_BUILD__) {
16-
jest.mock('./src', () => jest.requireActual('./dist/vue-test-utils.cjs'))
17-
}
17+
vi.mock('./src', () => {
18+
if (!__USE_BUILD__) {
19+
return vi.importActual('./dist/vue-test-utils.esm-bundler.mjs')
20+
} else {
21+
return vi.importActual('./src')
22+
}
23+
})

src/utils/getRootNodes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ShapeFlags } from '@vue/shared'
21
import { isNotNullOrUndefined } from '../utils'
32
import { VNode, VNodeArrayChildren } from 'vue'
3+
import { ShapeFlags } from './vueShared'
44

55
export function getRootNodes(vnode: VNode): Node[] {
66
if (vnode.shapeFlag & ShapeFlags.ELEMENT) {

src/utils/vueShared.ts

+14
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,17 @@ const hyphenateRE = /\B([A-Z])/g
1919
export const hyphenate = cacheStringFunction((str: string): string => {
2020
return str.replace(hyphenateRE, '-$1').toLowerCase()
2121
})
22+
23+
export const enum ShapeFlags {
24+
ELEMENT = 1,
25+
FUNCTIONAL_COMPONENT = 2,
26+
STATEFUL_COMPONENT = 4,
27+
TEXT_CHILDREN = 8,
28+
ARRAY_CHILDREN = 16,
29+
SLOTS_CHILDREN = 32,
30+
TELEPORT = 64,
31+
SUSPENSE = 128,
32+
COMPONENT_SHOULD_KEEP_ALIVE = 256,
33+
COMPONENT_KEPT_ALIVE = 512,
34+
COMPONENT = 6
35+
}

src/vueWrapper.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
ComponentCustomProperties,
55
ComponentPublicInstance
66
} from 'vue'
7-
import { ShapeFlags } from '@vue/shared'
87
// @ts-ignore todo - No DefinitelyTyped package exists for this
98
import pretty from 'pretty'
109

@@ -22,6 +21,7 @@ import {
2221
WrapperType
2322
} from './wrapperFactory'
2423
import { VNode } from '@vue/runtime-core'
24+
import { ShapeFlags } from './utils/vueShared'
2525

2626
export class VueWrapper<
2727
T extends Omit<

tests/RouterLinkStub.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from 'vitest'
12
import { defineComponent, h } from 'vue'
23
import { mount } from '../src'
34
import { RouterLinkStub } from '../src/components/RouterLinkStub'

tests/__snapshots__/shallowMount.spec.ts.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
1+
// Vitest Snapshot v1
22

3-
exports[`shallowMount renders props for stubbed component in a snapshot 1`] = `
3+
exports[`shallowMount > renders props for stubbed component in a snapshot 1`] = `
44
<div>
55
<my-label-stub val="username"></my-label-stub>
66
<async-component-stub></async-component-stub>

tests/attributes.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from 'vitest'
12
import { h } from 'vue'
23
import { mount } from '../src'
34
import Hello from './components/Hello.vue'

tests/autoUnmount.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
12
import { mount, enableAutoUnmount, disableAutoUnmount } from '../src'
23

34
describe('enableAutoUnmount', () => {
@@ -6,21 +7,21 @@ describe('enableAutoUnmount', () => {
67
})
78

89
it('calls the hook function', () => {
9-
const hookMock = jest.fn()
10+
const hookMock = vi.fn()
1011

1112
enableAutoUnmount(hookMock)
1213

1314
expect(hookMock).toHaveBeenCalledWith(expect.any(Function))
1415
})
1516

1617
it('uses the hook function to unmount wrappers', () => {
17-
const hookMock = jest.fn()
18+
const hookMock = vi.fn()
1819

1920
enableAutoUnmount(hookMock)
2021
const [unmountFn] = hookMock.mock.calls[0]
2122

2223
const wrapper = mount({ template: '<p>test</p>' })
23-
jest.spyOn(wrapper, 'unmount')
24+
vi.spyOn(wrapper, 'unmount')
2425

2526
unmountFn()
2627

tests/classes.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from 'vitest'
12
import { defineComponent, h } from 'vue'
23

34
import { mount } from '../src'

tests/config.spec.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
12
import { defineComponent, ComponentPublicInstance, h, inject } from 'vue'
23
import type { App } from 'vue'
34
import { config, mount } from '../src'
@@ -18,7 +19,7 @@ describe('config', () => {
1819
renderStubDefaultSlot: false
1920
}
2021

21-
jest.clearAllMocks()
22+
vi.clearAllMocks()
2223
})
2324

2425
describe('config merger', () => {
@@ -55,8 +56,8 @@ describe('config', () => {
5556
describe('config integrity', () => {
5657
it('should not leak config when plugins overwrite globalProperties', async () => {
5758
// test with a function because it's not an "easy to clone" primitive type
58-
const globalRouterMock = { push: jest.fn() }
59-
const pluginRouterMock = { push: jest.fn() }
59+
const globalRouterMock = { push: vi.fn() }
60+
const pluginRouterMock = { push: vi.fn() }
6061
const Component = defineComponent({ template: '<div />' })
6162

6263
class Plugin {
@@ -121,14 +122,14 @@ describe('config', () => {
121122
expect(comp.find('#default-slot').exists()).toBe(true)
122123

123124
// @ts-expect-error
124-
let comp = mount(Component, {
125+
let comp2 = mount(Component, {
125126
shallow: true,
126127
global: {
127128
renderStubDefaultSlot: 0
128129
}
129130
})
130131

131-
expect(comp.find('#default-slot').exists()).toBe(false)
132+
expect(comp2.find('#default-slot').exists()).toBe(false)
132133
})
133134
})
134135

@@ -256,7 +257,7 @@ describe('config', () => {
256257
})
257258

258259
describe('mixins', () => {
259-
const createdHook = jest.fn()
260+
const createdHook = vi.fn()
260261
const mixin = {
261262
created() {
262263
createdHook()
@@ -278,7 +279,7 @@ describe('config', () => {
278279

279280
it('concat with locally defined mixins', () => {
280281
config.global.mixins = [mixin]
281-
const localHook = jest.fn()
282+
const localHook = vi.fn()
282283
const localMixin = {
283284
created() {
284285
localHook(this.$options!.name)

tests/docs-examples/teleport.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { afterEach, beforeEach, expect, test } from 'vitest'
12
import { mount } from '../../src'
23
import Navbar from './Navbar.vue'
34
import Signup from './Signup.vue'

tests/element.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from 'vitest'
12
import { defineComponent, h } from 'vue'
23

34
import { mount } from '../src'

tests/emit.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
12
import {
23
defineComponent,
34
FunctionalComponent,
@@ -16,7 +17,7 @@ describe('emitted', () => {
1617

1718
beforeEach(() => {
1819
consoleWarnSave = console.warn
19-
console.warn = jest.fn()
20+
console.warn = vi.fn()
2021
})
2122

2223
afterEach(() => {

tests/exists.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from 'vitest'
12
import { h, defineComponent } from 'vue'
23

34
import { mount } from '../src'

tests/expose.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { describe, expect, it } from 'vitest'
12
import { mount } from '../src'
23
import Hello from './components/Hello.vue'
34
import DefineExpose from './components/DefineExpose.vue'

tests/features/ScriptSetup_ToRefsInject.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { expect, it } from 'vitest'
12
import { mount } from '../../src'
23
import ScriptSetup_ToRefsInject from '../components/ScriptSetup_ToRefsInject.vue'
34

tests/features/ScriptSetup_WatchEffect.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { expect, it } from 'vitest'
12
import { mount } from '../../src'
23
import ScriptSetup_WatchEffect from '../components/ScriptSetup_WatchEffect.vue'
34

0 commit comments

Comments
 (0)