Skip to content

PU-001: Feature Request To Test Functions Creators#9

Merged
han4wluc merged 6 commits intomainfrom
task/pu-001
Aug 23, 2025
Merged

PU-001: Feature Request To Test Functions Creators#9
han4wluc merged 6 commits intomainfrom
task/pu-001

Conversation

@han4wluc
Copy link
Copy Markdown
Contributor

Summary

  • Added support for testing factory functions that return objects with methods
  • Allow executions field in function tests (previously only available for class tests)
  • Skip return value assertion when executions is present and out is omitted

Implementation Details

This PR implements the feature request to support testing functions that return objects with methods (factory pattern). The key changes are:

  1. Modified YAML parsing to allow executions field for function tests
  2. Updated test execution logic to handle executions in function tests
  3. Added conditional return value assertion - only assert when out is explicitly provided

Test Plan

  • Added new test file examples/store.test.yaml with comprehensive test cases
  • Created examples/store.js with two factory functions (createStore and createCounter)
  • All existing tests pass without modification
  • New tests demonstrate:
    • Testing methods on returned objects
    • Skipping return value assertion when out is omitted
    • Support for nested method calls and assertions

Documentation

  • Added new section "Testing Factory Functions" in README.md
  • Updated table of contents
  • Included example usage and explanation of the feature

Backward Compatibility

This change is fully backward compatible. Existing tests continue to work as before, and the new functionality is only activated when executions is used in a function test.

- Allow executions field in function tests (not just class tests)
- Skip return value assertion when executions is present and out is omitted
- Add test cases for factory functions (createStore, createCounter)
- Update documentation with factory function testing section
- Maintain backward compatibility for existing tests

This enables testing of factory functions and builder patterns that return
objects with methods, addressing the common JavaScript pattern where
functions create and return objects with behavior.
- Return value assertion now depends on whether 'out' field is present
  in the test case, not on whether 'executions' exists
- When 'out' field is omitted: function is called but return value is not asserted
- When 'out:' is present (even empty): return value is asserted (empty = null in YAML)
- Fixed utils.js to only add 'out' property when present in YAML
- Updated mockResolver to handle simple mock definitions (fn: true)
- Added comprehensive tests demonstrating the behavior
- Updated documentation to clarify the behavior

This allows more flexible testing where you can:
- Test factory functions without asserting their return value
- Explicitly assert null/undefined returns when needed
- Skip return assertions for any function test
- Added special keyword __undefined__ to assert undefined return values
- Handles __undefined__ in all assertion contexts:
  - Function return values (out: __undefined__)
  - Method return values in executions
  - Mock return values and input expectations
  - Property assertions
- Added comprehensive tests demonstrating the functionality
- Updated documentation with examples

This solves the limitation where YAML cannot represent JavaScript undefined:
- `out:` in YAML becomes null
- `out: undefined` in YAML becomes the string "undefined"
- `out: __undefined__` now properly asserts undefined
@han4wluc han4wluc merged commit 5ff07b2 into main Aug 23, 2025
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.

1 participant