Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on code with private class fields #8929

Closed
Alexsey opened this issue Sep 8, 2019 · 3 comments
Closed

Crash on code with private class fields #8929

Alexsey opened this issue Sep 8, 2019 · 3 comments

Comments

@Alexsey
Copy link

Alexsey commented Sep 8, 2019

πŸ› Bug Report

The code with private class fields is crashing when executing with Jest with error:

>npx jest test
 FAIL  test/index.test.js
  ● Test suite failed to run

    SyntaxError: F:\_\WebstormProjects\jestBug\src\index.js: Unexpected character '#' (4:2)

      2 | 
      3 | class C {
    > 4 |   #x = 42;
        |   ^
      5 |   x() {
      6 |     return this.#x;
      7 |   }

      at Parser.raise (node_modules/@babel/parser/lib/index.js:6387:17)
      at Parser.readToken_numberSign (node_modules/@babel/parser/lib/index.js:6716:12)
      at Parser.getTokenFromCode (node_modules/@babel/parser/lib/index.js:7062:14)
      at Parser.nextToken (node_modules/@babel/parser/lib/index.js:6587:12)
      at Parser.next (node_modules/@babel/parser/lib/index.js:6527:10)
      at Parser.eat (node_modules/@babel/parser/lib/index.js:6532:12)
      at Parser.expect (node_modules/@babel/parser/lib/index.js:7690:10)
      at Parser.parseClassBody (node_modules/@babel/parser/lib/index.js:10670:10)
      at Parser.parseClass (node_modules/@babel/parser/lib/index.js:10645:22)
      at Parser.parseStatementContent (node_modules/@babel/parser/lib/index.js:9942:21)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        2.434s
Ran all test suites matching /test/i.

But direct run of the file under test works fine:

>node src/index.js
42

To Reproduce

Steps to reproduce the behavior:

  1. Create src/index.js
'use strict'

class C {
  #x = 42;
  x() {
    return this.#x;
  }
}

console.log(new C().x())

module.exports = C
  1. Create test/index.test.js
'use strict'

const C = require('../src')

test('test', () => {
  expect(new C().x()).toEqual(42) //?
})
  1. Run npx jest test

Expected behavior

Should have no error

Link to repl or repo (highly encouraged)

https://github.com/Alexsey/jestBug

envinfo

npx: installed 1 in 2.239s

  System:
    OS: Windows 10
    CPU: (4) x64 Intel(R) Core(TM) i5-6600 CPU @ 3.30GHz
  Binaries:
    Node: 12.10.0 - C:\Program Files\nodejs\node.EXE
    npm: 6.11.3 - C:\Program Files\nodejs\npm.CMD
@SimenB
Copy link
Member

SimenB commented Sep 8, 2019

Roundabout duplicate of #6829 and #8497. This is babel throwing - either disable babel or add the syntax plugin.

I recommend following babel/babel#7660 which I think is the only non ad hoc solution to this

@SimenB SimenB closed this as completed Sep 8, 2019
@piranna
Copy link
Contributor

piranna commented Sep 15, 2019

I recommend following babel/babel#7660 which I think is the only non ad hoc solution to this

I've read that issue but got no idea what should I do... can you explain it a little bit about how to support Node.js features with Jest without transpiling?

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants