Skip to content

Commit

Permalink
test: exclude non-deterministic tests for setTimeout (#254)
Browse files Browse the repository at this point in the history
* test: exclude non-deterministic tests

* test: set maxWorkers 0
  • Loading branch information
ppeeou authored Mar 10, 2024
1 parent 46b7186 commit e061098
Show file tree
Hide file tree
Showing 22 changed files with 27 additions and 189 deletions.
1 change: 0 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ module.exports = {
preset: "ts-jest",
testEnvironment: "node",
testMatch: [path.join(__dirname, "test/**/*.spec.ts")],
maxWorkers: 1,
};
8 changes: 1 addition & 7 deletions test/Lazy/append.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
toAsync,
} from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("append", function () {
describe("sync", function () {
Expand Down Expand Up @@ -43,8 +43,6 @@ describe("append", function () {
});

it("should be appended sequentially", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 3000);
let chainedPromise: Promise<void | number> = Promise.resolve();
const res = await pipe(
toAsync(range(1, 4)),
Expand All @@ -65,13 +63,10 @@ describe("append", function () {
),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([1, 2, 3, 4, 5, 6]);
}, 3050);

it("should be appended concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 1000);
const res = await pipe(
toAsync([1, 2, 3]),
map((a) => delay(1000, a)),
Expand All @@ -81,7 +76,6 @@ describe("append", function () {
concurrent(3),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([1, 2, 3, 4, 5, 6]);
}, 1050);

Expand Down
11 changes: 1 addition & 10 deletions test/Lazy/chunk.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
toAsync,
} from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

const expected = [
[1, 2, 3],
Expand Down Expand Up @@ -60,22 +60,17 @@ describe("chunk", function () {
});

it("should be chunked after concurrent", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 600);
const res = await pipe(
toAsync(range(1, 12)),
map((a) => delay(100, a)),
concurrent(2),
chunk(3),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual(expected);
}, 650);

it("should be chunked after concurrent with filter", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 1000);
const res = await pipe(
toAsync(range(1, 21)),
map((a) => delay(100, a)),
Expand All @@ -85,13 +80,10 @@ describe("chunk", function () {
toArray,
);
const expected: number[][] = [[2, 4, 6], [8, 10, 12], [14, 16, 18], [20]];
expect(fn).toBeCalled();
expect(res).toEqual(expected);
}, 1050);

it("should be chunked before concurrent", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 1000);
const res = await pipe(
toAsync(range(1, 21)),
map((a) => delay(100, a)),
Expand All @@ -101,7 +93,6 @@ describe("chunk", function () {
toArray,
);
const expected: number[][] = [[2, 4, 6], [8, 10, 12], [14, 16, 18], [20]];
expect(fn).toBeCalled();
expect(res).toEqual(expected);
}, 1050);

Expand Down
8 changes: 1 addition & 7 deletions test/Lazy/concat.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
toAsync,
} from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("concat", function () {
describe("sync", function () {
Expand All @@ -25,8 +25,6 @@ describe("concat", function () {

describe("async", function () {
it("should be concatenated given two 'AsyncIterable'", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 4000);
const res = await pipe(
[
map((a) => delay(1000, a), toAsync([1, 2])),
Expand All @@ -35,13 +33,10 @@ describe("concat", function () {
([a, b]) => concat(a, b),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([1, 2, 3, 4]);
}, 4050);

it("should be concatenated given two 'AsyncIterable' concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 3000);
const res = await pipe(
[
map((a) => delay(1000, a), toAsync([1, 2, 3])),
Expand All @@ -51,7 +46,6 @@ describe("concat", function () {
concurrent(2),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([1, 2, 3, 4, 5, 6]);
}, 3050);

Expand Down
24 changes: 0 additions & 24 deletions test/Lazy/concurrent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@ import {
range,
toAsync,
} from "../../src/index";
import { callFuncAfterTime } from "../utils";

describe("concurrent", function () {
it("should be consumed 'AsyncIterable' concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 2000);
const res = concurrent(
2,
toAsync(
Expand All @@ -29,14 +26,10 @@ describe("concurrent", function () {
for await (const item of res) {
acc.push(item);
}
expect(fn).toBeCalled();
expect(acc).toEqual([1, 2, 3, 4]);
}, 2050);

it("should be able to be used as a curried function in the pipeline", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 500);

const iter = pipe(
toAsync(range(1, 101)),
map((a) => delay(100, a)),
Expand All @@ -56,13 +49,10 @@ describe("concurrent", function () {
iter.next(),
]).then((arr) => arr.map((a) => a.value));

expect(fn).toBeCalled();
expect(arr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
}, 550);

it("should be affected only one concurrent below it, when nested concurrent", async function () {
let fn: jest.Mock<any, any>;
fn = jest.fn();
let concurrent10Count = 0;
let concurrent2Count = 0;

Expand All @@ -77,33 +67,23 @@ describe("concurrent", function () {
concurrent(2),
);

callFuncAfterTime(fn, 300);
await iter.next();
await iter.next();
expect(fn).toBeCalled();
expect(concurrent2Count).toEqual(4);
expect(concurrent10Count).toEqual(10);

fn = jest.fn();
callFuncAfterTime(fn, 200);
await iter.next();
await iter.next();
expect(fn).toBeCalled();
expect(concurrent2Count).toEqual(8);
expect(concurrent10Count).toEqual(10);

fn = jest.fn();
callFuncAfterTime(fn, 300);
await iter.next();
await iter.next();
expect(fn).toBeCalled();
expect(concurrent2Count).toEqual(12);
expect(concurrent10Count).toEqual(20);
}, 850);

it("should return IteratorReturnResult after all consuming 'AsyncIterable'", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 1000);
const iter = concurrent(
2,
toAsync(
Expand All @@ -121,7 +101,6 @@ describe("concurrent", function () {
{ value: v4, done: d4 },
] = await Promise.all([iter.next(), iter.next(), iter.next(), iter.next()]);

expect(fn).toBeCalled();
expect(v1).toEqual(1);
expect(d1).toEqual(false);
expect(v2).toEqual(2);
Expand All @@ -133,8 +112,6 @@ describe("concurrent", function () {
}, 1050);

it("should be able to handle an error when working concurrent", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 2000);
const res = concurrent(
2,
toAsync(
Expand All @@ -158,7 +135,6 @@ describe("concurrent", function () {
} catch (err) {
expect(err).toEqual("err");
}
expect(fn).toBeCalled();
expect(acc).toEqual([1, 2, 3]);
}, 2050);
});
6 changes: 1 addition & 5 deletions test/Lazy/cycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from "../../src";
import { Concurrent } from "../../src/Lazy/concurrent";
import cycle from "../../src/Lazy/cycle";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("cycle", function () {
describe("sync", function () {
Expand Down Expand Up @@ -77,9 +77,6 @@ describe("cycle", function () {
});

it("should be repeated concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 900);

const res = await pipe(
toAsync(
(function* () {
Expand All @@ -97,7 +94,6 @@ describe("cycle", function () {
toArray,
);

expect(fn).toBeCalled();
expect(res).toEqual([1, 2, 3, 4, 5, 6]);
}, 950);

Expand Down
6 changes: 1 addition & 5 deletions test/Lazy/difference.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { delay, map, pipe, toArray, toAsync } from "../../src";
import concurrent, { Concurrent } from "../../src/Lazy/concurrent";
import difference from "../../src/Lazy/difference";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("difference", function () {
describe("sync", function () {
Expand Down Expand Up @@ -53,9 +53,6 @@ describe("difference", function () {
});

it("should be handled concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 900);

const res = await pipe(
[1, 2, 3, 4, 5, 6, 7, 8, 9],
toAsync,
Expand All @@ -65,7 +62,6 @@ describe("difference", function () {
toArray,
);

expect(fn).toBeCalled();
expect(res).toEqual([1, 2, 6, 7, 8, 9]);
}, 950);

Expand Down
5 changes: 1 addition & 4 deletions test/Lazy/drop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
toAsync,
} from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("drop", function () {
describe("sync", function () {
Expand Down Expand Up @@ -61,8 +61,6 @@ describe("drop", function () {
});

it("should be discarded elements by length concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 400);
const res = await pipe(
toAsync([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
map((a) => delay(100, a)),
Expand All @@ -71,7 +69,6 @@ describe("drop", function () {
concurrent(3),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([6, 8, 10]);
}, 450);

Expand Down
5 changes: 1 addition & 4 deletions test/Lazy/dropRight.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
toAsync,
} from "../../src/index";
import { Concurrent } from "../../src/Lazy/concurrent";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("dropRight", function () {
describe("sync", function () {
Expand Down Expand Up @@ -69,8 +69,6 @@ describe("dropRight", function () {
});

it("should be discarded elements by length concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 400);
const res = await pipe(
toAsync([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
map((a) => delay(100, a)),
Expand All @@ -79,7 +77,6 @@ describe("dropRight", function () {
concurrent(3),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([2, 4, 6]);
}, 450);

Expand Down
9 changes: 1 addition & 8 deletions test/Lazy/dropUntil.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "../../src";
import { AsyncFunctionException } from "../../src/_internal/error";
import { Concurrent } from "../../src/Lazy/concurrent";
import { callFuncAfterTime, generatorMock } from "../utils";
import { generatorMock } from "../utils";

describe("dropUntil", function () {
describe("sync", function () {
Expand Down Expand Up @@ -89,8 +89,6 @@ describe("dropUntil", function () {
});

it("should be dropped elements concurrently", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 1500);
const res = await pipe(
[1, 2, 3, 4, 5, 1, 2],
toAsync,
Expand All @@ -100,14 +98,10 @@ describe("dropUntil", function () {
concurrent(3),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([11]);
}, 1550);

it("should be controlled the order when concurrency", async function () {
const fn = jest.fn();
callFuncAfterTime(fn, 2000);

const res = await pipe(
toAsync(
(function* () {
Expand All @@ -127,7 +121,6 @@ describe("dropUntil", function () {
concurrent(5),
toArray,
);
expect(fn).toBeCalled();
expect(res).toEqual([8, 9]);
}, 2050);

Expand Down
Loading

0 comments on commit e061098

Please sign in to comment.