jeasx / jsx-async-runtime

An asynchronous JSX runtime without dependencies to be used as server-side template engine.
Other
9 stars 1 forks source link

Rendering in Vitest results in `undefined (reading 'call')` #3

Closed pauleveritt closed 2 weeks ago

pauleveritt commented 2 weeks ago

Let me know if you need a repo. I'm trying out 0.4. This code works fine in foo.tsx when run from tsx:

import { jsxToString } from "jsx-async-runtime";

function Content() {
  return <div>yes</div>;
}

const content = <Content />;
const result = await jsxToString(content);

console.log(result);

When I put this in a Vitest test:

import { test } from "vitest";
import { jsxToString } from "jsx-async-runtime";

test("renders foo", async () => {
  function Content() {
    return <div>yes</div>;
  }

  const content = <Content />;
  const result = await jsxToString(content);
});

I get:

TypeError: Cannot read properties of undefined (reading 'call')
    at Module.o (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/jsx-async-runtime/dist/esm/index.js:113:29)
    at Module.o (file:///Users/pauleveritt/projects/pauleveritt/www.pauleveritt.org/node_modules/jsx-async-runtime/dist/esm/index.js:107:33)

It fails on this line...but the second time through. The first time, t.tag is function Content(). I continue and at the breakpoint the second time, there is no t.tag. Screenshot attached of what it is stopped on.

bug

jablonski commented 2 weeks ago

Can you provide a repo so I can check with Vitest? Seems strange...

pauleveritt commented 2 weeks ago

Here's a repo with some instructions in README: https://github.com/pauleveritt/vitest-jsx-async-runtime

jablonski commented 2 weeks ago

Repo seems to be private...;-)

pauleveritt commented 2 weeks ago

@jablonski Crap, that's the second time I've done this. I make the repo from WebStorm and forget it defaults to private. Fixed, sorry.

jablonski commented 2 weeks ago

Hi Paul,

thanks for reporting.

Vitest injects an empty object ({}) as JsxElement, which wasn't handled properly by jsx-async-runtime. This is fixed in 0.4.1.

IMHO injecting an empty object as JsxElement looks like a Vitest bug too me. At least I cannot think of any good reason for this behaviour, but it is good to have it covered in jsx-async-runtime anyway.

pauleveritt commented 2 weeks ago

I'm happy to say, I plugged it in, all my tests are passing.