D
D
Dmitry2020-09-15 13:53:42
JavaScript
Dmitry, 2020-09-15 13:53:42

Why doesn't Jest understand the syntax?. in the imported file?

1. Created an npm package with a useful feature. Tested with jest, published on npm
2. Checked on codesandbox, importing the package and running the function - everything works fine
3. Now I create another package, into which I import the first one and when I run the tests through jest I get an error

Jest encountered an unexpected token

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

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


Details:

C:\OSPanel\domains\localhost\js projects\sql-for-js-simple\node_modules\split-smartly\index.js:452 (searchSettings.mentions?.[fragment] && ACTION_ADD_FRAGMENT) ^

SyntaxError: Unexpected token .


That is, it swears at the syntax searchSettings.mentions?.[fragment]

in the imported file, despite the fact that this syntax works fine in the project itself.
Question: what am I doing wrong?

Link to the project where the error occurs (only when importing into another project and running tests via jest)
https://github.com/dimoff66/split-smartly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Robur, 2020-09-15
@Robur

Apparently your other project is not configured to understand the syntax "?."
The message from jest is quite informative

"This usually means that you are trying to import a file which Jest cannot parse, eg it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules.""

you either need to put the babel config the same to understand the new syntax, or compile your code with the same babel into an older standard

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question