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';
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.
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';
}
https://github.com/ethan605/typescript-playground
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
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.
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.