11import * as vm from 'node:vm' ;
2- import { notStrictEqual , strictEqual , throws } from 'node:assert' ;
2+ import { doesNotThrow , notStrictEqual , strictEqual , throws } from 'node:assert' ;
33
44export const vmTest = {
55 test ( ) {
@@ -33,10 +33,17 @@ export const vmTest = {
3333 const context = vm . createContext ( ) ;
3434 strictEqual ( vm . isContext ( context ) , true ) ;
3535
36- // Basic construction should validate and throw NOT_IMPLEMENTED
37- throws ( ( ) => new vm . Script ( 'code' ) , {
36+ // Basic construction should return a Script object that throws on run methods
37+ const script1 = new vm . Script ( 'code' ) ;
38+ strictEqual ( script1 instanceof vm . Script , true ) ;
39+ throws ( ( ) => script1 . runInContext ( context ) , {
40+ code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
41+ } ) ;
42+ throws ( ( ) => script1 . runInNewContext ( context ) , {
43+ code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
44+ } ) ;
45+ throws ( ( ) => script1 . runInThisContext ( ) , {
3846 code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
39- message : / S c r i p t / ,
4047 } ) ;
4148
4249 // Test Script constructor argument validation
@@ -62,10 +69,8 @@ export const vmTest = {
6269 }
6370 ) ;
6471
65- // Test Script with string options (should be treated as filename)
66- throws ( ( ) => new vm . Script ( 'code' , 'filename.js' ) , {
67- code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
68- } ) ;
72+ // Script can accept string options (should be treated as filename)
73+ doesNotThrow ( ( ) => new vm . Script ( 'code' , 'filename.js' ) ) ;
6974
7075 // Test runInContext function
7176 throws ( ( ) => vm . runInContext ( 'this.a = 1;' , { } ) , {
@@ -126,8 +131,16 @@ export const vmTest = {
126131 code : 'ERR_INVALID_ARG_VALUE' ,
127132 } ) ;
128133
129- // Test createScript function
130- throws ( ( ) => vm . createScript ( 'code' ) , {
134+ // Test createScript function returns a Script that throws on run methods
135+ const script2 = vm . createScript ( 'code' ) ;
136+ strictEqual ( script2 instanceof vm . Script , true ) ;
137+ throws ( ( ) => script2 . runInContext ( context ) , {
138+ code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
139+ } ) ;
140+ throws ( ( ) => script2 . runInNewContext ( context ) , {
141+ code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
142+ } ) ;
143+ throws ( ( ) => script2 . runInThisContext ( ) , {
131144 code : 'ERR_METHOD_NOT_IMPLEMENTED' ,
132145 } ) ;
133146
@@ -272,17 +285,17 @@ export const vmTest = {
272285 }
273286 ) ;
274287
275- // Test timeout validation in runInThisContext options
288+ // Test timeout validation in runInThisContext options (must be strictly positive)
276289 throws ( ( ) => vm . runInThisContext ( 'code' , { timeout : - 1 } ) , {
277- code : 'ERR_METHOD_NOT_IMPLEMENTED ' ,
290+ code : 'ERR_OUT_OF_RANGE ' ,
278291 } ) ;
279292
280293 // Test boolean validations
281294 throws ( ( ) => vm . runInThisContext ( 'code' , { breakOnSigint : 'invalid' } ) , {
282- code : 'ERR_METHOD_NOT_IMPLEMENTED ' ,
295+ code : 'ERR_INVALID_ARG_TYPE ' ,
283296 } ) ;
284297 throws ( ( ) => vm . runInThisContext ( 'code' , { displayErrors : 'invalid' } ) , {
285- code : 'ERR_METHOD_NOT_IMPLEMENTED ' ,
298+ code : 'ERR_INVALID_ARG_TYPE ' ,
286299 } ) ;
287300
288301 // Test that constants object is frozen
0 commit comments