Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get bun test working locally #2204

Merged
merged 8 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
590 changes: 295 additions & 295 deletions __tests__/core/__snapshots__/async.test.ts.snap

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions __tests__/core/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ test("middleware events are correct", () => {
const event1 = {
args: [7],
context: {},
id: process.env.NODE_ENV !== "production" ? 29 : 28,
id: 1,
name: "a",
parentId: 0,
rootId: process.env.NODE_ENV !== "production" ? 29 : 28,
rootId: 1,
allParentIds: [],
tree: {},
type: "action",
Expand All @@ -350,11 +350,11 @@ test("middleware events are correct", () => {
const event2 = {
args: [14],
context: {},
id: process.env.NODE_ENV !== "production" ? 30 : 29,
id: 2,
name: "b",
parentId: process.env.NODE_ENV !== "production" ? 29 : 28,
rootId: process.env.NODE_ENV !== "production" ? 29 : 28,
allParentIds: [process.env.NODE_ENV !== "production" ? 29 : 28],
parentId: 1,
rootId: 1,
allParentIds: [1],
tree: {},
type: "action",
parentEvent: event1,
Expand Down
26 changes: 13 additions & 13 deletions __tests__/core/actionTrackingMiddleware2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ async function flowTest(mode: "success" | "fail") {

if (mode === "success") {
expect(calls).toEqual([
"setY (9) - onStart",
"setX (11) - onStart",
"setX (11) - onFinish (error: false)",
"setY (9) - onFinish (error: false)"
"setY (3) - onStart",
"setX (5) - onStart",
"setX (5) - onFinish (error: false)",
"setY (3) - onFinish (error: false)"
])
} else {
expect(calls).toEqual([
"setY (16) - onStart",
"setX (18) - onStart",
"setX (18) - onFinish (error: true)",
"setY (16) - onFinish (error: true)"
"setY (10) - onStart",
"setX (12) - onStart",
"setX (12) - onFinish (error: true)",
"setY (10) - onFinish (error: true)"
])
}
}
Expand Down Expand Up @@ -213,7 +213,7 @@ test("#1250", async () => {
const p = model.setX()
expect(model.x).toBe(10)
expect(model.y).toBe(0)
expect(calls).toEqual(["setX (21) <- (undefined) - filter", "setX (21) <- (undefined) - onStart"])
expect(calls).toEqual(["setX (1) <- (undefined) - filter", "setX (1) <- (undefined) - onStart"])
calls.length = 0

await new Promise<void>((r) =>
Expand All @@ -225,16 +225,16 @@ test("#1250", async () => {
expect(model.x).toBe(10)
expect(model.y).toBe(10)
expect(calls).toEqual([
"setY (23) <- (undefined) - filter",
"setY (23) <- (undefined) - onStart",
"setY (23) <- (undefined) - onFinish (error: false)"
"setY (3) <- (undefined) - filter",
"setY (3) <- (undefined) - onStart",
"setY (3) <- (undefined) - onFinish (error: false)"
])
calls.length = 0

await p
expect(model.x).toBe(10)
expect(model.y).toBe(10)
expect(calls).toEqual(["setX (21) <- (undefined) - onFinish (error: false)"])
expect(calls).toEqual(["setX (1) <- (undefined) - onFinish (error: false)"])
calls.length = 0
})

Expand Down
4 changes: 0 additions & 4 deletions __tests__/core/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,6 @@ test("it should support observable arrays", () => {
})

test("it should support observable arrays, array should be real when useProxies eq 'always'", () => {
configure({
useProxies: "always"
})

const TestArray = types.array(types.number)
const testArray = TestArray.create(observable([1, 2]))
expect(testArray[0] === 1).toBe(true)
Expand Down
150 changes: 67 additions & 83 deletions __tests__/core/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
types
} from "../../src"
import { expect, test } from "bun:test"
import type { Writable } from "ts-essentials"

function delay<TV>(time: number, value: TV, shouldThrow = false): Promise<TV> {
return new Promise((resolve, reject) => {
Expand All @@ -24,54 +25,51 @@ function delay<TV>(time: number, value: TV, shouldThrow = false): Promise<TV> {
})
}

function testCoffeeTodo(
done: () => void,
async function testCoffeeTodo(
generator: (
self: any
) => (str: string) => Generator<Promise<any>, string | void | undefined, undefined>,
shouldError: boolean,
resultValue: string | undefined,
producedCoffees: any[]
) {
configure({ enforceActions: "observed" })
const Todo = types
.model({
title: "get coffee"
})
.actions((self) => ({
startFetch: flow(generator(self))
}))

const events: IMiddlewareEvent[] = []
const coffees: any[] = []
const t1 = Todo.create({})
addMiddleware(t1, (c, next) => {
events.push(c)
return next(c)
})

const coffees: any[] = []
reaction(
() => t1.title,
(coffee) => coffees.push(coffee)
)
function handleResult(res: string | undefined | void) {
expect(res).toBe(resultValue)
expect(coffees).toEqual(producedCoffees)
const filtered = filterRelevantStuff(events)
expect(filtered).toMatchSnapshot()

try {
configure({ enforceActions: "observed" })
const result = await t1.startFetch("black")
expect(shouldError).toBe(false)
expect(result).toBe(resultValue)
} catch (error) {
expect(shouldError).toBe(true)
} finally {
configure({ enforceActions: "never" })
done()
}
t1.startFetch("black").then(
(r) => {
expect(shouldError).toBe(false)
handleResult(r)
},
(r) => {
expect(shouldError).toBe(true)
handleResult(r)
}
)

expect(coffees).toEqual(producedCoffees)
const filtered = filterRelevantStuff(events)
expect(filtered).toMatchSnapshot()
}
test("flow happens in single ticks", (done) => {
test("flow happens in single ticks", async () => {
const X = types
.model({
y: 1
Expand All @@ -91,15 +89,13 @@ test("flow happens in single ticks", (done) => {
() => x.y,
(v) => values.push(v)
)
x.p().then(() => {
expect(x.y).toBe(5)
expect(values).toEqual([3, 5])
done()
})

await x.p()
expect(x.y).toBe(5)
expect(values).toEqual([3, 5])
})
test("can handle async actions", (done) => {
test("can handle async actions", () => {
testCoffeeTodo(
done,
(self) =>
function* fetchData(kind: string) {
self.title = "getting coffee " + kind
Expand All @@ -111,9 +107,8 @@ test("can handle async actions", (done) => {
["getting coffee black", "drinking coffee"]
)
})
test("can handle erroring actions", (done) => {
test("can handle erroring actions", () => {
testCoffeeTodo(
done,
(self) =>
function* fetchData(kind: string) {
throw kind
Expand All @@ -123,9 +118,8 @@ test("can handle erroring actions", (done) => {
[]
)
})
test("can handle try catch", (t) => {
test("can handle try catch", () => {
testCoffeeTodo(
t,
(self) =>
function* fetchData(kind: string) {
try {
Expand All @@ -141,12 +135,11 @@ test("can handle try catch", (t) => {
["tea"]
)
})
test("empty sequence works", (t) => {
testCoffeeTodo(t, () => function* fetchData(kind: string) {}, false, undefined, [])
test("empty sequence works", () => {
testCoffeeTodo(() => function* fetchData(kind: string) {}, false, undefined, [])
})
test("can handle throw from yielded promise works", (t) => {
test("can handle throw from yielded promise works", () => {
testCoffeeTodo(
t,
() =>
function* fetchData(kind: string) {
yield delay(10, "x", true)
Expand All @@ -156,7 +149,7 @@ test("can handle throw from yielded promise works", (t) => {
[]
)
})
test("typings", (done) => {
test("typings", async () => {
const M = types.model({ title: types.string }).actions((self) => {
function* a(x: string) {
yield delay(10, "x", false)
Expand All @@ -174,13 +167,11 @@ test("typings", (done) => {
const m1 = M.create({ title: "test " })
const resA = m1.a("z")
const resB = m1.b("z")
Promise.all([resA, resB]).then(([x1, x2]) => {
expect(x1).toBe(23)
expect(x2).toBe(24)
done()
})
const [x1, x2] = await Promise.all([resA, resB])
expect(x1).toBe(23)
expect(x2).toBe(24)
})
test("typings", (done) => {
test("typings", async () => {
const M = types.model({ title: types.string }).actions((self) => {
function* a(x: string) {
yield delay(10, "x", false)
Expand All @@ -198,13 +189,11 @@ test("typings", (done) => {
const m1 = M.create({ title: "test " })
const resA = m1.a("z")
const resB = m1.b("z")
Promise.all([resA, resB]).then(([x1, x2]) => {
expect(x1).toBe(23)
expect(x2).toBe(24)
done()
})
const [x1, x2] = await Promise.all([resA, resB])
expect(x1).toBe(23)
expect(x2).toBe(24)
})
test("recordActions should only emit invocation", (done) => {
test("recordActions should only emit invocation", async () => {
let calls = 0
const M = types
.model({
Expand All @@ -222,31 +211,29 @@ test("recordActions should only emit invocation", (done) => {
})
const m1 = M.create({ title: "test " })
const recorder = recordActions(m1)
m1.a("x").then(() => {
recorder.stop()
expect(recorder.actions).toEqual([
{
args: ["x"],
name: "a",
path: ""
}
])
expect(calls).toBe(1)
recorder.replay(m1)
setTimeout(() => {
expect(calls).toBe(2)
done()
}, 50)
})
await m1.a("x")

recorder.stop()
expect(recorder.actions).toEqual([
{
args: ["x"],
name: "a",
path: ""
}
])
expect(calls).toBe(1)
recorder.replay(m1)

await new Promise((resolve) => setTimeout(resolve, 50))
expect(calls).toBe(2)
})
test("can handle nested async actions", (t) => {
test("can handle nested async actions", () => {
// tslint:disable-next-line:no-shadowed-variable
const uppercase = flow(function* uppercase(value: string) {
const res = yield delay(20, value.toUpperCase())
return res
})
testCoffeeTodo(
t,
(self) =>
function* fetchData(kind: string) {
self.title = yield uppercase("drinking " + kind)
Expand All @@ -257,7 +244,7 @@ test("can handle nested async actions", (t) => {
["DRINKING BLACK"]
)
})
test("can handle nested async actions when using decorate", (done) => {
test("can handle nested async actions when using decorate", async () => {
const events: [IMiddlewareEventType, string][] = []
const middleware: IMiddlewareHandler = (call, next) => {
events.push([call.type, call.name])
Expand All @@ -275,19 +262,16 @@ test("can handle nested async actions when using decorate", (done) => {
})
return { act: decorate(middleware, act) }
})
Todo.create()
.act("x")
.then((res) => {
expect(res).toBe("X")
expect(events).toEqual([
["action", "act"],
["flow_spawn", "act"],
["flow_resume", "act"],
["flow_resume", "act"],
["flow_return", "act"]
])
done()
})

const res = await Todo.create().act("x")
expect(res).toBe("X")
expect(events).toEqual([
["action", "act"],
["flow_spawn", "act"],
["flow_resume", "act"],
["flow_resume", "act"],
["flow_return", "act"]
])
})

test("flow gain back control when node become not alive during yield", async () => {
Expand Down Expand Up @@ -318,8 +302,8 @@ test("flow gain back control when node become not alive during yield", async ()
}
})

function filterRelevantStuff(stuff: IMiddlewareEvent[]) {
return stuff.map((x: any) => {
function filterRelevantStuff(stuff: Partial<Writable<IMiddlewareEvent>>[]) {
return stuff.map((x) => {
delete x.context
delete x.tree
return x
Expand Down
Loading