Skip to content

in vitest browser runner plugin with transformIndexHtml to inject importmap script manually does not work. #6394

@sashafirsov

Description

@sashafirsov

Describe the bug

Need the importmap available for non-js resources like SVG and HTML to be resolved natively by import.meta.resolve( path ).

For some reasin in vitest browser runner the usual plugin with transformIndexHtml to inject importmap script manually does not work.

Advised in vitejs/vite#2483 method of injecting script via browser.testerScripts is not applicable as a) import maps requires script type="importmap", b) it is injected after stateScript which causes "import maps injected after module use" error. Following request, openingthe bug on vitest.

Root cause:

  • Declarative Custom Element is HTML-centric and needs support for importmap for use by import.meta.resolve( path ) API. Vitest in browser does not support import maps.
  • vitest browser run uses static HTML instead of loading by vite build and altering it afterwards
  • using vitest to run the StoryBook interactive tests for TDD on visual components (React and Web Components). The StoryBook is not supported by vitest browser run, had to do a bit of tricks.

Solving on any level from $subj to root causes, better all, would be beneficial for frameworks and reusable libs authors.

Reproduction

<script type="importmap">
{
    "imports": {
        "embed-lib": "./demo/lib-dir/embed-lib.html"
    }
}
</script>
const url = import.meta.resolve( 'embed-lib' );
expect( url ).to.include( 'embed-lib.html' );

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i7-1260P
    Memory: 1.31 GB / 15.69 GB
  Binaries:
    Node: 20.8.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.22 - C:\Program Files\nodejs\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.0.6 - C:\Program Files\nodejs\pnpm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    @vitest/browser: ^1.6.0 => 1.6.0
    @vitest/coverage-istanbul: ^1.6.0 => 1.6.0
    vite: ^5.3.1 => 5.3.1
    vitest: ^1.6.0 => 1.6.0

Used Package Manager

yarn

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat: browserIssues and PRs related to the browser runnerp2-nice-to-haveNot breaking anything but nice to have (priority)

    Type

    No type

    Projects

    Status

    Approved

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions