Optional chaining isn't working with `next.js`
See original GitHub issueCurrent behavior
If the test code contains optional chaining, the tester fails to run.
Error: Webpack Compilation Error
./cypress/integration/sandbox.test.ts 5:25
Module parse failed: Unexpected token (5:25)
File was processed with these loaders:
* C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\ts-loader\index.js
You may need an additional loader to handle the result of these loaders.
| it('fails', () => {
| let something;
> expect(something?.length).to.equal(5);
| });
| });
at Watching.handle [as handler] (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\@cypress\webpack-preprocessor\dist\index.js:172:23)
at C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Watching.js:99:9
at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\tapable\lib\Hook.js:154:20)
at Watching._done (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Watching.js:98:28)
at C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Watching.js:73:19
at Compiler.emitRecords (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Compiler.js:499:39)
at C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Watching.js:54:20
at C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Compiler.js:485:14
at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\tapable\lib\Hook.js:154:20)
at C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\webpack\lib\Compiler.js:482:27
at C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\neo-async\async.js:2818:7
at done (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\neo-async\async.js:3522:9)
at AsyncSeriesHook.eval [as callAsync] (eval at create (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\tapable\lib\HookCodeFactory.js:33:10), <anonymous>:6:1)
at AsyncSeriesHook.lazyCompileHook (C:\Users\maple\AppData\Local\Cypress\Cache\7.5.0\Cypress\resources\app\packages\server\node_modules\tapable\lib\Hook.js:154:20)
Desired behavior
Should allow to run the test without any additional configurations.
This seems to an issue coming from webpack and it has been resolved from a version.
Test code to reproduce
npm install cypress
npx cypress open
- Have the below in the testing code and run it,.
describe('Optional chaining', () => {
it('fails', () => {
let something: string[] | undefined;
expect(something?.length).to.equal(5);
});
});
Not forking or creating a repo for repro because it seems that the source of the problem is bundled webpack.
Versions
Cypress package version: 7.5.0
Cypress binary version: 7.5.0
Electron version: 12.0.0-beta.14
Bundled Node version:
14.15.1
Obtained from cypress --version
.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:31 (23 by maintainers)
Top Results From Across the Web
Next JS not supported optional chaining - Stack Overflow
Next JS not supported optional chaining · Looks like nullish coalescing operator, not optional chaining. – Camilo. Dec 14, 2021 at 17:08 ·...
Read more >Supported Browsers and Features - Next.js
Optional Chaining (ES2020); Nullish Coalescing (ES2020); Class Fields and Static Properties (part of stage 3 proposal); and more! Server-Side ...
Read more >Node.js 12 applications failing by Optional chaining -
Node.js version 14 has included Optional Chaining (?.) operator as part of the updates/hightlights, which enable JavaScript developers to ...
Read more >babel/plugin-proposal-optional-chaining
@babel/plugin-proposal-optional-chaining ... undefined const willThrow = obj?.foo.bar.qux(); // Error: not a function // Top function can be called directly ...
Read more >How to Use Optional Chaining in JavaScript - freeCodeCamp
Optional chaining is particularly useful when working with API data. If you're not sure whether an optional property exists, you can reach ...
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
No worries, glad to document this for anyone running into this.
For now, I think there are two main workarounds for the problem:
cypress/tsconfig.json
file, setcompilerOptions.target
toes2019
and then setcompilerOptions.lib
to an array includinges2022
,dom
anddom.iterable
(and any other lib values that your project needs) - as I mentioned in my last Workaround comment above@cypress/webpack-preprocessor
,ts-loader
andwebpack
in order to have more capabilities, as mentioned in Zach’s comment aboveAhh I see, I didn’t have all the context around this issue and was assuming this was a problem with CT. There should be a solution by importing and modifying the options for
@cypress/webpack-preprocessor
, you should be able to configurets-loader
to point to a custom tsconfig file. Haven’t tried it and would need to dig into it but I’ll try and find time to get something running.