Jest does not support private class fields
See original GitHub issue#8497 🐛 Bug Report
When using Jest with a class that has private fields (introduced in Node.js 12), the tests fail with a syntax error – “unexpected character ‘#’”
To Reproduce
Prerequisites
- Node.js version at least 12.0.0 is installed
- Yarn is installed
Steps to reproduce the behavior
Clone this repository: https://github.com/technology-ebay-de/zapperment
git clone git@github.com:technology-ebay-de/zapperment.git
cd zapperment
Checkout branch jest-issue:
git checkout jest-issue
Install npm dependencies:
yarn
Change directory to workspace backend:
cd packages/backend
Run tests:
yarn test
The console shows the following error:
FAIL src/model/SceneBuilder.test.js
● Test suite failed to run
SyntaxError: /Users/pahund/git/zapperment/packages/backend/src/model/SceneBuilder.js: Unexpected character '#' (7:2)
5 |
6 | module.exports = class {
> 7 | #foo = 'foo';
| ^
8 |
9 | constructor({ storage }) {
10 | this.storage = storage;
at Parser.raise (../../node_modules/@babel/parser/lib/index.js:6400:17)
at Parser.readToken_numberSign (../../node_modules/@babel/parser/lib/index.js:6729:12)
at Parser.getTokenFromCode (../../node_modules/@babel/parser/lib/index.js:7073:14)
at Parser.nextToken (../../node_modules/@babel/parser/lib/index.js:6600:12)
at Parser.next (../../node_modules/@babel/parser/lib/index.js:6540:10)
at Parser.eat (../../node_modules/@babel/parser/lib/index.js:6545:12)
at Parser.expect (../../node_modules/@babel/parser/lib/index.js:7714:10)
at Parser.parseClassBody (../../node_modules/@babel/parser/lib/index.js:10702:10)
at Parser.parseClass (../../node_modules/@babel/parser/lib/index.js:10677:22)
at Parser.parseExprAtom (../../node_modules/@babel/parser/lib/index.js:8897:21)
console.log jest.setup.js:3
Jest is using Node version 12.11.0
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.651s
Ran all test suites.
If you delete line 7 in the module SceneBuilder.js
, the tests run without error.
Expected behavior
No error, Jest should support private class fields with the #
syntax, since they can be used in Node.js 12 without flags or transpilers.
Link to repl or repo
See steps to reproduce above:
- Repository: technology-ebay-de/zapperment
- Branch: jest-issue
envinfo
System:
OS: macOS Mojave 10.14.6
CPU: (8) x64 Intel(R) Core(TM) i7-6920HQ CPU @ 2.90GHz
Binaries:
Node: 12.11.0 - ~/.nvm/versions/node/v12.11.0/bin/node
Yarn: 1.19.0 - /usr/local/bin/yarn
npm: 6.11.3 - ~/.nvm/versions/node/v12.11.0/bin/npm
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:10 (1 by maintainers)
Top Results From Across the Web
How can I mock a private property in a class I'm trying to test in ...
I tried that and the compiler errored out because the backdoor property doesn't already exist on the class :| with that approach I...
Read more >ES6 Class Mocks - Jest
Since calls to jest.mock() are hoisted to the top of the file, Jest prevents access to out-of-scope variables. By default, you cannot first ......
Read more >Setting up ESLint to work with new or proposed JavaScript ...
A quick search for: eslint private class fields you will most likely ... It will tell you that ESLint does not support experimental...
Read more >Using classes - JavaScript - MDN Web Docs - Mozilla
Note: Private fields and methods are new features in classes with no trivial equivalent in function constructors.
Read more >Testing private methods in Typescript - DEV Community
My example is a service with a private method for error, ... Because it is private we can't test but we have two...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The problem (and therefore the solution) is the babel configuration in jest. The fix is to add a babel configuration file with the correct presets and plugins. First install the correct packages:
npm i @babel/preset-env @babel/plugin-proposal-class-properties --save-dev
and then add a config file
babel.config.js
with the following content:I’ve found a workaround for this.
In my Jest config, I’m disabling Babel transpilation completely using transformIgnorePatterns: