Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Unexpected token 'export' while running jest test cases #345

Closed
vikaspuri-goswami opened this issue Mar 4, 2021 · 12 comments
Closed

Comments

@vikaspuri-goswami
Copy link

SyntaxError: Unexpected token 'export' while running jest test cases.

bable.config.js

module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        targets: {
          node: "current",
        },
      },
    ],
  ],
}

Jest configuration in package.json

"jest": {
    "transform": {
      "^.+\\.js$": "babel-jest",
      "^.+\\.svelte$": "svelte-jester"
    },
    "moduleFileExtensions": [
      "js",
      "svelte"
    ],
    "setupFilesAfterEnv": [
      "@testing-library/jest-dom/extend-expect"
    ],
    "testPathIgnorePatterns": [
      "dist"
    ]
  },

Screenshot attached
jest error

@jakobrosenberg
Copy link
Member

Can you set script to ESM somehow?

@vikaspuri-goswami
Copy link
Author

@jakobrosenberg You can find the import statement in the first screenshot import { goto } from "@roxi/routify". We are using jest src in the npm run test command in pckage.json. Please let me know if you need anything else?

@jakobrosenberg
Copy link
Member

If I'm reading the screenshot correctly, you haven't configured Jest to handle ESM.

@tmaxmax
Copy link

tmaxmax commented Mar 11, 2021

Same error here, is this looked upon?

@jakobrosenberg
Copy link
Member

Does this help? #349

@vikaspuri-goswami
Copy link
Author

We would need to ignore transform patterns for ESM. Below are the sample code for the reference.

  1. Create array of ESM which we would need to ignore while transforming
  2. Add full path of those modules in transformIgnorePatterns.
const esModules = ["@roxi"].join("|")

module.exports = {
  transform: {
    "^.+\\.js$": "babel-jest",
    "^.+\\.svelte$": "svelte-jester",
  },
  moduleFileExtensions: ["js", "svelte"],
  setupFilesAfterEnv: ["@testing-library/jest-dom/extend-expect"],
  testPathIgnorePatterns: ["dist"],
  transformIgnorePatterns: [`/node_modules/(?!${esModules})`],
}

Let me know if any question, happy to help you.

@doylio
Copy link

doylio commented Mar 15, 2021

Same issue here

@ghostdevv
Copy link
Member

I think you need to configure Jest as Jake said to support esm. If you still having issues please reopen

@tobiasbueschel
Copy link

I think you need to configure Jest as Jake said to support esm. If you still having issues please reopen

Thanks for this @vikaspuri-goswami!

Unfortunately, neither the jest.config.ts configuration to ignore the routify module nor the ESM approach worked for me. I still get the same error:
image

Dependency versions used:

        "@roxi/routify": "^2.18.5",
        "@testing-library/jest-dom": "^5.16.2",
        "@testing-library/svelte": "^3.0.3",
        "@tsconfig/svelte": "^3.0.0",
        "jest": "^27.5.1",
        "svelte": "^3.46.4",
        "svelte-check": "^2.4.5",
        "svelte-jester": "^2.3.2",
        "svelte-preprocess": "^4.10.4",
        "ts-jest": "^27.1.3",
        "ts-node": "^10.7.0",
        "tslib": "^2.3.1",
        "typescript": "^4.6.2",

jest.config.ts

import type { Config } from '@jest/types';

const config: Config.InitialOptions = {
    preset: 'ts-jest',
    testEnvironment: 'jsdom',
    verbose: true,
    transform: {
        '^.+\\.svelte$': [
            'svelte-jester',
            {
                preprocess: true
            }
        ],
        '^.+\\.ts$': 'ts-jest',
        '^.+\\.js$': 'ts-jest',
        '.+\\.(css|styl|less|sass|scss)$': 'jest-css-modules-transform'
    },
    setupFilesAfterEnv: ['@testing-library/jest-dom/extend-expect'],
    moduleFileExtensions: ['js', 'ts', 'svelte'],
    transformIgnorePatterns: ['<rootDir>/node_modules/(?!@roxi)']
};

export default config;

I've also tried:

"test": "NODE_OPTIONS=--experimental-vm-modules jest src",

I'll share back if I find a fix - if someone else has any suggestions in the meantime, would greatly appreciate it 🙏

Thanks

cc @ghostdevv

@TimoReusch
Copy link

@tobiasbueschel I'm facing the same problem - have you found a solution meanwhile?

@ghostdevv
Copy link
Member

I would recommend migrating to Vitest, for the most part it should be a drop in replacement for Jest as it supports the Jest mock syntax OOTB

@aarzootrehannagarro
Copy link

Any solution for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants