bug: `Fragment` is undefined in .tsx files without Stencil decorators

Open
#104 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
frontend, testing

Research direction

Start with stencilVitestPlugin() and the rule that decides which .tsx files go through the Stencil compiler. The repro is a helper module that imports Fragment and h from @stencil/core with no decorators, and it fails because the public entry does not export Fragment at runtime. Done means a *.unit.tsx test that renders <Pair /> from such a helper shows both spans, without the vi.mock workaround in the issue.

Written by the indexing model from the issue text.

Description

Prerequisites
  • I have searched for existing issues that already report this problem, without success.
Package Version

@stencil/vitest 1.16.1 (plugin/source mode, stencilVitestPlugin())

Stencil Version

@stencil/core 4.44.1

Vitest Version

vitest 5.0.3, environment stencil, domEnvironment mock-doc

Reproduction URL

N/A

Describe the Bug

In plugin mode, only .tsx files with Stencil decorators (@Component etc.) go through the Stencil
compiler. Helper modules that only export functional components using <Fragment>
(import { Fragment, h } from '@stencil/core') are left to esbuild. The public @stencil/core entry
does not export Fragment at runtime (the compiler normally rewrites the import), so Fragment is
undefined and rendering fails.

Workaround

// vitest setup file
vi.mock('@stencil/core', async importOriginal => {
  const { Fragment } = await import('@stencil/core/internal/client');
  return { ...(await importOriginal<object>()), Fragment };
});

Suggested fix

Transform every .tsx that imports from @stencil/core, not only decorated ones, or resolve
@stencil/core to a runtime entry that exports Fragment (and h, Host).

Steps to Reproduce
  1. util.tsx:
    import { Fragment, h } from '@stencil/core';
    export const Pair = () => <Fragment><span>a</span><span>b</span></Fragment>;
    
  2. A @Component that renders <Pair />.
  3. A *.unit.tsx test: await render(<my-cmp />) with stencilVitestPlugin().
Expected Behavior

Expected: both spans render, as with stencil build.
Actual: Fragment is undefined in util.tsx.

Logs

Dominant language
TypeScript
Stars
6
Forks
6
Avg merge
8d 19h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stenciljs/vitest

All issues in stenciljs/vitest

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.