aboutsummaryrefslogtreecommitdiff
path: root/integration/bunjs-only-snippets
diff options
context:
space:
mode:
Diffstat (limited to 'integration/bunjs-only-snippets')
-rw-r--r--integration/bunjs-only-snippets/setInterval.test.js35
-rw-r--r--integration/bunjs-only-snippets/setTimeout.test.js39
2 files changed, 74 insertions, 0 deletions
diff --git a/integration/bunjs-only-snippets/setInterval.test.js b/integration/bunjs-only-snippets/setInterval.test.js
new file mode 100644
index 000000000..f633998cd
--- /dev/null
+++ b/integration/bunjs-only-snippets/setInterval.test.js
@@ -0,0 +1,35 @@
+import { it, expect } from "bun:test";
+
+it("setInterval", async () => {
+ var counter = 0;
+ var start;
+ const result = await new Promise((resolve, reject) => {
+ start = performance.now();
+
+ var id = setInterval(() => {
+ counter++;
+ if (counter === 10) {
+ resolve(counter);
+ clearInterval(id);
+ }
+ }, 1);
+ });
+
+ expect(result).toBe(10);
+ expect(performance.now() - start >= 10).toBe(true);
+});
+
+it("clearInterval", async () => {
+ var called = false;
+ const id = setInterval(() => {
+ called = true;
+ expect(false).toBe(true);
+ }, 1);
+ clearInterval(id);
+ await new Promise((resolve, reject) => {
+ setInterval(() => {
+ resolve();
+ }, 10);
+ });
+ expect(called).toBe(false);
+});
diff --git a/integration/bunjs-only-snippets/setTimeout.test.js b/integration/bunjs-only-snippets/setTimeout.test.js
new file mode 100644
index 000000000..55f71712c
--- /dev/null
+++ b/integration/bunjs-only-snippets/setTimeout.test.js
@@ -0,0 +1,39 @@
+import { it, expect } from "bun:test";
+
+it("setTimeout", async () => {
+ var lastID = -1;
+ const result = await new Promise((resolve, reject) => {
+ var numbers = [];
+
+ for (let i = 1; i < 100; i++) {
+ const id = setTimeout(() => {
+ numbers.push(i);
+ if (i === 99) {
+ resolve(numbers);
+ }
+ }, i);
+ expect(id > lastID).toBe(true);
+ lastID = id;
+ }
+ });
+
+ for (let j = 0; j < result.length; j++) {
+ expect(result[j]).toBe(j + 1);
+ }
+ expect(result.length).toBe(99);
+});
+
+it("clearTimeout", async () => {
+ var called = false;
+ const id = setTimeout(() => {
+ called = true;
+ expect(false).toBe(true);
+ }, 1);
+ clearTimeout(id);
+ await new Promise((resolve, reject) => {
+ setTimeout(() => {
+ resolve();
+ }, 10);
+ });
+ expect(called).toBe(false);
+});