codeant-ai-for-open-source[bot] commented on code in PR #40379:
URL: https://github.com/apache/superset/pull/40379#discussion_r3290499762
##########
superset-frontend/packages/superset-core/src/translation/TranslatorSingleton.test.ts:
##########
@@ -96,6 +96,65 @@ test('tn() calls translateWithNumber on the singleton', ()
=> {
});
});
+test('pre-configure warning fires once per unique key', () => {
+ jest.isolateModules(() => {
+ const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() =>
{});
+ const { t } = require('./TranslatorSingleton');
+ t('apple');
+ t('apple');
+ t('apple');
+ t('banana');
+ expect(consoleSpy).toHaveBeenCalledTimes(2);
+ expect(consoleSpy).toHaveBeenNthCalledWith(
+ 1,
+ expect.stringContaining('"apple"'),
+ );
+ expect(consoleSpy).toHaveBeenNthCalledWith(
+ 2,
+ expect.stringContaining('"banana"'),
+ );
+ consoleSpy.mockRestore();
+ });
+});
+
+test('pre-configure warning suggests the lazy-function fix', () => {
+ jest.isolateModules(() => {
+ const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() =>
{});
+ const { t } = require('./TranslatorSingleton');
+ t('Sort ascending');
+ expect(consoleSpy).toHaveBeenCalledWith(
+ expect.stringContaining('() => t("Sort ascending")'),
+ );
+ consoleSpy.mockRestore();
+ });
+});
+
+test('pre-configure warning is suppressed in production', () => {
+ jest.isolateModules(() => {
+ const originalEnv = process.env.NODE_ENV;
+ process.env.NODE_ENV = 'production';
+ const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() =>
{});
+ const { t } = require('./TranslatorSingleton');
+ t('hello');
+ expect(consoleSpy).not.toHaveBeenCalled();
+ consoleSpy.mockRestore();
+ process.env.NODE_ENV = originalEnv;
Review Comment:
**Suggestion:** This test restores `NODE_ENV` by direct assignment, which is
incorrect when the original value was unset: assigning `undefined` to
`process.env` creates the literal string `"undefined"` instead of removing the
variable. That can leak environment state into later tests and produce flaky
behavior. Restore by deleting `process.env.NODE_ENV` when `originalEnv` is
`undefined`, otherwise assign the saved value. [incorrect variable usage]
<details>
<summary><b>Severity Level:</b> Major ⚠️</summary>
```mdx
- ⚠️ Jest test leaks NODE_ENV when originally unset.
- ⚠️ Later tests may read polluted NODE_ENV value.
- ⚠️ Environment-sensitive behavior (e.g. i18n warnings) becomes
order-dependent.
```
</details>
<details>
<summary><b>Steps of Reproduction ✅ </b></summary>
```mdx
1. Start the Jest test suite with `process.env.NODE_ENV` unset (no
environment variable
defined before Node starts), so `process.env.NODE_ENV` is `undefined` at
runtime.
2. Run the test `pre-configure warning is suppressed in production` in
`superset-frontend/packages/superset-core/src/translation/TranslatorSingleton.test.ts:132-143`.
At the beginning of this test, `const originalEnv = process.env.NODE_ENV;`
(line 134)
stores `undefined` into `originalEnv`.
3. The test then sets `process.env.NODE_ENV = 'production';` (line 135),
runs the
assertions, and at the end executes `process.env.NODE_ENV = originalEnv;`
(line 141).
Because `originalEnv` is `undefined`, this assignment sets the environment
variable to the
literal string `"undefined"` instead of removing it.
4. In the same Jest process, any later code that reads
`process.env.NODE_ENV` (for
example, `warnPreConfigure` in
`superset-frontend/packages/superset-core/src/translation/TranslatorSingleton.ts:47-53`
or
the dev-only logging guard in
`superset-frontend/src/utils/safeStringify.ts:35`) will now
see a defined env var with value `"undefined"` instead of an absent
variable, meaning the
global environment is polluted compared to the pre-test state and can lead
to subtle,
order-dependent test behavior. Deleting the env key when `originalEnv` is
`undefined`
would avoid this leak.
```
</details>
[Fix in
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=bcdff4a345384aeca59f202a140343b9&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
| [Fix in VSCode
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=bcdff4a345384aeca59f202a140343b9&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
*(Use Cmd/Ctrl + Click for best experience)*
<details>
<summary><b>Prompt for AI Agent 🤖 </b></summary>
```mdx
This is a comment left during a code review.
**Path:**
superset-frontend/packages/superset-core/src/translation/TranslatorSingleton.test.ts
**Line:** 134:141
**Comment:**
*Incorrect Variable Usage: This test restores `NODE_ENV` by direct
assignment, which is incorrect when the original value was unset: assigning
`undefined` to `process.env` creates the literal string `"undefined"` instead
of removing the variable. That can leak environment state into later tests and
produce flaky behavior. Restore by deleting `process.env.NODE_ENV` when
`originalEnv` is `undefined`, otherwise assign the saved value.
Validate the correctness of the flagged issue. If correct, How can I resolve
this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask
user if the user wants to fix the rest of the comments as well. if said yes,
then fetch all the comments validate the correctness and implement a minimal fix
```
</details>
<a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40379&comment_hash=1ab0b93f903a470fe81549be7f3d313c8db29188f9aaa110c7aeee85382fc13e&reaction=like'>👍</a>
| <a
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40379&comment_hash=1ab0b93f903a470fe81549be7f3d313c8db29188f9aaa110c7aeee85382fc13e&reaction=dislike'>👎</a>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]