Skip to content

fix: fallback when console.createTask returns undefined#147

Closed
leno23 wants to merge 1 commit into
unjs:mainfrom
leno23:fix/create-task-undefined-run-114
Closed

fix: fallback when console.createTask returns undefined#147
leno23 wants to merge 1 commit into
unjs:mainfrom
leno23:fix/create-task-undefined-run-114

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 26, 2026

Summary

  • Wrap console.createTask so a missing/undefined task falls back to a no-op runner
  • Prevents Cannot read properties of undefined (reading 'run') in serial and parallel hook callers

Example

console.createTask = () => undefined;
const hooks = new Hookable();
hooks.hook("test", () => {});
hooks.callHook("test"); // no longer throws

Fixes #114

Test plan

  • Added regression test for undefined console.createTask return value
  • pnpm exec vitest run — 44 passing
  • pnpm lint — clean

Summary by CodeRabbit

  • Bug Fixes

    • Improved fallback behavior in task creation to properly handle undefined values and edge cases.
  • Tests

    • Added regression test to verify system stability when task creation encounters specific conditions.

Review Change Stack

Some runtimes expose console.createTask but return undefined from it,
which caused parallel and serial hook callers to crash on task.run.

Fixes unjs#114
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 26, 2026

📝 Walkthrough

Walkthrough

The PR adds a nullish safeguard to the createTask factory. When console.createTask exists but returns undefined, createTask(name) now falls back to defaultTask via the ?? operator instead of returning undefined. A regression test verifies that Hookable.callHook and Hookable.callHookParallel handle undefined task results safely.

Changes

Nullish fallback for console.createTask

Layer / File(s) Summary
defaultTask fallback with undefined safeguard
src/utils.ts, test/hookable.test.ts
defaultTask is extracted and reused with nullish coalescing (??) when console.createTask returns undefined. A test stubs console.createTask to return undefined and verifies that callHook and callHookParallel complete without throwing errors.

🎯 2 (Simple) | ⏱️ ~8 minutes

🐰 A task without a home has found one at last,
When console returns naught, we're falling back fast.
Undefined begone! Our hooks now stand tall,
With defaultTask catching them one and all. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix: fallback when console.createTask returns undefined' directly and clearly describes the main change: handling the undefined return value case with a fallback.
Linked Issues check ✅ Passed The PR successfully addresses issue #114 by implementing a fallback when console.createTask returns undefined, preventing the 'Cannot read properties of undefined' runtime error.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the issue: modifying createTask logic in src/utils.ts and adding a regression test in test/hookable.test.ts.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/utils.ts (1)

66-67: ⚡ Quick win

Move internal helper to file end to match repo convention.

defaultTask is an internal helper and should be placed at the end of the file per project rule.

As per coding guidelines, "Place internal helpers at end of file".

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/utils.ts` around lines 66 - 67, The helper constant defaultTask (typed as
ReturnType<CreateTask>) is an internal helper and must be moved to the end of
the file to follow the project convention; relocate the declaration of
defaultTask to the bottom of src/utils.ts (after all exported functions/classes)
and ensure any references to CreateTask or defaultTask still resolve (no change
to its name or type) so imports/exports remain unaffected.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@src/utils.ts`:
- Around line 66-67: The helper constant defaultTask (typed as
ReturnType<CreateTask>) is an internal helper and must be moved to the end of
the file to follow the project convention; relocate the declaration of
defaultTask to the bottom of src/utils.ts (after all exported functions/classes)
and ensure any references to CreateTask or defaultTask still resolve (no change
to its name or type) so imports/exports remain unaffected.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 29850c77-a487-41e1-a8dc-a4512b27130c

📥 Commits

Reviewing files that changed from the base of the PR and between b77477c and 580cabb.

📒 Files selected for processing (2)
  • src/utils.ts
  • test/hookable.test.ts

@pi0 pi0 closed this May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'run')

2 participants