Skip to content

Commit

Permalink
feat: integrate vitest 🌱
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed Jan 21, 2023
1 parent 2ea165f commit 8dd5cac
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 11 deletions.
9 changes: 6 additions & 3 deletions package.json
Expand Up @@ -20,18 +20,21 @@
"scripts": {
"dev": "vite build --watch",
"build": "vite build",
"test": "vite -c test/vite.config.ts",
"test": "vitest run",
"types": "tsc",
"prepublishOnly": "npm run build"
"prepublishOnly": "npm run test && npm run build"
},
"dependencies": {
"fast-glob": "^3.2.12"
},
"devDependencies": {
"@ant-design/icons-svg": "^4.2.1",
"@types/node": "^18.11.18",
"node-fetch": "^3.3.0",
"typescript": "^4.9.4",
"vite": "^4.0.4",
"vite-plugin-utils": "^0.4.0"
"vite-plugin-utils": "^0.4.0",
"vitest": "^0.27.2"
},
"keywords": [
"vite",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 6 additions & 8 deletions test/vite.config.ts → test/fixtures/vite.config.ts
@@ -1,23 +1,21 @@
import fs from 'fs'
import path from 'path'
import { defineConfig } from 'vite'
import dynamicImport from '..'
import dynamicImport from '../..'

export default defineConfig({
root: __dirname,
plugins: [
dynamicImport({
onFiles: files => files.filter(f => !f.includes('main-output.js')),
}),
dynamicImport(),
{
name: 'vite-plugin-dynamic-import:test',
transform(code, id) {
if (/src\/main\.ts$/.test(id)) {
const { dir, name } = path.parse(id)
const __snapshots__ = dir.replace('src', '__snapshots__')
!fs.existsSync(__snapshots__) && fs.mkdirSync(__snapshots__, { recursive: true })
// Write transformed code to __snapshots__
fs.writeFileSync(path.join(__snapshots__, `${name}.js`), code)
const dist = dir.replace('src', 'dist')
!fs.existsSync(dist) && fs.mkdirSync(dist, { recursive: true })
// Write transformed code to dist
fs.writeFileSync(path.join(dist, `${name}.js`), code)
}
},
},
Expand Down
64 changes: 64 additions & 0 deletions test/resolve.test.ts
@@ -0,0 +1,64 @@
import path from 'node:path'
import { resolveConfig } from 'vite'
import {
describe,
expect,
it,
} from 'vitest'
import { Resolve } from '../src/resolve'

const root = path.join(__dirname, 'fixtures')
const importer = path.join(root, 'src/main.ts')

function raw(importeeRaw: string) {
return importeeRaw.slice(1, -1)
}

describe('src/resolve.ts', async () => {
const config = await resolveConfig({ configFile: path.join(root, 'vite.config.ts'), }, 'build')
const resolve = new Resolve(config)

it('tryResolveAlias', async () => {
const list: [string, string | undefined][] = [
[
"`@/views/${id}.js`",
'./views/${id}.js',
],
[
"`src/views/${id}.mjs`",
'./views/${id}.mjs',
],
[
"`/root/src/views/${id}`",
'./views/${id}',
],
[
"`./views${id}`", // without alias
undefined,
],
[
"`./views${id}.tsx`",
undefined,
],
[
"'@/views/' + 'foo.js'",
"./views/' + 'foo.js",
],
[
"`@/${id}`",
'./${id}',
],
]

for (const [form, to] of list) {
const resolved = await resolve.tryResolve(raw(form), importer)
expect(resolved?.import.resolved).eq(to)
}
})

it('tryResolveBare', async () => {
const resolved = await resolve.tryResolve("@ant-design/icons-svg/es/`${filename}", importer)
const value = '../../../node_modules/@ant-design/icons-svg/es/`${filename}'
expect(resolved?.import.resolved).eq(value)
})
})
40 changes: 40 additions & 0 deletions test/serve.test.ts
@@ -0,0 +1,40 @@
import fs from 'node:fs'
import path from 'node:path'
import {
type ViteDevServer,
createServer,
} from 'vite'
import {
afterAll,
beforeAll,
describe,
expect,
it,
} from 'vitest'
import fetch from 'node-fetch'

const root = path.join(__dirname, 'fixtures')
let server: ViteDevServer | null = null
const PORT = 4000

beforeAll(async () => {
fs.rmSync(path.join(root, 'dist'), { recursive: true, force: true })
server = await createServer({ configFile: path.join(root, 'vite.config.ts') })
await server.listen(PORT)
})

describe('vite serve', async () => {
it('__snapshots__', async () => {
const mainTs = await (await fetch(`http://localhost:${PORT}/src/main.ts`)).text()
const mainJs = fs.readFileSync(path.join(path.join(root, 'dist/main.js')), 'utf8')
const mainJsSnap = fs.readFileSync(path.join(path.join(root, '__snapshots__/main.js')), 'utf8')

expect(mainTs).string
expect(mainJs).eq(mainJsSnap)
})
})

afterAll(async () => {
await server?.close()
server = null
})
66 changes: 66 additions & 0 deletions test/utils.test.ts
@@ -0,0 +1,66 @@
import {
describe,
expect,
it,
} from 'vitest'
import {
toLooseGlob,
mappingPath,
} from '../src/utils'

describe('src/resolve.ts', async () => {
it('toLooseGlob', async () => {
const list: [string, string | string[]][] = [
[
'foo/*',
'foo/**/*',
],
[
'foo/*.js',
'foo/**/*.js',
],
// ---- string[] ----
[
'foo*',
['foo*', 'foo*/**/*'],
],
[
'foo*.js',
['foo*.js', 'foo*/**/*.js'],
],
[
'foo*bar.js',
['foo*bar.js', 'foo*/**/*bar.js'],
],
[
'foo*/bar.js',
['foo*/bar.js', 'foo*/**/bar.js'],
],
]

for (const [form, to] of list) {
const glob = toLooseGlob(form)
expect(glob).toEqual(to)
}
})

it('mappingPath', async () => {
const path1 = {
'./foo/index.js': [
'./foo',
'./foo/index',
'./foo/index.js',
],
}
const path2 = {
'./foo/index.js': [
'@/foo',
'@/foo/index',
'@/foo/index.js',
],
}

expect(mappingPath(Object.keys(path1))).toEqual(path1)
expect(mappingPath(Object.keys(path2), { '.': '@' })).toEqual(path2)
})
})

0 comments on commit 8dd5cac

Please sign in to comment.