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

Commit

Permalink
chore: migrate to vitest from mocha (#2694)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored and danielroe committed Jan 13, 2022
1 parent fbadf57 commit c7205de
Show file tree
Hide file tree
Showing 13 changed files with 2,068 additions and 1,074 deletions.
9 changes: 3 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
"lint": "eslint --ext .vue,.ts,.js,.mjs .",
"lint:docs": "./node_modules/.bin/markdownlint ./",
"test": "yarn lint && yarn test:presets",
"test:presets": "mocha test/presets/*.mjs",
"test:presets": "vitest test/presets",
"test:bridge:webpack": "TEST_BRIDGE=1 yarn test:presets",
"test:bridge:vite": "TEST_BRIDGE_VITE=1 TEST_BRIDGE=1 yarn test:presets",
"test:unit": "mocha -r jiti/register packages/*/test/*.test.*",
"test:unit": "vitest packages",
"version": "yarn && git add yarn.lock"
},
"resolutions": {
Expand All @@ -37,13 +37,10 @@
"@nuxt/ui": "^0.0.0-alpha.5",
"@nuxtjs/eslint-config": "^7.0.0",
"@nuxtjs/eslint-config-typescript": "^7.0.2",
"@types/chai": "^4.3.0",
"@types/jsdom": "^16",
"@types/mocha": "^9.0.0",
"@types/node": "^16.11.19",
"@types/object-hash": "^2",
"@unocss/reset": "^0.22.2",
"chai": "^4.3.4",
"esbuild": "^0.14.11",
"eslint": "^8.6.0",
"eslint-plugin-jsdoc": "^37.6.1",
Expand All @@ -53,11 +50,11 @@
"lerna": "^4.0.0",
"markdownlint-cli": "^0.30.0",
"miniflare": "^1.4.1",
"mocha": "^9.1.3",
"object-hash": "^2.2.0",
"pathe": "^0.2.0",
"typescript": "^4.5.4",
"unbuild": "^0.6.7",
"vitest": "^0.1.0",
"vue-router": "next"
},
"packageManager": "yarn@3.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/bridge/test/auto-imports.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as CompositionApi from '@vue/composition-api'
import { expect } from 'chai'
import { expect, describe, it } from 'vitest'

import { Nuxt3AutoImports } from '../../nuxt3/src/auto-imports/imports'

Expand Down
6 changes: 4 additions & 2 deletions packages/nuxt3/test/auto-imports.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs'
import type { AutoImport } from '@nuxt/schema'
import { expect } from 'chai'
import { expect, describe, it } from 'vitest'
import { join } from 'pathe'
import { createCommonJS, findExports } from 'mlly'
import * as VueFunctions from 'vue'
Expand Down Expand Up @@ -73,6 +73,8 @@ describe('auto-imports:nuxt3', () => {
})

const excludedVueHelpers = [
'__esModule',
'devtools',
'EffectScope',
'ReactiveEffect',
'stop',
Expand Down Expand Up @@ -166,7 +168,7 @@ describe('auto-imports:vue', () => {
continue
}
it(`should register ${name} globally`, () => {
expect(Nuxt3AutoImports.find(a => a.from === 'vue').names).to.include(name)
expect(Nuxt3AutoImports.find(a => a.from === 'vue').names).toContain(name)
})
}
})
2 changes: 1 addition & 1 deletion packages/nuxt3/test/pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect } from 'chai'
import { expect, describe, it } from 'vitest'
import { generateRoutesFromFiles } from '../src/pages/utils'

describe('pages:generateRoutesFromFiles', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt3/test/scan-components.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path'
import { ComponentsDir } from '@nuxt/schema'
import { expect } from 'chai'
import { expect, it } from 'vitest'
import { scanComponents } from '../src/components/scan'

const fixtureDir = resolve(__dirname, 'fixture')
Expand Down
24 changes: 15 additions & 9 deletions test/presets/_tests.mjs → test/presets/_tests.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { pathToFileURL } from 'url'
import { resolve } from 'pathe'
import destr from 'destr'
import { listen } from 'listhen'
import { listen, Listener } from 'listhen'
import { $fetch } from 'ohmyfetch'
import { execa } from 'execa'
import { expect } from 'chai'
import { fixtureDir, resolveWorkspace } from '../utils.mjs'
import { expect, it, beforeAll, afterAll } from 'vitest'
import { fixtureDir, resolveWorkspace } from '../utils'

const isBridge = Boolean(process.env.TEST_BRIDGE)

interface Context {
rootDir: string
outDir: string
fetch: (url:string) => Promise<any>
server?: Listener
}

export function importModule (path) {
return import(pathToFileURL(path).href)
}
Expand All @@ -18,14 +25,13 @@ export function setupTest (preset) {
const rootDir = fixtureDir(fixture)
const buildDir = resolve(rootDir, '.nuxt-' + preset)

const ctx = {
const ctx: Context = {
rootDir,
outDir: resolve(buildDir, 'output'),
fetch: url => $fetch(url, { baseURL: ctx.server.url })
fetch: url => $fetch(url, { baseURL: ctx.server!.url })
}

before('nitro build', async function () {
this.timeout(60000)
beforeAll(async () => {
const nuxtCLI = isBridge
? resolve(ctx.rootDir, 'node_modules/nuxt-edge/bin/nuxt.js')
: resolveWorkspace('packages/nuxi/bin/nuxi.mjs')
Expand All @@ -38,9 +44,9 @@ export function setupTest (preset) {
NODE_ENV: 'production'
}
})
})
}, (isBridge ? 120 : 60) * 1000)

after('Cleanup', async () => {
afterAll(async () => {
if (ctx.server) {
await ctx.server.close()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { promises as fsp } from 'fs'
import { resolve } from 'pathe'
import { Miniflare } from 'miniflare'
import { describe } from 'vitest'

import { setupTest, testNitroBehavior } from './_tests.mjs'
import { setupTest, testNitroBehavior } from './_tests'

// TODO: fix SyntaxError: Unexpected end of input on script executation
describe('nitro:preset:cloudflare', () => {
Expand Down
3 changes: 2 additions & 1 deletion test/presets/lambda.test.mjs → test/presets/lambda.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'pathe'
import { setupTest, testNitroBehavior, importModule } from './_tests.mjs'
import { describe } from 'vitest'
import { setupTest, testNitroBehavior, importModule } from './_tests'

describe('nitro:preset:lambda', () => {
const ctx = setupTest('lambda')
Expand Down
3 changes: 2 additions & 1 deletion test/presets/node.test.mjs → test/presets/node.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'pathe'
import { startServer, setupTest, testNitroBehavior, importModule } from './_tests.mjs'
import { describe } from 'vitest'
import { startServer, setupTest, testNitroBehavior, importModule } from './_tests.js'

describe('nitro:preset:node', () => {
const ctx = setupTest('node')
Expand Down
3 changes: 2 additions & 1 deletion test/presets/vercel.test.mjs → test/presets/vercel.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { resolve } from 'pathe'
import { setupTest, startServer, testNitroBehavior, importModule } from './_tests.mjs'
import { describe } from 'vitest'
import { setupTest, startServer, testNitroBehavior, importModule } from './_tests'

describe('nitro:preset:vercel', () => {
const ctx = setupTest('vercel')
Expand Down
12 changes: 6 additions & 6 deletions test/utils.mjs → test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import { execSync } from 'child_process'
import { resolve, dirname } from 'pathe'
import defu from 'defu'
import hash from 'object-hash'
import { execa } from 'execa'
import { execa, Options as ExecaOptions } from 'execa'
import { createCommonJS } from 'mlly'

const cjs = createCommonJS(import.meta.url)

export function resolveWorkspace (name) {
export function resolveWorkspace (name: string) {
return resolve(cjs.__dirname, '../', name)
}

export function fixtureDir (name) {
export function fixtureDir (name: string) {
return resolve(cjs.__dirname, 'fixtures', name)
}

export async function execNuxtCLI (args, opts) {
export async function execNuxtCLI (args: string[], opts: ExecaOptions) {
const nuxtCLI = resolveWorkspace('packages/nuxi/bin/nuxi.mjs')
await execa('node', [nuxtCLI, ...args], opts)
}

export async function loadFixture (opts, unhashedConfig) {
export async function loadFixture (opts: any, unhashedConfig?: any) {
const buildId = hash(opts)
const buildDir = resolve(opts.rootDir, '.nuxt', buildId)
const { loadNuxt } = await import('@nuxt/kit')
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function buildFixture (opts) {
}
}

function mkdirpSync (dir) {
function mkdirpSync (dir: string) {
if (!existsSync(dir)) {
mkdirpSync(dirname(dir))
mkdirSync(dir)
Expand Down
4 changes: 1 addition & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"noUnusedLocals": true,
"resolveJsonModule": true,
"types": [
"node",
"mocha",
"chai"
"node"
],
"paths": {
"#app": [
Expand Down
Loading

1 comment on commit c7205de

@stevez
Copy link

@stevez stevez commented on c7205de Apr 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I run into issue when migrate from mocha to vitest, since we use sinonjs for mock, I wonder can vitest support sinon or have to replace sinon with vitest's mock?

Please sign in to comment.