Search code examples
htmlnext.jsmockingvitesttrpc

Vitest failing when calling tRPC createCaller because the content contains invalid JS syntax (add "**/*.html" to `assetsInclude`)


I'm writing unit tests using Vitest and calling the tRPC createCaller in order to mock the trpc context. When running the tests it's failing on the following error:

Error: Failed to parse source for import analysis because the content contains invalid JS syntax. You may need to install appropriate plugins to handle the .html file format, or if it's an asset, add "**/*.html" to assetsInclude in your configuration.

The solution is to add assetsInclude: ['**/*.html'] to your vitest.config.js, or remove the html assets you might have.

export default defineConfig({
  plugins: [react],
  resolve: {
    alias,
  },
  
  test: {
    globals: true,
    exclude: [...configDefaults.exclude, '/.next/'],
    setupFiles: './tests/setup.ts',
  },
})

Solution

  • The solution is to add assetsInclude: ['**/*.html'] to your vitest.config.js, or remove the html assets you might have.

    export default defineConfig({
      plugins: [react],
      resolve: {
        alias,
      },
      
      test: {
        globals: true,
        exclude: [...configDefaults.exclude, '/.next/'],
        setupFiles: './tests/setup.ts',
      },
    })