jest-runner-eslint icon indicating copy to clipboard operation
jest-runner-eslint copied to clipboard

'Jest encountered an unexpected token' when set up as jest projects

Open jakeherp opened this issue 4 years ago • 5 comments

I am getting the message

'Jest encountered an unexpected token'

for all my unit tests when setting up jest-runner-eslint in projects:

module.exports = {
  roots: ['<rootDir>'],
  moduleNameMapper: {
    '.+\\.(css|styl|less|sass|scss)$': 'identity-obj-proxy',
    '.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/file-mock.js',
    '^Atoms(.*)$': '<rootDir>/src/components/atoms$1',
    '^Molecules(.*)$': '<rootDir>/src/components/molecules$1',
    '^Organisms(.*)$': '<rootDir>/src/components/organisms$1',
    '^Templates(.*)$': '<rootDir>/src/components/templates$1',
    '^Context(.*)$': '<rootDir>/src/context$1',
    '^Data(.*)$': '<rootDir>/src/data$1',
    '^Helpers(.*)$': '<rootDir>/src/helpers$1',
    '^Hooks(.*)$': '<rootDir>/src/hooks$1',
  },
  testPathIgnorePatterns: ['node_modules', '.cache', 'public'],
  transformIgnorePatterns: ['node_modules/(?!(gatsby)/)'],
  globals: {
    __PATH_PREFIX__: '',
  },
  transform: {
    '^.+\\.[jt]sx?$': '<rootDir>/jest-preprocess.js',
  },
  testURL: 'http://localhost',
  setupFiles: ['<rootDir>/loadershim.js'],
  collectCoverageFrom: ['**/src/**/*.+(js|jsx|ts|tsx)'],
  coverageThreshold: {
    global: {
      statements: 25,
      branches: 25,
      functions: 30,
      lines: 30,
    },
  },
  watchPlugins: [
    'jest-watch-typeahead/filename',
    'jest-watch-typeahead/testname',
  ],
  projects: [
    {
      displayName: 'test',
    },
    {
      displayName: 'lint',
      runner: 'jest-runner-eslint',
      testMatch: ['<rootDir>/src/**/*.js'],
    },
  ],
};

Removing projects results in all tests passing, so I assume it has to do with my implementation? This is within a Gatsby site following their implementation for TypeScript: https://www.gatsbyjs.org/docs/unit-testing/#using-typescript

jakeherp avatar Apr 13 '20 17:04 jakeherp

Are you able to run eslint by itself?

ljharb avatar Apr 13 '20 18:04 ljharb

Yes, eslint runs just fine on its own, it also runs within Jest if run alone, but not within projects.

jakeherp avatar Apr 14 '20 07:04 jakeherp

Should transform be applying to all projects, and moduleNameMapper? it seems like a lot of those should be moved down into the tests project.

ljharb avatar Apr 15 '20 06:04 ljharb

@jakeherp don't suppose you ever found a solution for this? I have the same issue that without the projects configuration set the tests run, if i set the projects with just the lint config it runs the linter, but having the test project set will cause the tests to fail with unexpected token (linting still runs)? Specifically the error it states is SyntaxError: Cannot use import statement outside a module pointing to the import of react-native, I've tried adding this to the transformIgnorePatterns etc, no idea what to do with it. Surely something weird going on considering the tests all run fine if i run jest from command line or indeed without the test project set in the config.

My project is react-native (expo bare workflow) + typescript. I created a fresh rn-expo project without typescript and it still occurred mind you.

kieran-osgood avatar Jul 26 '20 15:07 kieran-osgood

I have the same issue and have been trying to find a solution for days without success :(

lolero avatar Feb 13 '23 09:02 lolero