Jest encountered an unexpected token during upgrade Svelte v3 to v4

85 Views Asked by At

I'm stucked during the upgrade of svelte v3 to v4 using the official guide. My tests are failing and I've tried multiple ways to fix it but nothing worked here for me. Here some details about my svelte app.

// jest.config.js
module.exports = {
    testEnvironment: "jsdom",
    transform: {
        '^.+\\.js$': 'babel-jest',
        '^.+\\.svelte$': 'svelte-jester'
    }
};

//package.json
Package.json
{
  "name": "svelte-app",
  "version": "1.0.0",
  "devDependencies": {
    "@babel/core": "7.23.2",
    "@babel/preset-env": "7.23.2",
    "@ironkinoko/rollup-plugin-styles": "4.0.3",
    "@rollup/plugin-commonjs": "25.0.7",
    "@rollup/plugin-node-resolve": "15.2.3",
    "@rollup/plugin-replace": "5.0.5",
    "@rollup/plugin-terser": "0.4.4",
    "@testing-library/dom": "7.31.2",
    "@testing-library/jest-dom": "5.17.0",
    "@testing-library/svelte": "^4.0.0",
    "babel-jest": "29.7.0",
    "jest": "29.7.0",
    "jest-environment-jsdom": "29.7.0",
    "rollup": "4.2.0",
    "rollup-plugin-livereload": "2.0.5",
    "rollup-plugin-svelte": "7.1.6",
    "svelte": "^4.0.0",
    "svelte-jester": "3.0.0",
    "testing-library-table-queries": "0.2.0"
  },
  "dependencies": {
    "sirv-cli": "2.0.2",
    "svelte-spinner": "2.0.2"
  },
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public",
    "test": "jest",
    "test-watch": "jest --watchAll"
  }
 

Exception:

FAIL  src/EnummerEditor.spec.js
  ● Test suite failed to run
 
    Jest encountered an unexpected token
 
    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
 
    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
 
    By default "node_modules" folder is ignored by transformers.
 
    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
 
    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation
 
    Details:
 
    /Users/user123/code/zapp/zapp-config-ui/node_modules/@testing-library/svelte/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { act, cleanup } from './pure.js'
                                                                                      ^^^^^^
 
    SyntaxError: Cannot use import statement outside a module
 
      1 | import '@testing-library/jest-dom/extend-expect';
    > 2 | import {render, fireEvent, screen} from '@testing-library/svelte';
        | ^
      3 | import EnummerEditor from './EnummerEditor.svelte';
      4 |
      5 | describe('EnummerEditor', () => {
 
      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1505:14)
      at Object.require (src/EnummerEditor.spec.js:2:1)
 

Any idea or pointing to some resources would be helpful.

0

There are 0 best solutions below