Skip to content

Commit

Permalink
refactor: memory leak test (#880)
Browse files Browse the repository at this point in the history
* refactor: memory leak test

* simplify a bit

* does it run on prd?

* add ts versions
  • Loading branch information
dai-shi committed Apr 7, 2024
1 parent dcf48e4 commit a5eb32b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test-multiple-builds.yml
Expand Up @@ -37,7 +37,6 @@ jobs:
run: |
sed -i~ "s/it[.a-zA-Z]*('\[PRD-ONLY\]/it('/" tests/*.ts tests/*.tsx
sed -i~ "s/it[.a-zA-Z]*('\[DEV-ONLY\]/it.skip('/" tests/*.ts tests/*.tsx
sed -i~ "s/describe(/describe.skip(/" tests/memoryleaks.test.ts # FIXME why it doesn't work in production mode?
- name: Patch for CJS
if: ${{ matrix.build == 'cjs' }}
run: |
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test-old-typescript.yml
Expand Up @@ -13,7 +13,8 @@ jobs:
fail-fast: false
matrix:
typescript:
- 5.3.2
- 5.4.4
- 5.3.3
- 5.2.2
- 5.1.6
- 5.0.4
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -104,8 +104,8 @@
},
"homepage": "https://github.com/pmndrs/valtio",
"dependencies": {
"proxy-compare": "2.6.0",
"derive-valtio": "0.1.0",
"proxy-compare": "2.6.0",
"use-sync-external-store": "1.2.0"
},
"devDependencies": {
Expand Down Expand Up @@ -146,6 +146,7 @@
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-vitest": "^0.3.22",
"jest-leak-detector": "^29.7.0",
"jsdom": "^24.0.0",
"json": "^11.0.0",
"postinstall-postinstall": "^2.1.0",
Expand Down
64 changes: 28 additions & 36 deletions tests/memoryleaks.test.ts
@@ -1,51 +1,43 @@
import { exec } from 'child_process'
import LeakDetector from 'jest-leak-detector'
import { describe, expect, it } from 'vitest'
import { proxy } from 'valtio'

describe('no memory leaks with proxy', () => {
const runTest = async (code: string) => {
const testCode = `
const { proxy } = require("./dist/vanilla.js");
${code}
const registry = new FinalizationRegistry(() => {
console.log("state is garbage collected");
});
registry.register(state, undefined);
state = null;
setImmediate(() => {
global.gc();
});
`
const output = await new Promise((resolve) => {
exec(`node --expose-gc --eval '${testCode}'`, (err, stdout) => {
resolve(err || stdout)
})
})
expect(output).toMatch('state is garbage collected')
}

it('empty object', async () => {
await runTest(`
let state = proxy({});
`)
let detector: LeakDetector
;(() => {
const state = proxy({})
detector = new LeakDetector(state)
})()
expect(await detector.isLeaking()).toBe(false)
})

it('child object', async () => {
await runTest(`
let state = proxy({ child: {} });
`)
let detector: LeakDetector
;(() => {
const state = proxy({ child: {} })
detector = new LeakDetector(state)
})()
expect(await detector.isLeaking()).toBe(false)
})

it('global child object', async () => {
await runTest(`
const child = {};
let state = proxy({ child });
`)
let detector: LeakDetector
;(() => {
const child = {}
const state = proxy({ child })
detector = new LeakDetector(state)
})()
expect(await detector.isLeaking()).toBe(false)
})

it('global child proxy', async () => {
await runTest(`
const child = proxy({});
let state = proxy({ child });
`)
let detector: LeakDetector
;(() => {
const child = proxy({})
const state = proxy({ child })
detector = new LeakDetector(state)
})()
expect(await detector.isLeaking()).toBe(false)
})
})
13 changes: 13 additions & 0 deletions yarn.lock
Expand Up @@ -3697,6 +3697,19 @@ iterator.prototype@^1.1.2:
reflect.getprototypeof "^1.0.4"
set-function-name "^2.0.1"

jest-get-type@^29.6.3:
version "29.6.3"
resolved "https://registry.yarnpkg.com/jest-get-type/-/jest-get-type-29.6.3.tgz#36f499fdcea197c1045a127319c0481723908fd1"
integrity sha512-zrteXnqYxfQh7l5FHyL38jL39di8H8rHoecLH3JNxH3BwOrBsNeabdap5e0I23lD4HHI8W5VFBZqG4Eaq5LNcw==

jest-leak-detector@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-leak-detector/-/jest-leak-detector-29.7.0.tgz#5b7ec0dadfdfec0ca383dc9aa016d36b5ea4c728"
integrity sha512-kYA8IJcSYtST2BY9I+SMC32nDpBT3J2NvWJx8+JCuCdl/CR1I4EKUJROiP8XtCcxqgTTBGJNdbB1A8XRKbTetw==
dependencies:
jest-get-type "^29.6.3"
pretty-format "^29.7.0"

"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
Expand Down

0 comments on commit a5eb32b

Please sign in to comment.