aboutsummaryrefslogtreecommitdiff
path: root/integration/scripts/bun.js
blob: d227857877874be1cb9ccd65a11e702648f16fa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import snippets from "./snippets.json";

globalThis.console.assert = (condition, ...content) => {
  if (!condition) {
    throw new Error(content.join(" "));
  }
};
globalThis.getModuleScriptSrc = async (name) => {
  const response = await fetch(name, {
    cache: "force-cache",
  });

  if (response.ok) {
    return await response.text();
  } else {
    throw new Error(`Failed to get module script ${name}`);
  }
};

globalThis.runTest = async (name) => {
  testSuccess = false;
  var Namespace = await import(name);
  var testFunction = Namespace.test;

  if (
    !("test" in Namespace) &&
    "default" in Namespace &&
    typeof Namespace.default === "function"
  ) {
    Namespace = Namespace.default();
    testFunction = Namespace.test;
  }

  if (!testFunction) {
    throw new Error("No test function found in " + name);
  }

  if (typeof testFunction !== "function") {
    throw new Error(
      `Expected (await import(\"${name}\"")) to have a test function.\nReceived: ${Object.keys(
        Namespace
      ).join(", ")} `
    );
  }

  if (globalThis.BUN_DEBUG_MODE) {
    try {
      await testFunction();
      if (!testSuccess) {
        throw new Error("Test failed");
      }
    } catch (exception) {
      console.error(exception);
      debugger;
      throw exception;
    }
  } else {
    await testFunction();
    if (!testSuccess) {
      throw new Error("Test failed");
    }
  }
};

var testSuccess = false;
globalThis.testDone = () => {
  testSuccess = true;
};

let fail = 0;
for (let snippet of snippets) {
  try {
    await runTest("../snippets/" + snippet.substring(1));
    console.log("✅", snippet);
  } catch (exception) {
    console.error(`❌ ${snippet}`);
    console.error(exception);

    fail++;
  }
}

if (fail) throw new Error(`❌ browser test failed (${fail})`);

console.log(`✅ bun.js test passed`);