14
14
15
15
import assert from 'node:assert'
16
16
import { test , describe } from 'node:test'
17
- import { $ , tmpfile , fs } from '../build/index.js'
17
+ import { $ , tmpfile , tmpdir , fs , path } from '../build/index.js'
18
18
import { installDeps , parseDeps } from '../build/deps.js'
19
19
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
+
20
24
describe ( 'deps' , ( ) => {
21
25
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
+
22
35
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' )
29
45
} )
30
46
31
47
test ( 'loader works via JS API with custom npm registry URL' , async ( ) => {
32
48
await installDeps (
33
49
{
34
50
'@jsr/std__internal' : '1.0.5' ,
35
51
} ,
36
- undefined ,
52
+ cwd ,
37
53
'https://npm.jsr.io'
38
54
)
39
55
40
- assert ( ( await import ( '@jsr/std__internal' ) ) . diff instanceof Function )
56
+ assert ( load ( '@jsr/std__internal' ) . name === '@jsr/std__internal' )
41
57
} )
42
58
43
59
test ( 'loader works via CLI' , async ( ) => {
44
60
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)'`
46
62
assert . match ( out . stdout , / 4 .1 7 .1 5 / )
47
63
} )
48
64
@@ -51,13 +67,11 @@ describe('deps', () => {
51
67
'import { diff } from "@jsr/std__internal";console.log(diff instanceof Function)'
52
68
const file = tmpfile ( 'index.mjs' , code )
53
69
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 } `
56
71
fs . remove ( file )
57
72
assert . match ( out . stdout , / t r u e / )
58
73
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 } `
61
75
assert . match ( out . stdout , / t r u e / )
62
76
} )
63
77
} )
0 commit comments