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

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

Open
jakeherp opened this issue Apr 13, 2020 · 5 comments
Open

Comments

@jakeherp
Copy link

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
Copy link
Collaborator

ljharb commented Apr 13, 2020

Are you able to run eslint by itself?

@jakeherp
Copy link
Author

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

@ljharb
Copy link
Collaborator

ljharb commented Apr 15, 2020

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
Copy link

kieran-osgood commented Jul 26, 2020

@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
Copy link

lolero commented Feb 13, 2023

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

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

4 participants