Skip to content

Commit 9664542

Browse files
committed
test: enhance installDeps tests
1 parent 9546c77 commit 9664542

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

test/deps.test.js

+28-14
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,51 @@
1414

1515
import assert from 'node:assert'
1616
import { test, describe } from 'node:test'
17-
import { $, tmpfile, fs } from '../build/index.js'
17+
import { $, tmpfile, tmpdir, fs, path } from '../build/index.js'
1818
import { installDeps, parseDeps } from '../build/deps.js'
1919

20+
const __dirname = new URL('.', import.meta.url).pathname
21+
const root = path.resolve(__dirname, '..')
22+
const cli = path.resolve(root, 'build/cli.js')
23+
2024
describe('deps', () => {
2125
describe('installDeps()', () => {
26+
const pkgjson = tmpfile(
27+
'package.json',
28+
'{"name": "temp", "version": "0.0.0"}'
29+
)
30+
const cwd = path.dirname(pkgjson)
31+
const t$ = $({ cwd })
32+
const load = (dep) =>
33+
fs.readJsonSync(path.join(cwd, 'node_modules', dep, 'package.json'))
34+
2235
test('loader works via JS API', async () => {
23-
await installDeps({
24-
cpy: '9.0.1',
25-
'lodash-es': '4.17.21',
26-
})
27-
assert((await import('cpy')).default instanceof Function)
28-
assert((await import('lodash-es')).pick instanceof Function)
36+
await installDeps(
37+
{
38+
cpy: '9.0.1',
39+
'lodash-es': '4.17.21',
40+
},
41+
cwd
42+
)
43+
assert(load('cpy').name === 'cpy')
44+
assert(load('lodash-es').name === 'lodash-es')
2945
})
3046

3147
test('loader works via JS API with custom npm registry URL', async () => {
3248
await installDeps(
3349
{
3450
'@jsr/std__internal': '1.0.5',
3551
},
36-
undefined,
52+
cwd,
3753
'https://npm.jsr.io'
3854
)
3955

40-
assert((await import('@jsr/std__internal')).diff instanceof Function)
56+
assert(load('@jsr/std__internal').name === '@jsr/std__internal')
4157
})
4258

4359
test('loader works via CLI', async () => {
4460
const out =
45-
await $`node build/cli.js --install <<< 'import _ from "lodash" /* @4.17.15 */; console.log(_.VERSION)'`
61+
await t$`node ${cli} --install <<< 'import _ from "lodash" /* @4.17.15 */; console.log(_.VERSION)'`
4662
assert.match(out.stdout, /4.17.15/)
4763
})
4864

@@ -51,13 +67,11 @@ describe('deps', () => {
5167
'import { diff } from "@jsr/std__internal";console.log(diff instanceof Function)'
5268
const file = tmpfile('index.mjs', code)
5369

54-
let out =
55-
await $`node build/cli.js --i --registry=https://npm.jsr.io ${file}`
70+
let out = await t$`node ${cli} --i --registry=https://npm.jsr.io ${file}`
5671
fs.remove(file)
5772
assert.match(out.stdout, /true/)
5873

59-
out =
60-
await $`node build/cli.js -i --registry=https://npm.jsr.io <<< ${code}`
74+
out = await t$`node ${cli} -i --registry=https://npm.jsr.io <<< ${code}`
6175
assert.match(out.stdout, /true/)
6276
})
6377
})

0 commit comments

Comments
 (0)