deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Displaced lines in Actual / Expected diff

Open GJZwiers opened this issue 3 years ago • 2 comments

Some of the lines from the deno test Actual / Expected diff are getting displaced.

fetch.js

export async function fetchText() {
    const response = await fetch(new URL("./file.txt", import.meta.url));
    return await response.text();
}

export const _internals = { fetch };

fetch_test.js

import { _internals, fetchText } from "./fetch.js";
import {
  assertSpyCall,
  returnsNext,
  stub,
} from "https://deno.land/[email protected]/testing/mock.ts";
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

Deno.test("fetch mock", async () => {
  const fetchTextStub = stub(_internals, "fetch", returnsNext(["fetched it"]));

  try {
      assertEquals(await fetchText(), "fetched it");
    } finally {
      fetchTextStub.restore();
    }

  assertSpyCall(fetchTextStub, 0, {
      returned: "fetched it",
    });
});

In the output 'fetched it' should be next to the + symbol in the Expected part but it is part of the 'Actual' lines

deno test --allow-read
running 1 test from ./fetch_test.js
fetch mock ... FAILED (40ms)
running 0 tests from ./sum.test.js

 ERRORS 

fetch mock => ./fetch_test.js:9:6
error: AssertionError: Values are not equal:


    [Diff] Actual / Expected


-   Hello File!\n
-   fetched it
+   


  throw new AssertionError(message);
        ^
    at assertEquals (https://deno.land/[email protected]/testing/asserts.ts:184:9)
    at file:///C:/Users/GJZwiers/repos/medium/fetch_test.js:13:7

 FAILURES

fetch mock => ./fetch_test.js:9:6

FAILED | 0 passed | 1 failed (79ms)

error: Test failed

file.txt

Hello File!

deno --version

deno 1.28.2 (release, x86_64-pc-windows-msvc)
v8 10.9.194.1
typescript 4.8.3

GJZwiers avatar Nov 25 '22 10:11 GJZwiers

Reproduction:

import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";

assertEquals("Hello there!\n", "fetched it");

dsherret avatar Nov 25 '22 15:11 dsherret

The problem comes from https://github.com/denoland/deno_std/blob/main/testing/_diff.ts#L329-L332 The generated diff contains this array :

[
  { type: "removed", value: "Hello world\\n\n" },
  { type: "added", value: "fetched it" },
  { type: "common", value: "\n" }
]

Where each object is a line in the final rendered diff. So in the current example, it should instead be something like :

[
  { type: "removed", value: "Hello world\\n\n" },
  { type: "added", value: "fetched it" }
]

It seems the problems is added when the trailing \n is added, but I can't change the current code without making the others tests in https://github.com/denoland/deno_std/blob/main/testing/_diff_test.ts fail.

JeanDavidDaviet avatar Jun 07 '23 09:06 JeanDavidDaviet