@@ -143,26 +143,34 @@ async function load(url, context, nextLoad) {
143
143
const baseURL = parsedURL ? parsedURL . href : url ;
144
144
const mock = mocks . get ( baseURL ) ;
145
145
146
+ const original = await nextLoad ( url , context ) ;
146
147
debug ( 'load hook, mock = %o' , mock ) ;
147
148
if ( mock ?. active !== true ) {
148
- return nextLoad ( url ) ;
149
+ return original ;
149
150
}
150
151
151
152
// Treat builtins as commonjs because customization hooks do not allow a
152
153
// core module to be replaced.
153
- const format = mock . format === 'builtin' ? 'commonjs' : mock . format ;
154
+ // Also collapse 'commonjs-sync' and 'require-commonjs' to 'commonjs'.
155
+ const format = (
156
+ original . format === 'builtin' ||
157
+ original . format === 'commonjs-sync' ||
158
+ original . format === 'require-commonjs' ) ? 'commonjs' : original . format ;
154
159
155
- return {
160
+ const result = {
156
161
__proto__ : null ,
157
162
format,
158
163
shortCircuit : true ,
159
- source : await createSourceFromMock ( mock ) ,
164
+ source : await createSourceFromMock ( mock , format ) ,
160
165
} ;
166
+
167
+ debug ( 'load hook finished, result = %o' , result ) ;
168
+ return result ;
161
169
}
162
170
163
- async function createSourceFromMock ( mock ) {
171
+ async function createSourceFromMock ( mock , format ) {
164
172
// Create mock implementation from provided exports.
165
- const { exportNames, format , hasDefaultExport, url } = mock ;
173
+ const { exportNames, hasDefaultExport, url } = mock ;
166
174
const useESM = format === 'module' ;
167
175
const source = `${ testImportSource ( useESM ) }
168
176
if (!$__test.mock._mockExports.has('${ url } ')) {
0 commit comments