Stack Exchange
"TypeError: Cannot create proxy with a non-object as target or handler" in nest.js test of ConfigurableModuleBuilder
I am getting the error
   TypeError: Cannot create proxy with a non-object as target or handler

      14 |
      15 |   beforeEach(async () => {
    > 16 |     const module: TestingModule = await Test.createTestingModule({
         |                                   ^
      17 |       imports: [],
      18 |       providers: [
      19 |         WorkloadIdentityTokenService,

      at TestingInjector.createMock [as mocker] (node_modules/@golevelup/ts-jest/src/mocks.ts:67:17)

on a jest test of a service that is part of a nest.js dynamic module built using ConfigurableModuleBuilder.
My test looks like this, using @golevelup/ts-jest automocker:
describe('WorkloadIdentityTokenService', () => {
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [],
      providers: [WorkloadIdentityTokenService],
    })
      .useMocker(createMock)
      .compile();
  });
});

with the service looking like this:
export const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder<WorkloadIdentityModuleOptions>().build();

[...]

@Injectable()
export class WorkloadIdentityTokenService {
  private readonly minutesToRefreshTokenBeforeExpiry: number;

  public constructor(@Inject(MODULE_OPTIONS_TOKEN) options: WorkloadIdentityModuleOptions) {
    this.minutesToRefreshTokenBeforeExpiry = options.minutesToRefreshTokenBeforeExpiry ?? 60;
  }
}

The error message is quite unhelpful and there is no information I could find on how to properly test a dynamic module build with the ConfigurableModuleBuilder.

The reason for this error was the missing injection token in the service constructor in the test setup, so this worked in the end:
describe('WorkloadIdentityTokenService', () => {
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [],
      providers: [
        WorkloadIdentityTokenService,
        {
          provide: MODULE_OPTIONS_TOKEN,
          useValue: {
            minutesToRefreshTokenBeforeExpiry: 60,
          },
        },
      ],
    })
      .useMocker(createMock)
      .compile();
  });
});

Even when I tried to import the module in the imports array it did not recognize the MODULE_OPTIONS_TOKEN as something that exists, it had to be provided in the providers.

Hi your implement is close to done
So just
describe('WorkloadIdentityTokenService', () => {
  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [WorkloadIdentityTokenModule.forRoot()], // add dynamic module in here
      providers: [
        WorkloadIdentityTokenService,
        {
          provide: MODULE_OPTIONS_TOKEN,
          useValue: {
            minutesToRefreshTokenBeforeExpiry: 60,
          },
        },
      ],
    })
      .useMocker(createMock)
      .compile();
  });
});


As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
