Skip to content

Commit

Permalink
chore(test): add memory leak tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 16, 2025
1 parent 45feab1 commit 6f32ea1
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions tests/vanilla/memoryleaks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, it } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'
import type { Atom } from 'jotai/vanilla'

describe('test memory leaks (get & set only)', () => {
describe('memory leaks (get & set only)', () => {
it('one atom', async () => {
const store = createStore()
let objAtom: Atom<object> | undefined = atom({})
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('test memory leaks (get & set only)', () => {
})
})

describe('test memory leaks (with subscribe)', () => {
describe('memory leaks (with subscribe)', () => {
it('one atom', async () => {
const store = createStore()
let objAtom: Atom<object> | undefined = atom({})
Expand Down Expand Up @@ -91,3 +91,45 @@ describe('test memory leaks (with subscribe)', () => {
expect(await detector.isLeaking()).toBe(false)
})
})

describe('memory leaks (with dependencies)', () => {
it('sync dependency', async () => {
const store = createStore()
let objAtom: Atom<object> | undefined = atom({})
const detector = new LeakDetector(store.get(objAtom))
const atom1 = atom(0)
const atom2 = atom((get) => get(atom1) || (objAtom && get(objAtom)))
store.sub(atom2, () => {})
store.set(atom1, 1)
objAtom = undefined
expect(await detector.isLeaking()).toBe(false)
})

it('async dependency', async () => {
const store = createStore()
let objAtom: Atom<object> | undefined = atom({})
const detector = new LeakDetector(store.get(objAtom))
const atom1 = atom(0)
const atom2 = atom(async (get) => get(atom1) || (objAtom && get(objAtom)))
store.sub(atom2, () => {})
store.set(atom1, 1)
objAtom = undefined
await Promise.resolve()
expect(await detector.isLeaking()).toBe(false)
})

it('async await dependency', async () => {
const store = createStore()
let objAtom: Atom<object> | undefined = atom({})
const detector = new LeakDetector(store.get(objAtom))
const atom1 = atom(0)
const atom2 = atom(async (get) => {
await Promise.resolve()
return get(atom1) || (objAtom && get(objAtom))
})
store.sub(atom2, () => {})
store.set(atom1, 1)
objAtom = undefined
expect(await detector.isLeaking()).toBe(false)
})
})

0 comments on commit 6f32ea1

Please sign in to comment.