Search code examples
typescriptplaywrightsveltekit

Import needs to have explicit .ts if file is being imported other than from /src/lib in Sveltekit


I'm trying to write some tests using Playwright inside /tests folder. I'd like to use some helper functions which I could put under /tests/lib/helpers.

  • Tests throws module not found error if .ts extension is not explicitly specified in the import.
  • If it is, on the other hand, then IDE/Eslint complains: TS2691: An import path cannot end with a '.ts' extension. Consider importing './lib/helpers/fresh-storage.js' instead. Though the scripts are being imported, successfully compiled and are functional.

If I would be to add this helper function under /src/lib and import them in /tests there would be no problems. So I assume I should be able change some config somewhere to be able to import typescript modules from /tests just like I can from /src/lib at the moment.


Solution

  • Alias has to be set in svelte.config.js

    // svelte.config.js
    
    const config = {
        kit: {
            ...
            alias: {
                '$tests': 'tests',
                '$tests/*': 'tests/*',
            }
        }
    };