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

Optional chaining rewriting by Hammerhead causes TypeError: undefined has no properties #3012

Open
thomas-jakemeyn opened this issue Jul 12, 2024 · 5 comments

Comments

@thomas-jakemeyn
Copy link

thomas-jakemeyn commented Jul 12, 2024

What is your Scenario?

My application uses @reduxjs/toolkit that contains the following function (see here):

function buildCreateSlice({ creators } = {}) {
	const cAT = creators?.asyncThunk?.[asyncThunkSymbol];
	...
}

What is the Current behavior?

The functionality that I am testing works well in Firefox. However, when I run the TestCafe test, it fails with a TypeError: undefined has no properties. This is caused by the transformation that is applied by Hammerhead.

The code is transformed from this:

function buildCreateSlice({ creators } = {}) {
	const cAT = creators?.asyncThunk?.[asyncThunkSymbol];
	...
}

into this, which throws a TypeError when creators?.asyncThunk is undefined:

__get$ = function(o,p){return o[p]}

function buildCreateSlice( _hh$temp418  = {}) {
	var creators = _hh$temp418.creators;
	const cAT = __get$(creators?.asyncThunk, asyncThunkSymbol, true);
	...
}

What is the Expected behavior?

The behaviour of creators?.asyncThunk?.[asyncThunkSymbol] should be preserved after the transformation.

What is your public website URL? (or attach your complete example)

NA

What is your TestCafe test code?

NA

Your complete configuration file

No response

Your complete test report

No response

Screenshots

No response

Steps to Reproduce

NA

TestCafe version

3.6.2

Node.js version

No response

Command-line arguments

testcafe firefox -e

Browser name(s) and version(s)

No response

Platform(s) and version(s)

No response

Other

No response

Copy link

Thank you for submitting a bug report. We would love to help you investigate the issue. Please share a simple code example that reliably reproduces the bug. For more information, read the following article: How To Create a Minimal Working Example When You Submit an Issue. We look forward to your response.

@thomas-jakemeyn
Copy link
Author

The test below passes in Chrome but fails in Firefox with a TypeError.

index.html

<!DOCTYPE html>
<html lang='en'>
<head>
    <meta charset='UTF-8'>
    <title>TestCafe</title>
</head>
<body>
  <script>
    const worker = new Worker('worker.js');
    worker.onmessage = e => {
      console.log(e.data);
    };
  </script>
</body>
</html>

worker.js

const asyncThunkSymbol = 'asyncThunkSymbol';

function buildCreateSlice({ creators } = {}) {
  return creators?.asyncThunk?.[asyncThunkSymbol];
}

try {
  const shouldBeUndefined = buildCreateSlice();
  postMessage(`OK: ${shouldBeUndefined}`);
} catch (error) {
  postMessage(error);
}

index.spec.js

const express = require('express');

const PORT = 3000;
let server;

fixture`Fixture`
  .page(`http://localhost:${PORT}/index.html`)
  .before(() => {
    const app = express();
    app.use(express.static(__dirname));
    server = app.listen(PORT);
  })
  .after(() => {
    server.close();
  });

test('test', async t => {
  await t.wait(1000);
  const { log } = await t.getBrowserConsoleMessages();
  console.log('worker logs: ', log);
  await t.expect(log[0]).match(/^OK:/);
});

@thomas-jakemeyn
Copy link
Author

Hello @Bayheck,

Any progress on this issue?

@thomas-jakemeyn
Copy link
Author

Hello @Bayheck,

Any chance this bug gets fixed in a near future?

Copy link

github-actions bot commented Sep 2, 2024

We appreciate you taking the time to share information about this issue. We reproduced the bug and added this ticket to our internal task queue. We'll update this thread once we have news.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants