kit icon indicating copy to clipboard operation
kit copied to clipboard

Use `$lib` in tests files too

Open frederikhors opened this issue 2 years ago • 4 comments

Describe the problem

It would be very useful to use $lib in playwright tests files too.

Error: Cannot find package '$lib' imported from C:\project\src\lib\helpers.ts

Describe the proposed solution

Rich's Magic

Alternatives considered

Wait for Godot

Importance

would make my life easier

Additional Information

No response

frederikhors avatar Jan 26 '23 21:01 frederikhors

Judging from https://github.com/microsoft/playwright/pull/10525 and https://github.com/microsoft/playwright/issues/16164 this should already exist. Could you give a reproducible?

dummdidumm avatar Jan 27 '23 10:01 dummdidumm

https://github.com/WilliamHua/lib-sveltekit-bug-playwright

It works using version 1.1.1 of SvelteKit, but not 1.1.2.

I suspect it's because of this change: https://github.com/sveltejs/kit/pull/8568

You can temporarily work around this by modifying your tsconfig.json and adding

{
	"extends": "./.svelte-kit/tsconfig.json",
	"compilerOptions": {
                ...
		"baseUrl": ".",
		"paths": {
			"$lib": [
				"src/lib"
			],
			"$lib/*": [
				"src/lib/*"
			]
		}
	}
}

WilliamHua avatar Jan 28 '23 17:01 WilliamHua

Same issue with $env, but can't really use the solution above. Do we have any other way to access env in dev? (dotenv is probably the way to go)

Smirow avatar Feb 06 '23 19:02 Smirow

https://github.com/WilliamHua/lib-sveltekit-bug-playwright

It works using version 1.1.1 of SvelteKit, but not 1.1.2.

I suspect it's because of this change: #8568

You can temporarily work around this by modifying your tsconfig.json and adding

{
	"extends": "./.svelte-kit/tsconfig.json",
	"compilerOptions": {
                ...
		"baseUrl": ".",
		"paths": {
			"$lib": [
				"src/lib"
			],
			"$lib/*": [
				"src/lib/*"
			]
		}
	}
}

not working in playwright. still throws cannot find $lib

TadasV1 avatar May 18 '23 04:05 TadasV1

any update on this? trying to use playwrights component testing and getting this same error.

iamthe-Wraith avatar Jul 11 '23 23:07 iamthe-Wraith

has anyone been able to resolve this? even just a hacky work around?

iamthe-Wraith avatar Jul 23 '23 17:07 iamthe-Wraith

The following workaround does it for me (as mentioned by William). For whatever reason it only works when the baseUrl option is included. /tsconfig.json

{
	"extends": "./.svelte-kit/tsconfig.json",
	"compilerOptions": {
        ...
		"baseUrl": ".",
		"paths": {
			"$lib": [
				"./src/lib"
			],
			"$lib/*": [
				"./src/lib/*"
			]
		},
	},
}

eltigerchino avatar Jul 24 '23 07:07 eltigerchino

The following workaround does it for me (as mentioned by William). For whatever reason it only works when the baseUrl option is included. /tsconfig.json

This worked for me, thank you!

iamthe-Wraith avatar Aug 03 '23 00:08 iamthe-Wraith

I'm running into the same issue with accessing $env from tests, just like @Smirow mentioned. Is there a workaround for this?

coryvirok avatar Aug 26 '23 22:08 coryvirok

I'm also running into the same issue within a test.

DylanStandridge avatar Sep 15 '23 18:09 DylanStandridge

@s3812497 @WilliamHua what about this?

Error: Cannot find package '$app' imported from ...?

frederikhors avatar Sep 26 '23 18:09 frederikhors

what about this?

Error: Cannot find package '$app' imported from ...?

See https://github.com/sveltejs/kit/issues/1485

eltigerchino avatar Sep 26 '23 18:09 eltigerchino

@s3812497 I don't know what to read for fixing this.

frederikhors avatar Sep 26 '23 18:09 frederikhors

It doesn't seem like there's a straight-forward fix at the moment. There are a few examples in that issue of people mocking the $app module for their testing frameworks.

eltigerchino avatar Sep 26 '23 18:09 eltigerchino

I can use:

"$app": [
	"./node_modules/@sveltejs/kit/src/runtime/app"
],
"$app/*": [
	"./node_modules/@sveltejs/kit/src/runtime/app/*"
],
"$env": [
	"./node_modules/@sveltejs/kit/src/runtime/env"
],
"$env/*": [
	"./node_modules/@sveltejs/kit/src/runtime/env/*"
]

but for example $env/static/public is a problem.

frederikhors avatar Sep 26 '23 19:09 frederikhors

I'm using this and it seems to work:

// tsconfig.json
{
  "extends": "./.svelte-kit/tsconfig.json",
  "compilerOptions": {
    // Everything below is required for Playwright tests to access path aliases
    "baseUrl": ".",
    "paths": {
      "$lib": ["src/lib"],
      "$lib/*": ["src/lib/*"],
      "$app/*": ["tests/e2e/mocks/app/*"],
      "$env/*": ["tests/e2e/mocks/env/*"]
    }
  }
}
// tests/e2e/mocks/env/app/dynamic/private.ts
export const env = process.env

coryvirok avatar Sep 27 '23 00:09 coryvirok

@coryvirok Is there a way to put tsconfig.json into tests/ dir and not in the root one?

frederikhors avatar Sep 28 '23 08:09 frederikhors

