While running jest tests, coverage fails with Unexpected token

Asked
Active3 hr before
Viewed126 times

8 Answers

unexpectedtokentestsrunning
90%

Recently I had the same issue. As I investigated, it was cause by my tsconfig The line jsx:"preserve" mean, that your TSX will be compiled to JSX, but Jest does not understand JSX w/o and preprocessing, What motivated the Indian supreme court to legalize homosexuality? ,Connect and share knowledge within a single location that is structured and easy to search.

Recently I had the same issue. As I investigated, it was cause by my tsconfig The line jsx:"preserve" mean, that your TSX will be compiled to JSX, but Jest does not understand JSX w/o and preprocessing

compilerOptions: {
   //...
   "jsx": "preserve"
}
load more v
88%

Somehow the coverage generator is trying to parse the package.json and outputs this error., Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. ,I personally encounter this issue about once a day. It seems to happen when you have a non-.js file that has uncommitted changes, and you have coverage and watch/watchAll enabled.

load more v
72%

The "issue" is that Jest only wants to process CommonJS-style code. So for the Jest tests to run, it first needs to be transpiled by Babel. If you don't get it properly transpiled, you'll see an error like this: ,Many of the threads are long. This isn't one of those issues where a quick answer solves the problem for the original poster.,NOTE: This is not .babelrc. In this particular setup, I appear to have needed the file to be babel.config.json.

Jest encountered an unexpected token
load more v
65%

In some cases, when I run jest --coverage --watch, I get the following error:, Ask questionsSyntax error when running Jest with coverage and watch (example repo included) ,Run yarn test-w. You will get the same error.

In some cases, when I run jest --coverage --watch, I get the following error:

Running coverage on untested files...Failed to collect coverage from / Users / gary / Downloads / Unsorted / test / jest - bug / package.json
ERROR: Unexpected token, expected;
(2: 8)
STACK: SyntaxError: Unexpected token, expected;
(2: 8)

Paste the results here:

  System:
     OS: macOS High Sierra 10.13 .5
  CPU: x64 Intel(R) Core(TM) i7 - 3615 QM CPU @ 2.30 GHz
  Binaries:
     Node: 8.11 .2 - /usr/local / bin / node
  Yarn: 1.9 .4 - /usr/local / bin / yarn
  npm: 6.4 .0 - /usr/local / bin / npm
  npmPackages:
     jest: ^ 23.5 .0 => 23.5 .0
load more v
75%

I am trying to get the code coverage of the tests but facing an issue. If I run "npm test" the test is being passed, but when I run "npm run coverage" it is not recognizing <AddDoctor/> and throwing an error there.,Here is the screenshot of the error,Stopping Azure subdomain takeovers

file.test.js

import { shallow } from 'enzyme';
import AddDoctor from '../pages/AddDoctor'
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
configure({ adapter: new Adapter() });
describe('AddDoctor',  () => {
    it('should render correctly in debug mode', () => {
        const component = shallow(<AddDoctor debug/>);
        expect(component).toMatchSnapshot();
    })
})
load more v
40%

Indicates whether the coverage information should be collected while executing the test. Because this retrofits all executed files with coverage collection statements, it may significantly slow down your tests.,The directory where Jest should output its coverage files.,Note: Setting this option overwrites the default values. Add "text" or "text-summary" to see a coverage summary in the console output.

Pretag
 Pretag team - issue, fix, solve, resolve
22%

Pretag
 Pretag team - issue, fix, solve, resolve
60%

at my app variable. It seems the test runner is not able to understand Typescript code. How can I fix my configuration to support Typescript in test files for Jest?,Try to add typescript extension in jest config :,I have a Node project using Typescript and Jest. Currently I have this project structure

With this tsconfig.json file

  "compilerOptions": {
     "target": "ES2017",
     "module": "commonjs",
     "allowJs": true,
     "outDir": "dist",
     "rootDir": "src",
     "strict": true,
     "moduleResolution": "node",
     "esModuleInterop": true
  }

this jest.config.js file

module.exports = {
   clearMocks: true,
   coverageDirectory: "coverage",
   testEnvironment: "node",
};

and this package.json file

{
   "scripts": {
      "start": "node dist/App.js",
      "dev": "nodemon src/App.ts",
      "build": "tsc -p .",
      "test": "jest"
   },
   "dependencies": {
      "commander": "^3.0.1"
   },
   "devDependencies": {
      "@types/jest": "^24.0.18",
      "@types/node": "^12.7.4",
      "jest": "^24.9.0",
      "nodemon": "^1.19.2",
      "ts-jest": "^24.0.2",
      "ts-node": "^8.3.0",
      "typescript": "^3.6.2"
   }
}

I created a test file in my tests directory

import {
   App
} from '../src/App';

describe('Generating App', () => {
   let app: App;

   test('It runs a test', () => {
      expect(true).toBe(true);
   });
});
load more v

Other "unexpected-token" queries related to "While running jest tests, coverage fails with Unexpected token"