1
1
import { existsSync , mkdirSync , rmSync } from 'node:fs' ;
2
+ import { createRequire } from 'node:module' ;
2
3
import { resolve } from 'node:path' ;
3
4
import process from 'node:process' ;
4
5
import { mock } from 'node:test' ;
@@ -18,13 +19,15 @@ import RunContext, { BasicRunContext, type RunContextSettings } from './run-cont
18
19
import testContext from './test-context.js' ;
19
20
import { createEnv as createEnvironment } from './default-environment.js' ;
20
21
21
- let GeneratorImplementation ;
22
- try {
23
- const GeneratorImport = await import ( 'yeoman-generator' ) ;
24
- GeneratorImplementation = GeneratorImport . default ?? GeneratorImport ;
25
- } catch {
26
- // Ignore error
27
- }
22
+ let dummyParentClass ;
23
+ export const setDefaultDummyParentClass = parentClass => {
24
+ dummyParentClass = parentClass ;
25
+ } ;
26
+
27
+ const getDummyParentClass = ( ) => {
28
+ dummyParentClass ??= createRequire ( import . meta. url ) ( 'yeoman-generator' ) . default ;
29
+ return dummyParentClass ;
30
+ } ;
28
31
29
32
export type CreateEnv = ( options : BaseEnvironmentOptions ) => Promise < BaseEnvironment > ;
30
33
@@ -145,7 +148,7 @@ export class YeomanTest {
145
148
/**
146
149
* Create a mocked generator
147
150
*/
148
- createMockedGenerator ( GeneratorClass = GeneratorImplementation ) : ReturnType < typeof mock . fn > {
151
+ createMockedGenerator ( GeneratorClass = getDummyParentClass ( ) ) : ReturnType < typeof mock . fn > {
149
152
class MockedGenerator extends GeneratorClass { }
150
153
const generator = mock . fn ( MockedGenerator ) ;
151
154
for ( const methodName of [ 'run' , 'queueTasks' , 'runWithOptions' , 'queueOwnTasks' ] ) {
@@ -161,7 +164,7 @@ export class YeomanTest {
161
164
* Create a simple, dummy generator
162
165
*/
163
166
createDummyGenerator < GenParameter extends BaseGenerator = DefaultGeneratorApi > (
164
- Generator : GetGeneratorConstructor < GenParameter > = GeneratorImplementation ,
167
+ Generator : GetGeneratorConstructor < GenParameter > = getDummyParentClass ( ) ,
165
168
contents : Record < string , ( ...arguments_ : any [ ] ) => void > = {
166
169
test ( this : any ) {
167
170
this . shouldRun = true ;
0 commit comments