Skip to content

Commit a768015

Browse files
poyohosheremet-va
andauthoredMar 20, 2023
feat: support relative path in html report (#2695)
Co-authored-by: Vladimir <sleuths.slews0s@icloud.com>
1 parent 0cf4409 commit a768015

File tree

11 files changed

+23
-21
lines changed

11 files changed

+23
-21
lines changed
 

‎docs/guide/ui.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ If you still want to see how your tests are running in real time in the terminal
4242
To preview your HTML report, you can use [vite preview](https://vitejs.dev/guide/cli.html#vite-preview) command:
4343

4444
```sh
45-
npx vite preview --base __vitest__ --outDir ./html
45+
npx vite preview --outDir ./html
4646
```
4747

4848
You can configure output with [`outputFile`](/config/#outputfile) config option. You need to specify `.html` path there. For example, `./html/index.html` is the default value.

‎packages/ui/client/composables/client/index.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import type { WebSocketStatus } from '@vueuse/core'
33
import type { Ref } from 'vue'
44
import { reactive } from 'vue'
55
import type { RunState } from '../../../types'
6+
import { ENTRY_URL, isReport } from '../../constants'
67
import { activeFileId } from '../params'
78
import { createStaticClient } from './static'
89
import type { File, ResolvedConfig } from '#types'
910

10-
export const PORT = import.meta.hot ? '51204' : location.port
11-
export const HOST = [location.hostname, PORT].filter(Boolean).join(':')
12-
export const ENTRY_URL = `${location.protocol === 'https:' ? 'wss:' : 'ws:'}//${HOST}/__vitest_api__`
13-
export const isReport = !!window.METADATA_PATH
11+
export { ENTRY_URL, PORT, HOST, isReport } from '../../constants'
12+
1413
export const testRunState: Ref<RunState> = ref('idle')
1514

1615
export const client = (function createVitestClient() {

‎packages/ui/client/composables/client/static.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function createStaticClient(): VitestClient {
3232
getFiles: () => {
3333
return metadata.files
3434
},
35-
getPaths: async () => {
35+
getPaths: () => {
3636
return metadata.paths
3737
},
3838
getConfig: () => {

‎packages/ui/client/constants.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const PORT = import.meta.hot ? '51204' : location.port
2+
export const HOST = [location.hostname, PORT].filter(Boolean).join(':')
3+
export const ENTRY_URL = `${location.protocol === 'https:' ? 'wss:' : 'ws:'}//${HOST}/__vitest_api__`
4+
export const isReport = !!window.METADATA_PATH
5+
export const BASE_PATH = isReport ? import.meta.env.BASE_URL : __BASE_PATH__

‎packages/ui/client/global-setup.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'codemirror-theme-vars/base.css'
1111
import './styles/main.css'
1212
import 'floating-vue/dist/style.css'
1313
import 'uno.css'
14+
import { BASE_PATH } from './constants'
1415

1516
export const directives = {
1617
tooltip: VTooltip,
@@ -20,7 +21,7 @@ FloatingVue.options.instantMove = true
2021
FloatingVue.options.distance = 10
2122

2223
export const createRouter = () => _createRouter({
23-
history: createWebHistory(import.meta.env.BASE_URL),
24+
history: createWebHistory(BASE_PATH),
2425
routes,
2526
})
2627

‎packages/ui/client/shim.d.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
/// <reference types="vite/client" />
22
/// <reference types="vite-plugin-pages/client" />
33

4-
declare module '*.vue' {
5-
import type { DefineComponent } from 'vue'
6-
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types
7-
const component: DefineComponent<{}, {}, any>
8-
export default component
9-
}
10-
11-
const __REPORT__: boolean
4+
const __BASE_PATH__: string
125

136
declare interface Window {
147
METADATA_PATH?: string

‎packages/ui/node/reporter.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class HTMLReporter implements Reporter {
3030

3131
async onFinished() {
3232
const result: HTMLReportData = {
33-
paths: await this.ctx.state.getPaths(),
33+
paths: this.ctx.state.getPaths(),
3434
files: this.ctx.state.getFiles(),
3535
config: this.ctx.config,
3636
moduleGraph: {},
@@ -71,6 +71,6 @@ export default class HTMLReporter implements Reporter {
7171
}))
7272

7373
this.ctx.logger.log(`${c.bold(c.inverse(c.magenta(' HTML ')))} ${c.magenta('Report is generated')}`)
74-
this.ctx.logger.log(`${c.dim(' You can run ')}${c.bold(`npx vite preview --base __vitest__ --outDir ${relative(this.ctx.config.root, htmlDir)}`)}${c.dim(' to see the test results.')}`)
74+
this.ctx.logger.log(`${c.dim(' You can run ')}${c.bold(`npx vite preview --outDir ${relative(this.ctx.config.root, htmlDir)}`)}${c.dim(' to see the test results.')}`)
7575
}
7676
}

‎packages/ui/shim.d.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <reference types="./client/auto-imports" />
2+
/// <reference types="./client/components" />
3+
/// <reference types="./client/shim" />

‎packages/ui/vite.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const debugLink = 'http://127.0.0.1:4173/__vitest__'
1515

1616
export const config: UserConfig = {
1717
root: __dirname,
18-
base: '/__vitest__/',
18+
base: './',
1919
resolve: {
2020
dedupe: ['vue'],
2121
alias: {
@@ -24,7 +24,7 @@ export const config: UserConfig = {
2424
},
2525
},
2626
define: {
27-
__REPORT__: false,
27+
__BASE_PATH__: '"/__vitest__/"',
2828
},
2929
plugins: [
3030
Vue(),

‎test/ui/test/html-report.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ describe.skipIf(isWindows)('html report', () => {
2020

2121
const exit = await startServerCommand(
2222
root,
23-
`npx vite preview --outDir html --strict-port --base /__vitest__/ --port ${port}`,
24-
`http://localhost:${port}/__vitest__/`,
23+
`npx vite preview --outDir html --strict-port --port ${port}`,
24+
`http://localhost:${port}/`,
2525
)
2626

2727
return exit

‎tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"./packages/*/*.d.ts",
4949
"./packages/*/*.d.cts",
5050
"./packages/ui/client/**",
51+
"./packages/ui/cypress/**",
5152
"./examples/**/*.*",
5253
"./bench/**",
5354
"./test/typescript/**"

0 commit comments

Comments
 (0)
Please sign in to comment.