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

refactor: memory leak test #880

Merged
merged 4 commits into from
Apr 7, 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
1 change: 0 additions & 1 deletion .github/workflows/test-multiple-builds.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading