Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test suite #2

Merged
merged 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: releaser

on:
push:
tags: ['v*']

jobs:
releaser:
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Extra Changelog
run: |
CHANGELOG=$(awk -v ver=$(awk -F'"' '/"version": ".+"/{ print $4; exit; }' package.json) '/^## / { if (p) { exit }; if ($2 == ver) { p=1; next} } p' CHANGELOG.md)
echo "CHANGELOG<<EOF" >> $GITHUB_ENV
echo "$CHANGELOG" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
- name: Github Releaser
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
body: ${{ env.CHANGELOG }}
draft: false
prerelease: false
22 changes: 22 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install berry
run: corepack enable
- uses: actions/setup-node@v3
with:
node-version: 18
- name: Install Dependices
run: yarn install

- name: Run Test
run: yarn test

- name: Report Coverage
uses: codecov/codecov-action@v2
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ pnpm-debug.log*
analyzer.html

stats.json

coverage
13 changes: 13 additions & 0 deletions __tests__/fixtures/normal/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script id="app"></script>
<script type="module" src="./index.tsx"></script>
</body>
</html>
10 changes: 10 additions & 0 deletions __tests__/fixtures/normal/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react'
import ReactDOM from 'react-dom/client'


function App() {
return <div>111</div>
}


ReactDOM.createRoot(document.querySelector('#app')!).render(<React.StrictMode><App /></React.StrictMode>)
48 changes: 48 additions & 0 deletions __tests__/plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import path from 'path'
import fs from 'fs'
import fsp from 'fs/promises'
import { build } from 'vite'
import chai from 'chai'
import react from '@vitejs/plugin-react'
import { analyzer } from '../src/server'
import type { AnalyzerPluginOptions } from '../src/server'

const defaultWd = __dirname

const fixturePath = path.join(defaultWd, 'fixtures')
const bundlePath = path.join(defaultWd, 'dist')

function getId() {
return Math.random().toString(32).slice(2, 10)
}

async function createBuildServer(fixtureName: string, options?: AnalyzerPluginOptions) {
const entry = path.join(fixturePath, fixtureName)
const id = getId()
await build({
root: entry,
logLevel: 'silent',
configFile: false,
plugins: [react(), analyzer(options)],
build: {
outDir: path.join(bundlePath, id)
}
})
return entry
}

describe('Plugin', () => {
afterEach(async () => {
await fsp.rm(bundlePath, { recursive: true })
})
it('generator JSON', async () => {
const id = await createBuildServer('normal', { analyzerMode: 'json' })
const statsPath = path.join(id, 'stats.json')
chai.assert.isTrue(fs.existsSync(statsPath))
})
it('generator Static Page', async () => {
const id = await createBuildServer('normal', { analyzerMode: 'static' })
const analyzerPath = path.join(id, 'analyzer.html')
chai.assert.isTrue(fs.existsSync(analyzerPath))
})
})
18 changes: 16 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@
"scripts": {
"dev": "vite src/client",
"build:client": "vite build src/client",
"build:plugin": "tsup"
"build:plugin": "tsup",
"test": "c8 -r=lcov mocha"
},
"devDependencies": {
"@babel/runtime": "^7.23.1",
"@carrotsearch/foamtree": "^3.5.1",
"@geist-ui/core": "^2.3.8",
"@geist-ui/icons": "^1.0.2",
"@types/chai": "^4.3.8",
"@types/mocha": "^10.0.2",
"@types/node": "^20.7.0",
"@types/react": "18.2.21",
"@types/react-dom": "18.2.7",
"@typescript-eslint/eslint-plugin": "^6.7.3",
"@typescript-eslint/parser": "^6.7.3",
"@vitejs/plugin-react": "^4.0.4",
"c8": "^8.0.1",
"chai": "^4.3.10",
"eslint": "^8.49.0",
"eslint-config-kagura": "^1.2.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"mocha": "^10.2.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"style9": "^0.18.2",
"tsup": "^7.2.0",
"tsx": "^3.13.0",
"typescript": "^5.2.2",
"vite": "^4.4.9"
},
Expand Down Expand Up @@ -82,5 +89,12 @@
},
"dependencies": {
"fast-glob": "^3.3.1"
},
"mocha": {
"extension": [
"ts"
],
"spec": "__tests__/*.spec.ts",
"require": "tsx/cjs"
}
}
}
7 changes: 6 additions & 1 deletion src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fsp from 'fs/promises'
import path from 'path'
import type{ Plugin } from 'vite'
import { name } from '../../package.json'
import { defaultWd } from './shared'
import type { AnalyzerPluginOptions } from './interface'
import { createAnalyzerModule } from './analyzer-module'
import { renderView } from './render'
Expand All @@ -11,11 +10,15 @@ import { renderView } from './render'
function analyzer(opts: AnalyzerPluginOptions = {}): Plugin {
const { analyzerMode = 'json', statsFilename = 'stats.json', reportFileName = 'analyzer.html' } = opts
const analyzerModule = createAnalyzerModule(opts)
let defaultWd = process.cwd()

const plugin = <Plugin>{
name,
apply: 'build',
enforce: 'post',
configResolved(config) {
defaultWd = config.root
},
generateBundle(_, outputBundle) {
// After consider. I trust process chunk is enougth. (If you don't think it's right. PR welcome.)
for (const bundleName in outputBundle) {
Expand Down Expand Up @@ -51,3 +54,5 @@ function analyzer(opts: AnalyzerPluginOptions = {}): Plugin {
export { analyzer }

export { analyzer as default }
export * from './interface'

Loading
Loading