jest-community / jest-runner-eslint

An ESLint runner for Jest
MIT License
480 stars 46 forks source link

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

Open jakeherp opened 4 years ago

jakeherp commented 4 years ago

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

ljharb commented 4 years ago

Are you able to run eslint by itself?

jakeherp commented 4 years ago

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

ljharb commented 4 years ago

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

kieran-osgood commented 4 years ago

@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.

lolero commented 1 year ago

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