Sveltekit - Jest cannot use import statement outside a module

Viewed 283

I am currently facing a problem with Jest in a sveltekit + typescript project that I cannot solve.

I use the svelte-fa library for displaying icons. The project runs correctly and also jest works correctly as long as I don't test any files that contain this library. Otherwise, the following error is triggered:

/Users/username/documents/projectname/node_modules/svelte-fa/src/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Fa from './fa.svelte';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module
       7 |      import Fa from 'svelte-fa';
       8 |
    >  9 |      export const { id, header, msg, alertType } = $$props;
         |                             ^
      10 |
      11 |      const iconClass = {
      12 |              error: 'text-red-600',

My jest configuration is

module.exports = {
    transform: {
        '^.+\\.ts$': 'ts-jest',
        '^.+\\.js$': 'ts-jest',
        '^.+\\.svelte$': ['svelte-jester', { preprocess: './svelte.config.test.cjs' }]
    },
    moduleFileExtensions: ['js', 'ts', 'svelte'],
    moduleNameMapper: {
        '^\\$lib(.*)$': '<rootDir>/src/lib$1',
        '^\\$app(.*)$': [
            '<rootDir>/.svelte-kit/dev/runtime/app$1',
            '<rootDir>/.svelte-kit/build/runtime/app$1'
        ],
        '^.+\\.(css|less|scss)$': 'babel-jest'
    },
    setupFilesAfterEnv: ['<rootDir>/jest-setup.ts'],
    collectCoverageFrom: ['src/**/*.{ts,tsx,svelte,js,jsx}'],
    //transformIgnorePatterns: ['node_modules/(?!svelta-fa/.*)']
};

and my svelte.config.test.cjs

const preprocess = require('svelte-preprocess');
module.exports = { preprocess: preprocess() };

As i understand, after reading other problems with this error, i think it's a problem with translating that library from typescript to javascript but i can not manage to solve it. I also tried to add transformIgnorePatterns but it's still not working

0 Answers
Related