Skip to content

Commit c124838

Browse files
committed
chore: add tests (coverage broken)
1 parent 2ca50ba commit c124838

File tree

5 files changed

+108
-13
lines changed

5 files changed

+108
-13
lines changed

app/components/BuildEnvironment.vue

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<script setup lang="ts">
2-
defineProps<{
2+
import type { BuildInfo } from '#shared/types'
3+
4+
const { footer = false, buildInfo: buildInfoProp } = defineProps<{
35
footer?: boolean
6+
buildInfo?: BuildInfo
47
}>()
58
69
const { locale } = useI18n()
7-
const buildInfo = useAppConfig().buildInfo
10+
const appConfig = useAppConfig()
11+
const buildInfo = computed(() => buildInfoProp || appConfig.buildInfo)
812
</script>
913

1014
<template>

config/env.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const isPreview =
4141
process.env.VERCEL_ENV === 'preview' ||
4242
process.env.VERCEL_ENV === 'development'
4343

44+
export type EnvType = 'dev' | 'preview' | 'canary' | 'release'
45+
4446
const git = Git()
4547
export async function getGitInfo() {
4648
let branch

modules/build-env.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { EnvType } from '../config/env'
12
import type { BuildInfo } from '../shared/types'
23
import { createResolver, defineNuxtModule } from 'nuxt/kit'
34
import { isCI } from 'std-env'
@@ -10,18 +11,31 @@ export default defineNuxtModule({
1011
name: 'npmx:build-env',
1112
},
1213
async setup(_options, nuxt) {
13-
const { env, commit, shortCommit, branch } = await getEnv(nuxt.options.dev)
14-
1514
nuxt.options.appConfig = nuxt.options.appConfig || {}
16-
nuxt.options.appConfig.env = env
17-
nuxt.options.appConfig.buildInfo = {
18-
version,
19-
time: +Date.now(),
20-
commit,
21-
shortCommit,
22-
branch,
23-
env,
24-
} satisfies BuildInfo
15+
let env: EnvType = 'dev'
16+
17+
if (process.env.TEST) {
18+
nuxt.options.appConfig.buildInfo = {
19+
env,
20+
version: '0.0.0',
21+
commit: '704987bba88909f3782d792c224bde989569acb9',
22+
shortCommit: '704987b',
23+
branch: 'xxx',
24+
time: 1770237446424,
25+
} satisfies BuildInfo
26+
} else {
27+
const { env: useEnv, commit, shortCommit, branch } = await getEnv(nuxt.options.dev)
28+
env = useEnv
29+
nuxt.options.appConfig.env = useEnv
30+
nuxt.options.appConfig.buildInfo = {
31+
version,
32+
time: +Date.now(),
33+
commit,
34+
shortCommit,
35+
branch,
36+
env,
37+
} satisfies BuildInfo
38+
}
2539

2640
nuxt.options.nitro.publicAssets = nuxt.options.nitro.publicAssets || []
2741
if (env === 'dev') nuxt.options.nitro.publicAssets.unshift({ dir: resolve('../public-dev') })
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { mountSuspended } from '@nuxt/test-utils/runtime'
3+
import AppFooter from '~/components/AppFooter.vue'
4+
5+
describe('AppFooter', () => {
6+
it('BuildEnvironment is properly displayed at settings', async () => {
7+
const component = await mountSuspended(AppFooter, {
8+
route: '/settings',
9+
})
10+
const html = component.html()
11+
expect(html).toContain('<span class="tracking-wider">dev</span>')
12+
expect(html).toContain(
13+
'<a href="https://github.com/npmx-dev/npmx.dev/commit/704987bba88909f3782d792c224bde989569acb9"',
14+
)
15+
})
16+
17+
it('BuildEnvironment is hidden at home', async () => {
18+
const component = await mountSuspended(AppFooter, {
19+
route: '/',
20+
})
21+
const html = component.html()
22+
expect(html).not.toContain(
23+
'<a href="https://github.com/npmx-dev/npmx.dev/commit/704987bba88909f3782d792c224bde989569acb9"',
24+
)
25+
expect(html).not.toContain('<span class="tracking-wider">dev</span>')
26+
})
27+
})
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { BuildInfo } from '#shared/types'
2+
import { describe, expect, it } from 'vitest'
3+
import { mountSuspended } from '@nuxt/test-utils/runtime'
4+
import BuildEnvironment from '~/components/BuildEnvironment.vue'
5+
6+
describe('BuildEnvironment', () => {
7+
it('renders dev environment correctly', async () => {
8+
const buildInfo = useAppConfig().buildInfo as BuildInfo
9+
const component = await mountSuspended(BuildEnvironment, {
10+
props: {
11+
buildInfo,
12+
},
13+
})
14+
const html = component.html()
15+
16+
// In dev mode, it shows env name, not version link
17+
expect(html).toContain('<span class="tracking-wider">dev</span>')
18+
expect(html).toContain(
19+
'<a href="https://github.com/npmx-dev/npmx.dev/commit/704987bba88909f3782d792c224bde989569acb9"',
20+
)
21+
expect(html).not.toContain('href="https://github.com/npmx-dev/npmx.dev/tag/v0.0.0"')
22+
})
23+
24+
it('renders release environment correctly', async () => {
25+
const buildInfo: BuildInfo = {
26+
env: 'release',
27+
version: '1.2.3',
28+
time: 1234567890,
29+
commit: 'abcdef',
30+
shortCommit: 'abc',
31+
branch: 'release',
32+
}
33+
34+
const component = await mountSuspended(BuildEnvironment, {
35+
props: {
36+
buildInfo,
37+
},
38+
})
39+
const html = component.html()
40+
41+
// In release mode, it shows tag version link, not env name
42+
expect(html).not.toContain('<span class="tracking-wider">dev</span>')
43+
expect(html).not.toContain(
44+
'<a href="https://github.com/npmx-dev/npmx.dev/commit/704987bba88909f3782d792c224bde989569acb9"',
45+
)
46+
expect(html).toContain('href="https://github.com/npmx-dev/npmx.dev/tag/v1.2.3"')
47+
})
48+
})

0 commit comments

Comments
 (0)