question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:

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:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
sjclemmycommented, Dec 3, 2019

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:

module.exports = {
  presets: [
    [
      '@babel/preset-env',
      {
        targets: {
          node: true,
        },
      },
    ],
  ],
  plugins: [
    '@babel/plugin-proposal-class-properties',
  ]
};

4reactions
pahundcommented, Oct 8, 2019

I’ve found a workaround for this.

In my Jest config, I’m disabling Babel transpilation completely using transformIgnorePatterns:

module.exports = {
  testEnvironment: "node",
  transformIgnorePatterns: ['.*']
};

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found