I haven't tried it, but it looks like you'll need to compile separately:

from https://playwright.dev/docs/test-typescript

{
  "scripts": {
    "pretest": "tsc --incremental -p tests/tsconfig.json",
    "test": "playwright test -c tests-out"
  }
}

coryvirok avatar Sep 28 '23 09:09 coryvirok

I'm using this and it seems to work:

// tsconfig.json
{
  "extends": "./.svelte-kit/tsconfig.json",
  "compilerOptions": {
    // Everything below is required for Playwright tests to access path aliases
    "baseUrl": ".",
    "paths": {
      "$lib": ["src/lib"],
      "$lib/*": ["src/lib/*"],
      "$app/*": ["tests/e2e/mocks/app/*"],
      "$env/*": ["tests/e2e/mocks/env/*"]
    }
  }
}
// tests/e2e/mocks/env/app/dynamic/private.ts
export const env = process.env

I cannot make this work. Can you please push a repo to github, @coryvirok?

frederikhors avatar Oct 08 '23 17:10 frederikhors

For sure. Here is the SvelteKit skeleton app with upgraded deps + env mocks for Playwright.

https://github.com/coryvirok/tmp-mock-env-sveltekit

coryvirok avatar Oct 08 '23 20:10 coryvirok

Thanks. The issue is that if I use this tsconfig.json:

{
  "extends": "./.svelte-kit/tsconfig.json",
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "baseUrl": ".",
    "paths": {
      "$lib": ["./src/lib"],
      "$lib/*": ["./src/lib/*"],
      "$app": ["./node_modules/@sveltejs/kit/src/runtime/app"],
      "$app/*": ["./node_modules/@sveltejs/kit/src/runtime/app/*"],
      "$env": ["./tests/e2e/mocks/env"],
      "$env/*": ["./tests/e2e/mocks/env/*"]
    }
  }
}

I'm getting this error:

Error: Cannot find module 'C:\project\node_modules\@tanstack\svelte-query\build\lib\types' imported from C:\project\node_modules\@tanstack\svelte-query\build\lib\index.js

I suspect the issue is caused by "baseUrl": ".". If I don't use it I get other errors like cannot find $lib.

@ tanstack\svelte-query\build\lib\index.js is:

/* istanbul ignore file */
// Re-export core
export * from '@tanstack/query-core';
// Svelte Query
export * from './types';
export * from './context';
export { createQuery } from './createQuery';
export { createQueries } from './createQueries';
export { createInfiniteQuery } from './createInfiniteQuery';
export { createMutation } from './createMutation';
export { useQueryClient } from './useQueryClient';
export { useIsFetching } from './useIsFetching';
export { useIsMutating } from './useIsMutating';
export { useHydrate } from './useHydrate';
export { default as Hydrate } from './Hydrate.svelte';
export { default as QueryClientProvider } from './QueryClientProvider.svelte';

and @ tanstack\svelte-query\build\lib\types is:

export {};

I don't know what to do.

frederikhors avatar Oct 09 '23 11:10 frederikhors

I'm not familiar with the svelte query lib but here's what I would recommend:

Add another file to your mocks folder called app.ts. From there, export whichever vars are used by your app at build and runtime. I do this for the building var. Then, rename the path aliases in tsconfig for $app to point to this new mock, just like the $env var does.

Your baseUrl looks correct.

coryvirok avatar Oct 09 '23 17:10 coryvirok

Hello same issue here for me. Any advancement on the subject ? @frederikhors have you managed to make it work since ?

sowdowdow avatar Oct 18 '23 10:10 sowdowdow

have you managed to make it work since ?

Nope. We are all waiting for a fix.

frederikhors avatar Oct 18 '23 10:10 frederikhors

Workaround ($env only)

import { connect } from "@planetscale/database";
import { drizzle } from "drizzle-orm/planetscale-serverless";
import 'dotenv/config'

var DB_HOST = process.env.DB_HOST
var DB_PASSWORD = process.env.DB_PASSWORD
var DB_USERNAME = process.env.DB_USERNAME

const connection = connect({
  host: DB_HOST,
  username: DB_USERNAME,
  password: DB_PASSWORD,
});

export const db = drizzle(connection);

sowdowdow avatar Oct 18 '23 11:10 sowdowdow

Another workaround, run Playwright in Vitest

https://www.the-koi.com/projects/how-to-run-playwright-within-vitest/

This is working well for me

oscarhermoso avatar Nov 03 '23 06:11 oscarhermoso

Looks like this became even more relevant to fix now in SvelteKit 2 due to https://kit.svelte.dev/docs/migrating-to-sveltekit-2#generated-tsconfig-json-is-more-strict

mcmxcdev avatar Dec 17 '23 18:12 mcmxcdev

This is relevant https://playwright.dev/docs/test-components#i-have-a-project-that-already-uses-vite-can-i-reuse-the-config

Playwright-for-Svelte uses its own vite internally, so it has a way to inject configs to it

import { defineConfig } from '@playwright/experimental-ct-svelte';

export default defineConfig({
  use: {
    ctViteConfig: {
      // ...
    },
  },
});

Keep in mind this is not standard playwright (@playwright/test), it's an official wrapper configured to test Svelte components

madacol avatar Mar 23 '24 10:03 madacol

@madacol I cannot make it work with this neither.

frederikhors avatar Apr 16 '24 14:04 frederikhors

I ended up using a relative import.

phcoliveira avatar Apr 16 '24 14:04 phcoliveira