From 6a349c3b0a91389a4e1af1cdf836ef0ea7b0d80f Mon Sep 17 00:00:00 2001 From: Jairus Date: Wed, 17 Jul 2024 11:50:02 -0700 Subject: [PATCH] use diff --- assembly/__tests__/sleep.spec.ts | 2 +- assembly/src/log.ts | 2 +- assembly/src/suite.ts | 6 ++--- bin/run.js | 38 +++++++++++++++++++++++++++++--- cli/run.ts | 38 +++++++++++++++++++++++++++++--- package.json | 3 ++- 6 files changed, 77 insertions(+), 12 deletions(-) diff --git a/assembly/__tests__/sleep.spec.ts b/assembly/__tests__/sleep.spec.ts index 55e07dd..8f353e5 100644 --- a/assembly/__tests__/sleep.spec.ts +++ b/assembly/__tests__/sleep.spec.ts @@ -14,7 +14,7 @@ describe("Should sleep", () => { test("1s", () => { const start = Date.now(); sleep(1000); - expect(Date.now() - start).toBeGreaterOrEqualTo(1000); + expect(Date.now() - start).toBeGreaterOrEqualTo(12345); }); test("5s", () => { const start = Date.now(); diff --git a/assembly/src/log.ts b/assembly/src/log.ts index 4d9f061..ab136ea 100644 --- a/assembly/src/log.ts +++ b/assembly/src/log.ts @@ -10,6 +10,6 @@ export class Log { this.text = text; } display(): void { - term.write(" ".repeat(this.depth + 1) + `${rainbow.bgBlackBright(" LOG ")}${rainbow.dimMk(":")} ${this.text}\n`); + term.write(" ".repeat(this.depth + 1) + `${rainbow.bgBlackBright(" LOG ")}${rainbow.dimMk(": " + this.text)}\n`); } } \ No newline at end of file diff --git a/assembly/src/suite.ts b/assembly/src/suite.ts index 5ef4e81..9c09306 100644 --- a/assembly/src/suite.ts +++ b/assembly/src/suite.ts @@ -78,11 +78,11 @@ export class Suite { } } - if (!suiteNone || this.tests.length) { + if (this.verdict == "fail") { + suiteLn.edit(`${suiteDepth}${rainbow.bgRed(" FAIL ")} ${rainbow.dimMk(this.description)}\n`); + } else if (!suiteNone || this.tests.length) { this.verdict = "ok"; suiteLn.edit(`${suiteDepth}${rainbow.bgGreenBright(" PASS ")} ${rainbow.dimMk(this.description)}\n`); - } else if (this.verdict == "fail") { - suiteLn.edit(`${suiteDepth}${rainbow.bgRed(" FAIL ")} ${rainbow.dimMk(this.description)}\n`); } else { suiteLn.edit(`${suiteDepth}${rainbow.bgBlackBright(" EMPTY ")} ${rainbow.dimMk(this.description)}\n`); } diff --git a/bin/run.js b/bin/run.js index b1edfa1..3ea9e37 100644 --- a/bin/run.js +++ b/bin/run.js @@ -4,6 +4,7 @@ import { glob } from "glob"; import { formatTime, getExec, loadConfig } from "./util.js"; import * as path from "path"; import { existsSync, mkdirSync, writeFileSync } from "fs"; +import { diff } from "typer-diff"; const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json"); const ansi = new RegExp("[\\u001B\\u009B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", "g"); export async function run() { @@ -68,14 +69,45 @@ export async function run() { for (const failed of reporter.failed) { console.log(`${chalk.bgRed(" FAIL ")} ${chalk.dim(failed.description)}\n`); for (const test of failed.tests) { + const diffResult = diff(JSON.stringify(test._left), JSON.stringify(test._right)); + let expected = chalk.dim(JSON.stringify(test._left)); + let received = ""; + for (const res of diffResult.diff) { + switch (res.type) { + case "correct": { + received += chalk.dim(res.value); + continue; + } + case "extra": { + received += chalk.red.strikethrough(res.value); + continue; + } + case "missing": { + received += chalk.bgBlack(res.value); + continue; + } + case "wrong": { + received += chalk.bgRed(res.value); + continue; + } + case "untouched": { + //received += chalk.bgBlackBright(res.value); + continue; + } + case "spacer": { + //received += chalk.bgBlackBright(res.value); + continue; + } + } + } if (test.verdict == "fail") { - console.log(`${chalk.dim("(expected) ->")} ${chalk.bold(test._left.toString())}`); - console.log(`${chalk.dim("(received) ->")} ${chalk.bold(test._right.toString())}\n`); + console.log(`${chalk.dim("(expected) ->")} ${expected}`); + console.log(`${chalk.dim("(received) ->")} ${received}\n`); } } } } - console.log("----------------- [RESULTS] ------------------\n"); + console.log(chalk.dim("----------------- [RESULTS] ------------------\n")); process.stdout.write(chalk.bold("Files: ")); if (reporter.failedFiles) { process.stdout.write(chalk.bold.red(reporter.failedFiles + " failed")); diff --git a/cli/run.ts b/cli/run.ts index d443393..084a591 100644 --- a/cli/run.ts +++ b/cli/run.ts @@ -5,6 +5,7 @@ import { glob } from "glob"; import { formatTime, getExec, loadConfig } from "./util.js"; import * as path from "path"; import { appendFileSync, existsSync, fstat, mkdirSync, writeFileSync } from "fs"; +import { diff } from "typer-diff"; const CONFIG_PATH = path.join(process.cwd(), "./as-test.config.json"); @@ -82,15 +83,46 @@ export async function run() { for (const failed of reporter.failed) { console.log(`${chalk.bgRed(" FAIL ")} ${chalk.dim(failed.description)}\n`); for (const test of failed.tests) { + const diffResult = diff(JSON.stringify(test._left), JSON.stringify(test._right)); + let expected = chalk.dim(JSON.stringify(test._left)); + let received = ""; + for (const res of diffResult.diff) { + switch (res.type) { + case "correct": { + received += chalk.dim(res.value); + continue; + } + case "extra": { + received += chalk.red.strikethrough(res.value); + continue; + } + case "missing": { + received += chalk.bgBlack(res.value); + continue; + } + case "wrong": { + received += chalk.bgRed(res.value); + continue; + } + case "untouched": { + //received += chalk.bgBlackBright(res.value); + continue; + } + case "spacer": { + //received += chalk.bgBlackBright(res.value); + continue; + } + } + } if (test.verdict == "fail") { - console.log(`${chalk.dim("(expected) ->")} ${chalk.bold(test._left.toString())}`); - console.log(`${chalk.dim("(received) ->")} ${chalk.bold(test._right.toString())}\n`); + console.log(`${chalk.dim("(expected) ->")} ${expected}`); + console.log(`${chalk.dim("(received) ->")} ${received}\n`); } } } } - console.log("----------------- [RESULTS] ------------------\n"); + console.log(chalk.dim("----------------- [RESULTS] ------------------\n")); process.stdout.write(chalk.bold("Files: ")); if (reporter.failedFiles) { diff --git a/package.json b/package.json index 2a25e3d..9e894d7 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "chalk": "^5.3.0", "glob": "^11.0.0", "jest": "^29.7.0", - "json-as": "^0.9.14" + "json-as": "^0.9.14", + "typer-diff": "^1.1.1" }, "overrides": { "assemblyscript": "$assemblyscript",