Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
fix: ensure vitest mocking plugin is placed last (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Jul 17, 2023
1 parent a12da00 commit 8a89fe3
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 211 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@nuxt/schema": "3.6.3",
"@release-it-plugins/workspaces": "4.0.0",
"@release-it/conventional-changelog": "7.0.0",
"@vitest/coverage-c8": "0.30.1",
"@vitest/coverage-v8": "0.33.0",
"conventional-changelog-conventionalcommits": "6.1.0",
"eslint": "8.45.0",
"eslint-config-prettier": "8.8.0",
Expand All @@ -34,7 +34,7 @@
"release-it": "16.1.2",
"typescript": "5.1.6",
"unbuild": "1.2.1",
"vitest": "0.30.1",
"vitest": "0.33.0",
"vue": "3.3.4",
"vue-tsc": "1.8.5"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/nuxt-vitest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
"@vitejs/plugin-vue": "*",
"@vitejs/plugin-vue-jsx": "*",
"vite": "*",
"vitest": "^0.30.0"
"vitest": "^0.33.0"
},
"dependencies": {
"@nuxt/kit": "^3.2.0",
"@vitest/ui": "^0.30.0",
"@vitest/ui": "^0.33.0",
"defu": "^6.1.2",
"get-port-please": "^3.0.1",
"perfect-debounce": "^1.0.0",
Expand All @@ -63,8 +63,8 @@
},
"devDependencies": {
"@nuxt/module-builder": "0.4.0",
"vitest": "^0.33.0",
"@nuxt/schema": "3.6.3",
"nuxt": "3.6.3",
"vitest": "^0.30.0"
"nuxt": "3.6.3"
}
}
23 changes: 22 additions & 1 deletion packages/vitest-environment-nuxt/src/modules/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { CallExpression } from 'estree'
import { AcornNode } from 'rollup'
import MagicString from 'magic-string'
import { Component } from '@nuxt/schema'
import type { Plugin } from 'vite'

const PLUGIN_NAME = 'nuxt:vitest:mock-transform'

Expand Down Expand Up @@ -52,11 +53,31 @@ export default defineNuxtModule({
components = _
})

// Polyfill Array.prototype.findLastIndex for legacy Node.js
function findLastIndex<T>(arr: T[], predicate: (item: T) => boolean) {
for (let i = arr.length - 1; i >= 0; i--) {
if (predicate(arr[i])) return i
}
return -1
}

addVitePlugin({
name: PLUGIN_NAME,
enforce: 'post',
// Place Vitest's mock plugin after all Nuxt plugins
configResolved(config) {
const plugins = (config.plugins || []) as Plugin[]
// `vite:mocks` was a typo in Vitest before v0.34.0
const mockPluginIndex = plugins.findIndex(i => i.name === 'vite:mocks' || i.name === 'vitest:mocks')
const lastNuxt = findLastIndex(plugins, i => i.name?.startsWith('nuxt:'))
if (mockPluginIndex !== -1 && lastNuxt !== -1) {
if (mockPluginIndex < lastNuxt) {
const [mockPlugin] = plugins.splice(mockPluginIndex, 1)
plugins.splice(lastNuxt, 0, mockPlugin)
}
}
},
transform: {
order: 'post',
handler(code, id) {
if (!HELPERS_NAME.some(n => code.includes(n))) return
if (id.includes('/node_modules/')) return
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"nuxt": "3.6.3",
"nuxt-vitest": "0.9.0",
"typescript": "5.1.6",
"vitest": "0.30.1",
"vitest": "0.33.0",
"vitest-environment-nuxt": "0.9.0",
"vue-tsc": "1.8.5"
},
Expand Down
Loading

0 comments on commit 8a89fe3

Please sign in to comment.