Jest: "Jest encountered an unexpected token" with TypeScript static properties

Created on 10 Sep 2019  ·  1Comment  ·  Source: facebook/jest

🐛 Bug Report

When running tests which invoke TypeScript classes having static property initialization under class declaration, Jest report this error:

Jest encountered an unexpected token

This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

Here's what you can do:
  • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
  • If you need a custom transformation specify a "transform" option in your config.
  • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html

Details:

.../src/Houses.ts:13
  static words = 'Ours is the Fury';

To Reproduce

There's a sample project to demo this bug: https://github.com/ethan605/typescript-playground, following the README.md instructions and running tests should reproduce this error.

Expected behavior

In the demo project's test case at __tests__/Houses.spec.ts, if toggle the default imports with this one:

import { Baratheon, Lannister, Stark, Targaryen } from '../src/HousesThatWork';

then the tests will pass. It seems that the static property initializations are incompatible with Jest:

export class Stark extends House {
  static readonly words = 'Winter Is Coming';
}

Link to repl or repo

https://github.com/ethan605/typescript-playground

envinfo

System:
  OS: macOS 10.14.6
  CPU: (8) x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
Binaries:
  Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
  Yarn: 1.17.3 - /usr/local/bin/yarn
  npm: 6.9.0 - ~/.nvm/versions/node/v10.16.3/bin/npm
npmPackages:
  jest: ^24.9.0 => 24.9.0
Bug Report Needs Repro Needs Triage

Most helpful comment

You need to include https://babeljs.io/docs/en/babel-plugin-proposal-class-properties.

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

>All comments

You need to include https://babeljs.io/docs/en/babel-plugin-proposal-class-properties.

Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jardakotesovec picture jardakotesovec  ·  3Comments

withinboredom picture withinboredom  ·  3Comments

mmcgahan picture mmcgahan  ·  3Comments

Secretmapper picture Secretmapper  ·  3Comments

gustavjf picture gustavjf  ·  3Comments