Skip to content
Open
Show file tree
Hide file tree
Changes from 17 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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@
"esbuild": "0.*"
},
"devDependencies": {
"@builder.io/qwik": "^1.12.1",
"@noma.to/qwik-testing-library": "^1.3.2",
"@testing-library/preact": "3.2.4",
"@testing-library/react": "^14.1.0",
"@testing-library/vue": "8.1.0",
"@types/jsdom": "^21.1.5",
"@types/jsdom-global": "^3.0.7",
"@types/mdx": "^2.0.10",
"@types/react": "^18.2.37",
"@types/react-dom": "^18.2.15",
Expand All @@ -67,6 +70,7 @@
"esbuild": "^0.19.5",
"hono": "4.6.14",
"jsdom": "^22.1.0",
"jsdom-global": "^3.0.2",
"kcd-scripts": "^14.0.1",
"left-pad": "^1.3.0",
"mdx-test-data": "^1.0.1",
Expand Down
106 changes: 106 additions & 0 deletions src/__tests__/qwik.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* eslint-disable react/no-unknown-property */
/* eslint-disable react/react-in-jsx-scope */
/** @jsxImportSource @builder.io/qwik */

import * as Qwik from "@builder.io/qwik";
import {suite} from 'uvu'
import * as assert from 'uvu/assert'
import { render } from '@noma.to/qwik-testing-library'
import {bundleMDX} from '../index.js'
import {getMDXComponent} from '../client/jsx.js';

/**
* NOTE: Qwik v2 will change the package name from '@builder.io/qwik' to '@qwik.dev/core'.
*
* It's the Qwik team's responsibility to update the package name in the test once v2 has released. (Jack, a core team member has made the initial PR to this package, and will update this test once v2 has released.)
*/

const test = suite("qwik");

const jsxBundlerConfig = {
jsxLib: {
varName: 'Qwik',
package: '@builder.io/qwik',
},
jsxRuntime: {
varName: '_jsx_runtime',
package: '@builder.io/qwik/jsx-runtime',
},
}

const jsxComponentConfig = {
Qwik,
_jsx_runtime: {
jsx: Qwik.jsx,
jsxs: Qwik.jsx,
Fragment: Qwik.Fragment
}
}

const mdxSource = `
---
title: Example Post
published: 2021-02-13
description: This is some meta-data
---
import { Demo } from './demo'

# This is the title

Here's a **neat** demo:
<Demo />
`.trim();

const demoTsx = `
import { component$ } from '@builder.io/qwik'

export const Demo = component$(() => {
return <div>mdx-bundler with Qwik's runtime!</div>
})
`.trim();

test('smoke test for qwik', async () => {
const result = await bundleMDX({
source: mdxSource,
jsxConfig: jsxBundlerConfig,
files: {
'./demo.tsx': demoTsx
}
});

/**
* @type {any}
*/
const Component = getMDXComponent(result.code, jsxComponentConfig)

/** @type {Qwik.Component<{}>} */
const SpanBold = Qwik.component$(() => {
return Qwik.jsx('span', { class: "strong", children: Qwik.jsx(Qwik.Slot, { name: "" }) })
})

assert.equal(result.frontmatter, {
title: 'Example Post',
published: new Date('2021-02-13'),
description: 'This is some meta-data',
})

const {container} = await render(
Qwik.jsx(Component, { components: { strong: SpanBold } })
)

/**
* Qwik v1 uses HTML comments as markers in its output for component boundaries and resumability.
* When Qwik v2 is released, the expected output will change to be more similar to other frameworks,
* but with distinctive dynamic and constant attributes (denoted by the ':' character).
* This test will need to be updated accordingly when migrating to v2.
*/

assert.equal(
container.innerHTML,
`<h1>This is the title</h1>
<p>Here's a <!--qv --><span class="strong"><!--qv q:key q:sref=0 q:s-->neat<!--/qv--></span><!--/qv--> demo:</p>
<!--qv --><div>mdx-bundler with Qwik's runtime!</div><!--/qv-->`
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would understand if we want to keep it that way, but to prevent having to update the test once we migrate to qwik V2, we could use queries to match the expected results instead of the raw inner html.

For example:

  const element = await render(
    Qwik.jsx(Component, { components: { strong: SpanBold } })
  )
  
  assert.ok(element.getByRole("heading", { name: "This is the title" }))
  assert.ok(element.getByText("Here's a neat demo: mdx-bundler with Qwik's runtime!"))

Or something along those lines.

Copy link
Author

@thejackshelton thejackshelton Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this should also work, since the v1 import is compatible in v2. I'll update this soon 🫡 . Appreciate the feedback!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually @ianlet I'm getting some linter issues from the react testing library that it's bad practice to not destructure here.

But when I try to use screen, it becomes hard to select the h1 for the qwik smoke test

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah no worries! In any case, as we're testing the result of a transformation, it makes sense to expect the exact output. It's just that it makes this test more fragile and that we'll have to remember to come fix it as soon as Qwik internals change something (shouldn't happen often though).

})

test.run()
7 changes: 2 additions & 5 deletions src/__tests__/setup-tests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import jsdomPkg from 'jsdom'
import jsdomGlobal from 'jsdom-global';
jsdomGlobal();
process.env.NODE_ENV = 'test'

const {JSDOM} = jsdomPkg
Expand All @@ -16,11 +18,6 @@ function copyProps(src, target) {
})
}

// @ts-expect-error TS2322 🤷‍♂️
global.window = window
global.document = window.document
// @ts-expect-error TS2740 🤷‍♂️
global.navigator = {userAgent: 'node.js'}
global.requestAnimationFrame = callback => setTimeout(callback, 0)
global.cancelAnimationFrame = id => clearTimeout(id)
copyProps(window, global)