Description
Hey Team,
I have been spending some time already in trying to get svelte-testing-library to work in my monorepo.
However I am failing to do so. I will try to outline the details as best as I can below.
The error message.
When I run my test nx test ui-component
(which is just a nx command for running the vitest command). I get the following error.
FAIL src/lib/Divider.spec.ts > should render correctly
Svelte error: rune_outside_svelte
The `$state` rune is only available inside `.svelte` and `.svelte.js/ts` files
When I remove this $state part from the render function inside modern.svelte.js (line 26( I get the following error:
FAIL src/lib/Divider.spec.ts > should render correctly
Svelte error: lifecycle_function_unavailable
`mount(...)` is not available on the server
To me this sounds like something is wrong with getting svelte to load correctly. I am in the myst about what I should do, and how to fix it. I have tried looking thoroughly at my vite configs, tsconfigs, etc. But I cannot copy the tutorials one on one as we're using Nx.
Below I will share some code / config
The test (nothing wrong with this, it works in other setups):
import { render, screen } from '@testing-library/svelte';
import { test } from 'vitest';
import '@testing-library/jest-dom/vitest';
import { Divider } from '../';
test('should render correctly', () => {
render(Divider);
expect(screen.queryByRole('button')).not.toBeInTheDocument();
});
The component itself (cannot be wrong I guess, works in Storybook)
<hr class="my-0 border-b border-t border-b-white/10 border-t-black/30" />
My vite.config.ts
/// <reference types='vitest' />
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { svelteTesting } from '@testing-library/svelte/vite';
import * as path from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig({
root: __dirname,
cacheDir: '../../node_modules/.vite/ui/divider',
plugins: [
svelte(),
svelteTesting(),
nxViteTsPaths({ buildLibsFromSource: false }),
dts({ entryRoot: 'src', tsconfigPath: path.join(__dirname, 'tsconfig.lib.json') }),
viteStaticCopy({
targets: [
{
src: path.join(__dirname, '**/*.md'),
dest: '.',
},
{
src: path.join(__dirname, '**/index.ts'),
dest: '.',
},
{
src: path.join(__dirname, '**/*.svelte'),
dest: './lib',
},
],
}),
],
// Uncomment this if you are using workers.
// worker: {
// plugins: [ nxViteTsPaths() ],
// },
// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
outDir: '../../dist/ui/divider',
emptyOutDir: true,
reportCompressedSize: true,
lib: {
// Could also be a dictionary or array of multiple entry points.
entry: 'src/index.ts',
name: 'ui-divider',
fileName: 'index',
// Change this to the formats you want to support.
// Don't forget to update your package.json as well.
formats: ['es'],
},
rollupOptions: {
// External packages that should not be bundled into your library.
external: ['svelte'],
},
},
test: {
watch: false,
globals: true,
environment: 'jsdom',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: '../../coverage/ui/divider',
provider: 'v8',
},
},
});
My tsconfig:
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"target": "es2017",
"verbatimModuleSyntax": true,
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
My packages:
"svelte": "5.0.0-next.246",
"@sveltejs/vite-plugin-svelte": "^4.0.0-next.7",
"vitest": "^1.3.1",
"@testing-library/svelte": "^5.2.3",
"@testing-library/jest-dom": "^6.5.0"
Do you have any idea why this error is popping up? And how I can fix it.