Skip to content

Commit 1638b44

Browse files
feat(browser): add qwik to the vitest init cli command (#8330)
1 parent 083f521 commit 1638b44

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

packages/vitest/src/create/browser/creator.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ function getFramework(): prompt.Choice[] {
104104
value: 'marko',
105105
description: '"A declarative, HTML-based language that makes building web apps fun"',
106106
},
107+
{
108+
title: 'qwik',
109+
value: 'qwik',
110+
description: '"Instantly interactive web apps at scale"',
111+
},
107112
]
108113
}
109114

@@ -125,6 +130,8 @@ function getFrameworkTestPackage(framework: string) {
125130
return '@solidjs/testing-library'
126131
case 'marko':
127132
return '@marko/testing-library'
133+
case 'qwik':
134+
return 'vitest-browser-qwik'
128135
}
129136
throw new Error(`Unsupported framework: ${framework}`)
130137
}
@@ -143,6 +150,8 @@ function getFrameworkPluginPackage(framework: string) {
143150
return 'vite-plugin-solid'
144151
case 'marko':
145152
return '@marko/vite'
153+
case 'qwik':
154+
return '@builder.io/qwik/optimizer'
146155
}
147156
return null
148157
}
@@ -224,6 +233,9 @@ function getPossibleFramework(dependencies: Record<string, string>) {
224233
if (dependencies.marko) {
225234
return 'marko'
226235
}
236+
if (dependencies['@builder.io/qwik'] || dependencies['@qwik.dev/core']) {
237+
return 'qwik'
238+
}
227239
return 'vanilla'
228240
}
229241

@@ -257,22 +269,36 @@ function fail() {
257269
process.exitCode = 1
258270
}
259271

272+
function getFrameworkImportInfo(framework: string) {
273+
switch (framework) {
274+
case 'svelte':
275+
return { importName: 'svelte', isNamedExport: true }
276+
case 'qwik':
277+
return { importName: 'qwikVite', isNamedExport: true }
278+
default:
279+
return { importName: framework, isNamedExport: false }
280+
}
281+
}
282+
260283
async function generateFrameworkConfigFile(options: {
261284
configPath: string
262285
framework: string
263286
frameworkPlugin: string | null
264287
provider: string
265288
browsers: string[]
266289
}) {
267-
const frameworkImport = options.framework === 'svelte'
268-
? `import { svelte } from '${options.frameworkPlugin}'`
269-
: `import ${options.framework} from '${options.frameworkPlugin}'`
290+
const { importName, isNamedExport } = getFrameworkImportInfo(options.framework)
291+
292+
const frameworkImport = isNamedExport
293+
? `import { ${importName} } from '${options.frameworkPlugin}'`
294+
: `import ${importName} from '${options.frameworkPlugin}'`
295+
270296
const configContent = [
271297
`import { defineConfig } from 'vitest/config'`,
272298
options.frameworkPlugin ? frameworkImport : null,
273299
``,
274300
'export default defineConfig({',
275-
options.frameworkPlugin ? ` plugins: [${options.framework}()],` : null,
301+
options.frameworkPlugin ? ` plugins: [${importName}()],` : null,
276302
` test: {`,
277303
` browser: {`,
278304
` enabled: true,`,

packages/vitest/src/create/browser/examples.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,42 @@ test('renders name', async () => {
191191
`,
192192
}
193193

194+
const qwikExample = {
195+
name: 'HelloWorld.jsx',
196+
js: `
197+
import { component$ } from '@builder.io/qwik'
198+
199+
export default component$(({ name }) => {
200+
return (
201+
<div>
202+
<h1>Hello {name}!</h1>
203+
</div>
204+
)
205+
})
206+
`,
207+
ts: `
208+
import { component$ } from '@builder.io/qwik'
209+
210+
export default component$(({ name }: { name: string }) => {
211+
return (
212+
<div>
213+
<h1>Hello {name}!</h1>
214+
</div>
215+
)
216+
})
217+
`,
218+
test: `
219+
import { expect, test } from 'vitest'
220+
import { render } from 'vitest-browser-qwik'
221+
import HelloWorld from './HelloWorld.tsx'
222+
223+
test('renders name', async () => {
224+
const { getByText } = render(<HelloWorld name="Vitest" />)
225+
await expect.element(getByText('Hello Vitest!')).toBeInTheDocument()
226+
})
227+
`,
228+
}
229+
194230
const vanillaExample = {
195231
name: 'HelloWorld.js',
196232
js: `
@@ -251,6 +287,8 @@ function getExampleTest(framework: string) {
251287
return litExample
252288
case 'marko':
253289
return markoExample
290+
case 'qwik':
291+
return qwikExample
254292
default:
255293
return vanillaExample
256294
}

0 commit comments

Comments
 (0)