ssssota / doc-vitest

Documentation tests with Vitest
https://npmjs.com/vite-plugin-doctest
62 stars 0 forks source link

doc-virtest reporting wrong location for the failing (typescript) doctest. #87

Open wizzardx opened 2 months ago

wizzardx commented 2 months ago

In my file, src/functions.ts, I have this code starting at line 13:

/**
 * Adds two numbers together.
 *
 * @param {Big | "Infinity"} a - The first number to add.
 * @param {Big} b - The second number to add.
 * @returns {Big | "Infinity"} The sum of a and b.
 * @example
 * ```ts @import.meta.vitest
 * expect(add(Big(5), Big(3))).toStrictEqual(Big(8))
 * expect(add("Infinity", Big(3))).toBe("InfinityZ")
 * ```
 */
export function add(a: Big | "Infinity", b: Big): Big | "Infinity" {
  if (a === "Infinity") {
    return "Infinity";
  }
  return a.add(b);
}

There is an intentional error there, where I'm expecting the string return result to be "InfinityZ" rather than "Infinity"

doc-vitest does catch the error, but it reports on the wrong part of my code containing the failed doctest:

 FAIL  src/functions.ts > /home/david/learning/dev/the_odin_project/foundations-calculator/src/functions.ts#0
AssertionError: expected 'Infinity' to be 'InfinityZ' // Object.is equality

Expected: "InfinityZ"
Received: "Infinity"

 ❯ src/functions.ts:603:63
    601|    */
    602|   public pressBackspaceButton(): void {
    603|     if (this.currentInputNumber === null) {
       |                                                               ^
    604|       return; // Nothing to backspace if there's no current input
    605|     }

 FAIL  src/functions.ts > /home/david/learning/dev/the_odin_project/foundations-calculator/src/functions.ts#0
AssertionError: expected 'Infinity' to be 'InfinityZ' // Object.is equality