66 *
77 */
88
9+ import os from 'os' ;
10+ import path from 'path' ;
11+ import { promises as fs } from 'graceful-fs' ;
912import type { Config } from '@jest/types' ;
1013import type Runtime from '..' ;
1114import { createOutsideJestVmPath } from '../helpers' ;
@@ -15,6 +18,9 @@ let createRuntime: (
1518 config ?: Config . InitialOptions ,
1619) => Promise < Runtime & { __mockRootPath : string } > ;
1720
21+ const getTmpDir = async ( ) =>
22+ await fs . mkdtemp ( path . join ( os . tmpdir ( ) , 'jest-resolve-test-' ) ) ;
23+
1824describe ( 'Runtime require.resolve' , ( ) => {
1925 beforeEach ( ( ) => {
2026 createRuntime = require ( 'createRuntime' ) ;
@@ -29,6 +35,47 @@ describe('Runtime require.resolve', () => {
2935 expect ( resolved ) . toEqual ( require . resolve ( './test_root/resolve_self.js' ) ) ;
3036 } ) ;
3137
38+ it ( 'resolves an absolute module path' , async ( ) => {
39+ const absoluteFilePath = path . join ( await getTmpDir ( ) , 'test.js' ) ;
40+ await fs . writeFile (
41+ absoluteFilePath ,
42+ 'module.exports = require.resolve(__filename);' ,
43+ 'utf-8' ,
44+ ) ;
45+
46+ const runtime = await createRuntime ( __filename ) ;
47+ const resolved = runtime . requireModule (
48+ runtime . __mockRootPath ,
49+ absoluteFilePath ,
50+ ) ;
51+
52+ expect ( resolved ) . toEqual ( require . resolve ( absoluteFilePath ) ) ;
53+ } ) ;
54+
55+ it ( 'required modules can resolve absolute module paths with no paths entries passed' , async ( ) => {
56+ const tmpdir = await getTmpDir ( ) ;
57+ const entrypoint = path . join ( tmpdir , 'test.js' ) ;
58+ const target = path . join ( tmpdir , 'target.js' ) ;
59+
60+ // we want to test the require.resolve implementation within a
61+ // runtime-required module, so we need to create a module that then resolves
62+ // an absolute path, so we need two files: the entrypoint, and an absolute
63+ // target to require.
64+ await fs . writeFile (
65+ entrypoint ,
66+ `module.exports = require.resolve(${ JSON . stringify (
67+ target ,
68+ ) } , {paths: []});`,
69+ 'utf-8' ,
70+ ) ;
71+
72+ await fs . writeFile ( target , `module.exports = {}` , 'utf-8' ) ;
73+
74+ const runtime = await createRuntime ( __filename ) ;
75+ const resolved = runtime . requireModule ( runtime . __mockRootPath , entrypoint ) ;
76+ expect ( resolved ) . toEqual ( require . resolve ( target , { paths : [ ] } ) ) ;
77+ } ) ;
78+
3279 it ( 'resolves a module path with moduleNameMapper' , async ( ) => {
3380 const runtime = await createRuntime ( __filename , {
3481 moduleNameMapper : {
0 commit comments