Skip to content

Commit

Permalink
update tests to pass on node <20
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Nov 11, 2023
1 parent 32fb439 commit 6105ea7
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/.tap
/node_modules
/dist
/docs
14 changes: 7 additions & 7 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/node_modules
/tap-snapshots
/coverage
/.nyc_output
/benchmark
/.github
/scripts
/CHANGELOG.md
/docs
/.tap
/*.json
/example
/.github
/dist
/tap-snapshots
/benchmark
/.tshy
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
## 9.0.0

- Use named export only, no default export.
- Bring back minimal polyfill. If this polyfill ends up being
- Bring back minimal polyfill. If this polyfill ends up being
used, then a warning is printed, as it is not safe for use
outside of LRUCache.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ For example:
```ts
const cache = new LRUCache<string, any>({
ttl: 100,
fetchMethod: async (url, oldValue, { signal }) => {
fetchMethod: async (url, oldValue, { signal }) => {
const res = await fetch(url, { signal })
return await res.json()
}
},
})
cache.set('https://example.com/', { some: 'data' })
// 100ms go by...
Expand Down
75 changes: 0 additions & 75 deletions scripts/max-size-test.js

This file was deleted.

4 changes: 2 additions & 2 deletions test/avoid-memory-leak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// https://github.com/isaacs/node-lru-cache/issues/227

import t, {Test} from 'tap'
import t, { Test } from 'tap'
import { expose } from './fixtures/expose.js'

const maxSize = 100_000
Expand All @@ -28,7 +28,7 @@ const tryReq = (mod: string) => {
const v8 = tryReq('v8')

import { LRUCache } from '../dist/esm/index.js'
import {createRequire} from 'module'
import { createRequire } from 'module'
const expectItemCount = Math.ceil(maxSize / itemSize)
const max = expectItemCount + 1
const keyRange = expectItemCount * 2
Expand Down
4 changes: 2 additions & 2 deletions test/move-to-tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ t.test('list integrity', { bail: true }, t => {
t.test(msg, { bail: false }, t => {
for (let i = 0; i < c.max; i++) {
if (i !== exp.head) {
t.equal(exp.next[(exp.prev[i] as number)], i, 'n[p[i]] === i')
t.equal(exp.next[exp.prev[i] as number], i, 'n[p[i]] === i')
}
if (i !== exp.tail) {
t.equal(exp.prev[(exp.next[i] as number)], i, 'p[n[i]] === i')
t.equal(exp.prev[exp.next[i] as number], i, 'p[n[i]] === i')
}
}
t.end()
Expand Down
71 changes: 48 additions & 23 deletions test/warn-missing-ac.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {fileURLToPath} from 'url'
import { createRequire } from 'module'
import { fileURLToPath } from 'url'

export {}
const __filename = fileURLToPath(import.meta.url)
const main = async () => {
const { default: t } = await import('tap')
Expand All @@ -9,32 +9,46 @@ const main = async () => {
// need to run both tests in parallel so we don't miss the close event
t.jobs = 3

const warn = spawn(process.execPath, [
...process.execArgv,
__filename,
'child',
])
const argv = process.execArgv.filter(
a => !a.startsWith('--no-warnings')
)
const warn = spawn(
process.execPath,
[...argv, __filename, 'child'],
{
env: {
...process.env,
NODE_OPTIONS: '',
},
}
)
const warnErr: Buffer[] = []
warn.stderr.on('data', c => warnErr.push(c))

const noWarn = spawn(
process.execPath,
[...process.execArgv, __filename, 'child'],
[...argv, __filename, 'child'],
{
env: {
...process.env,
LRU_CACHE_IGNORE_AC_WARNING: '1',
NODE_OPTIONS: '',
},
}
)
const noWarnErr: Buffer[] = []
noWarn.stderr.on('data', c => noWarnErr.push(c))

const noFetch = spawn(process.execPath, [
...process.execArgv,
__filename,
'child-no-fetch',
])
const noFetch = spawn(
process.execPath,
[...argv, __filename, 'child-no-fetch'],
{
env: {
...process.env,
NODE_OPTIONS: '',
},
}
)
const noFetchErr: Buffer[] = []
noFetch.stderr.on('data', c => noFetchErr.push(c))

Expand All @@ -46,7 +60,10 @@ const main = async () => {
r()
})
)
t.equal(Buffer.concat(noWarnErr).toString().trim(), '')
t.notMatch(
Buffer.concat(noWarnErr).toString().trim(),
'NO_ABORT_CONTROLLER'
)
})

t.test('no warning (because no fetch)', async t => {
Expand All @@ -57,7 +74,10 @@ const main = async () => {
r()
})
)
t.equal(Buffer.concat(noFetchErr).toString().trim(), '')
t.notMatch(
Buffer.concat(noWarnErr).toString().trim(),
'NO_ABORT_CONTROLLER'
)
})

t.test('warning', async t => {
Expand All @@ -68,24 +88,29 @@ const main = async () => {
r()
})
)
t.not(Buffer.concat(warnErr).toString().trim(), '')
t.match(
Buffer.concat(warnErr).toString().trim(),
/NO_ABORT_CONTROLLER/
)
})
}

switch (process.argv[2]) {
case 'child':
//@ts-ignore
//@ts-expect-error
process.emitWarning = null
//@ts-expect-error
globalThis.AbortController = undefined
//@ts-ignore
//@ts-expect-error
globalThis.AbortSignal = undefined
import('../dist/esm/index.js').then(({ LRUCache }) => {
new LRUCache({ max: 1, fetchMethod: async () => 1 }).fetch(1)
})
const req = createRequire(import.meta.url)
const { LRUCache } = req('../dist/commonjs/index.js')
new LRUCache({ max: 1, fetchMethod: async () => 1 }).fetch(1)
break
case 'child-no-fetch':
//@ts-ignore
//@ts-expect-error
globalThis.AbortController = undefined
//@ts-ignore
//@ts-expect-error
globalThis.AbortSignal = undefined
import('../dist/esm/index.js')
break
Expand Down

0 comments on commit 6105ea7

Please sign in to comment.