Skip to content

Commit 12d24f3

Browse files
committed
refactor(tests): convert lazy require calls to static ES6 imports
- Convert json.test.ts: hoist fs/os/path imports and safeDelete/getEditableJsonClass to top-level - Convert strings.test.ts: remove redundant require calls (already imported) - Convert dlx/package.test.ts: add readFileSync to imports, remove inline requires - Improves consistency with ES6 module patterns - Better for tree-shaking and static analysis
1 parent ae87882 commit 12d24f3

File tree

3 files changed

+11
-27
lines changed

3 files changed

+11
-27
lines changed

test/unit/dlx/package.test.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*/
1212

1313
import { createHash } from 'node:crypto'
14-
import { existsSync, mkdirSync, writeFileSync } from 'node:fs'
14+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
1515
import path from 'node:path'
1616
import { describe, expect, it } from 'vitest'
1717

@@ -567,10 +567,7 @@ describe('dlx-package', () => {
567567
// Reading package.json should work but bin field is missing
568568
expect(existsSync(path.join(nodeModules, 'package.json'))).toBe(true)
569569
const pkg = JSON.parse(
570-
require('node:fs').readFileSync(
571-
path.join(nodeModules, 'package.json'),
572-
'utf8',
573-
),
570+
readFileSync(path.join(nodeModules, 'package.json'), 'utf8'),
574571
)
575572
expect(pkg.bin).toBeUndefined()
576573
}, 'dlx-pkg-missing-')
@@ -602,10 +599,7 @@ describe('dlx-package', () => {
602599
// Should auto-select the single binary
603600
expect(existsSync(path.join(nodeModules, 'cli.js'))).toBe(true)
604601
const pkg = JSON.parse(
605-
require('node:fs').readFileSync(
606-
path.join(nodeModules, 'package.json'),
607-
'utf8',
608-
),
602+
readFileSync(path.join(nodeModules, 'package.json'), 'utf8'),
609603
)
610604
expect(typeof pkg.bin).toBe('string')
611605
expect(pkg.bin).toBe('./cli.js')
@@ -658,10 +652,7 @@ describe('dlx-package', () => {
658652

659653
// Should find the binary matching last segment of package name
660654
const pkg = JSON.parse(
661-
require('node:fs').readFileSync(
662-
path.join(nodeModules, 'package.json'),
663-
'utf8',
664-
),
655+
readFileSync(path.join(nodeModules, 'package.json'), 'utf8'),
665656
)
666657
expect(pkg.bin['multi-bin']).toBe('./bin/main.js')
667658

@@ -706,10 +697,7 @@ describe('dlx-package', () => {
706697

707698
// Should fall back to first binary (other-a)
708699
const pkg = JSON.parse(
709-
require('node:fs').readFileSync(
710-
path.join(nodeModules, 'package.json'),
711-
'utf8',
712-
),
700+
readFileSync(path.join(nodeModules, 'package.json'), 'utf8'),
713701
)
714702
const firstBinary = Object.keys(pkg.bin)[0]
715703
expect(firstBinary).toBe('other-a')

test/unit/json.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313
* including empty strings, special characters, unicode, and very large JSON payloads.
1414
*/
1515

16+
import { mkdtemp, readFile, writeFile } from 'node:fs/promises'
17+
import { tmpdir } from 'node:os'
18+
import { join } from 'node:path'
19+
20+
import { safeDelete } from '@socketsecurity/lib/fs'
21+
import { getEditableJsonClass } from '@socketsecurity/lib/json/edit'
1622
import { isJsonPrimitive, jsonParse } from '@socketsecurity/lib/json/parse'
1723
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
1824

@@ -752,14 +758,6 @@ describe('json', () => {
752758
})
753759

754760
describe('EditableJson', () => {
755-
const { tmpdir } = require('node:os')
756-
const { join } = require('node:path')
757-
const { mkdtemp, readFile, writeFile } = require('node:fs/promises')
758-
const { safeDelete } =
759-
require('@socketsecurity/lib/fs') as typeof import('@socketsecurity/lib/fs')
760-
const { getEditableJsonClass } =
761-
require('@socketsecurity/lib/json/edit') as typeof import('@socketsecurity/lib/json/edit')
762-
763761
let testDir: string
764762

765763
beforeEach(async () => {

test/unit/strings.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,12 +448,10 @@ describe('strings', () => {
448448

449449
describe('fromCharCode', () => {
450450
it('should be exported', () => {
451-
const { fromCharCode } = require('@socketsecurity/lib/strings')
452451
expect(typeof fromCharCode).toBe('function')
453452
})
454453

455454
it('should convert char codes to strings', () => {
456-
const { fromCharCode } = require('@socketsecurity/lib/strings')
457455
expect(fromCharCode(65)).toBe('A')
458456
expect(fromCharCode(97)).toBe('a')
459457
expect(fromCharCode(48)).toBe('0')

0 commit comments

Comments
 (0)