Skip to content

Commit

Permalink
feat: render unit test files even in --bare mode
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang committed Dec 20, 2024
1 parent e7603a4 commit b56500c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 22 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ jobs:
verification-script:
- pnpm --filter '!*typescript*' build
- pnpm --filter '*typescript*' build
# bare templates only contain vitest configurations, but do not include unit test files
- pnpm --filter '*vitest*' --filter '!*bare*' test:unit
- pnpm --filter '*vitest*' test:unit
- pnpm --filter '*eslint*' lint --no-fix --max-warnings=0
- pnpm --filter '*prettier*' format --write --check
# FIXME: it's failing now
Expand Down Expand Up @@ -173,7 +172,6 @@ jobs:

- name: Cypress component testing for projects without Vitest
if: ${{ contains(matrix.e2e-framework, 'cypress') }}
# bare templates only contain test configurations, but do not include component test files
run: pnpm --filter '*cypress*' --filter '!*vitest*' --filter '!*bare*' --workspace-concurrency 1 test:unit
run: pnpm --filter '*cypress*' --filter '!*vitest*' --workspace-concurrency 1 test:unit

# FIXME: `--with-tests` folders. It's failing now.
15 changes: 15 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,21 @@ async function init() {

if (argv.bare) {
trimBoilerplate(root, { needsTypeScript, needsRouter })
render('bare/base')

// TODO: refactor the `render` utility to avoid this kind of manual mapping?
if (needsTypeScript) {
render('bare/typescript')
}
if (needsVitest) {
render('bare/vitest')
}
if (needsCypressCT) {
render('bare/cypress-ct')
}
if (needsNightwatchCT) {
render('bare/nightwatch-ct')
}
}

// Instructions:
Expand Down
7 changes: 7 additions & 0 deletions template/bare/base/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup></script>

<template>
<h1>Hello World</h1>
</template>

<style scoped></style>
8 changes: 8 additions & 0 deletions template/bare/cypress-ct/src/__tests__/App.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import App from '../App.vue'

describe('App', () => {
it('mounts and renders properly', () => {
cy.mount(App)
cy.get('h1').should('contain', 'Hello World')
})
})
14 changes: 14 additions & 0 deletions template/bare/nightwatch-ct/src/__tests__/App.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
describe('App', function () {
before((browser) => {
browser.init()
})

it('mounts and renders properly', async function () {
const appComponent = await browser.mountComponent('/src/App.vue');

browser.expect.element(appComponent).to.be.present;
browser.expect.element('h1').text.to.contain('Hello World');
})

after((browser) => browser.end())
})
7 changes: 7 additions & 0 deletions template/bare/typescript/src/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script setup lang="ts"></script>

<template>
<h1>Hello World</h1>
</template>

<style scoped></style>
11 changes: 11 additions & 0 deletions template/bare/vitest/src/__tests__/App.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, it, expect } from 'vitest'

import { mount } from '@vue/test-utils'
import App from '../App.vue'

describe('App', () => {
it('mounts renders properly', () => {
const wrapper = mount(App)
expect(wrapper.text()).toContain('Hello World')
})
})
21 changes: 3 additions & 18 deletions utils/trimBoilerplate.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
import * as fs from 'node:fs'
import * as path from 'path'

function getBareBoneAppContent(isTs: boolean) {
return `<script setup${isTs ? ' lang="ts"' : ''}>
</script>
<template>
<h1>Hello World</h1>
</template>
<style scoped>
</style>
`
}

function replaceContent(filepath: string, replacer: (content: string) => string) {
const content = fs.readFileSync(filepath, 'utf8')
fs.writeFileSync(filepath, replacer(content))
Expand All @@ -24,17 +11,15 @@ export default function trimBoilerplate(rootDir: string, features: Record<string
const srcDir = path.resolve(rootDir, 'src')

for (const filename of fs.readdirSync(srcDir)) {
// Keep `App.vue`, `main.js/ts`, `router`, and `stores` directories
if (['App.vue', 'main.js', 'main.ts', 'router', 'stores'].includes(filename)) {
// Keep `main.js/ts`, `router`, and `stores` directories
// `App.vue` would be re-rendered in the next step
if (['main.js', 'main.ts', 'router', 'stores'].includes(filename)) {
continue
}
const fullpath = path.resolve(srcDir, filename)
fs.rmSync(fullpath, { recursive: true })
}

// Replace the content in `src/App.vue` with a barebone template
replaceContent(path.resolve(rootDir, 'src/App.vue'), () => getBareBoneAppContent(isTs))

// Remove CSS import in the entry file
const entryPath = path.resolve(rootDir, isTs ? 'src/main.ts' : 'src/main.js')
replaceContent(entryPath, (content) => content.replace("import './assets/main.css'\n\n", ''))
Expand Down

0 comments on commit b56500c

Please sign in to comment.