p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

p5.Vector: mock setup blocks the 8 disabled rotate tests — 6 revivable, 2 need a decision

Open Shruti2110-coder opened this issue 2 months ago • 3 comments

Topic

Follow-up to #9139 — same origin commit, different outcome.

Commit 7af496704 ("Mark most failing tests as todos", Sep 2024) disabled eight rotate tests in test/unit/math/p5.Vector.js (suite.todo at lines 189, 219, 242). Unlike the p5.Color ones in #9139, these can't just be re-enabled. I traced the blockers and wanted to flag what I found rather than guess at the right fix.

Two errors, in sequence

Re-enabling the suites as-is gives:

ReferenceError: RADIANS is not defined
  test/unit/math/p5.Vector.js:191
    mockP5Prototype.angleMode(RADIANS);

RADIANS/DEGREES live in src/math/trigonometry.js (line 87: const RADIANS = (fn.RADIANS = 'radians')), attached to whatever object the module is initialised with. The test file calls math() and vector() in beforeAll but never trigonometry(), so angleMode and the constants don't exist on the mock. The 2024 diff shows these calls were originally mockP5.RADIANS; the prefix was dropped at some point and the bare identifiers were never imported — invisible because the suites never ran.

Adding trigonometry(mockP5, mockP5Prototype) and qualifying the constants clears that, and surfaces the next one:

TypeError: Right-hand side of 'instanceof' is not callable
  src/math/math.js:95
    if (this instanceof p5) {

mockP5 in this file is an object literal, and instanceof requires a function on the right. (test/js/mocks.js uses vi.fn() for its mock, which is presumably why p5.Color's tests work.)

What revives 6 of the 8

Three changes, all in the test file:

  1. Import and call trigonometry(mockP5, mockP5Prototype) in beforeAll
  2. Change const mockP5 = { ... } to vi.fn() with _friendlyError assigned afterwards
  3. Reference the constants as mockP5Prototype.RADIANS / .DEGREES

Result: the three radians tests and all three [CLASS] tests pass. The two degrees tests still fail.

Why the last two fail

createVector only binds the angle-conversion helpers inside the this instanceof p5 branch. mockP5Prototype isn't an instance of mockP5, so that branch never runs and degree→radian conversion is never wired up. Radians pass because that's the default no-op path.

Evidence: rotating [0,1,0] by 180 in DEGREES mode gives x = 0.801, which is |sin(180 radians)|. The 180 is being treated as radians.

The question

Should the mock be constructed so this instanceof p5 holds — which would also unblock the p5.prototype.createVector / setHeading todos and the angleMode(DEGREES) heading tests, all of which depend on the same path — or should the degree-mode tests stay disabled and be covered elsewhere?

Happy to implement whichever direction is preferred. I haven't opened a PR.

Environment: macOS, Node v22.22.2, Vitest 4.1.10, chromium.

Shruti2110-coder avatar Sep 06 '26 12:09 Shruti2110-coder

Hi @Shruti2110-coder thanks for reporting! Since you have been assigned an issue already, please wait until the PR for that is merged before creating new issues or requesting assigment. Thanks for understanding!

ksen0 avatar Sep 11 '26 09:09 ksen0

Hi @ksen0, #9139 is now closed via #9166, so I'm following up on this one. Could I be assigned?

Shruti2110-coder avatar Sep 20 '26 02:09 Shruti2110-coder

Hi! I'd be interested in contributing to this issue. I understand that six of the eight disabled rotate tests can potentially be restored with mock setup changes, while the two degree-mode tests need a decision about the intended behavior.

I'd be happy to investigate the test setup and verify the proposed changes. Would you be open to another contributor working on this issue, and should the two degree-mode tests be addressed by fixing the mock instance relationship or kept disabled for now?

Thanks!

Gautam5471 avatar Oct 10 '26 22:10 Gautam5471