Skip to content

Commit

Permalink
chore(test): cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
caoxiemeihao committed May 4, 2023
1 parent bcac9ab commit fe75572
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
File renamed without changes.
4 changes: 4 additions & 0 deletions test/fixtures/tsconfig.json
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"exclude": ["__snapshots__", "dist"]
}
10 changes: 5 additions & 5 deletions test/fixtures/vite.config.ts
Expand Up @@ -11,11 +11,11 @@ export default defineConfig({
name: 'vite-plugin-dynamic-import:test',
transform(code, id) {
if (/src\/main\.ts$/.test(id)) {
const { dir, name } = path.parse(id)
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)
// write transformed code to dist/
const filename = id.replace('src', 'dist')
const dirname = path.dirname(filename)
if (!fs.existsSync(dirname)) fs.mkdirSync(dirname)
fs.writeFileSync(filename, code)
}
},
},
Expand Down
20 changes: 13 additions & 7 deletions test/serve.test.ts
Expand Up @@ -12,25 +12,31 @@ import {
it,
} from 'vitest'
import fetch from 'node-fetch'
import fastGlob from 'fast-glob'

const root = path.join(__dirname, 'fixtures')
let server: ViteDevServer | null = null
const PORT = 4000
let 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)
await server.listen(port)
// @ts-ignore
port = server.httpServer?.address().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(root, 'dist/main.js'), 'utf8')
const mainJsSnap = fs.readFileSync(path.join(root, '__snapshots__/main.js'), 'utf8')
const files = fastGlob.sync('__snapshots__/**/*', { cwd: root })
for (const file of files) {
const response = await (await fetch(`http://localhost:${port}/${file.replace('__snapshots__', 'src')}`)).text()
const distFile = fs.readFileSync(path.join(root, file.replace('__snapshots__', 'dist')), 'utf8')
const snapFile = fs.readFileSync(path.join(root, file), 'utf8')

expect(mainTs).string
expect(mainJs).eq(mainJsSnap)
expect(response).string
expect(distFile).eq(snapFile)
}
})
})

Expand Down

0 comments on commit fe75572

Please sign in to comment.