You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm encountering what I think is the same problem as this StackOverflow issue. A more generalized repro scenario looks like:
import { createMock } from '@golevelup/ts-jest'
import { Test, TestingModule } from '@nestjs/testing'
import { Injectable, Inject } from '@nestjs/common'
class BarProvider {}
@Injectable()
class FooService {
constructor(
@Inject('fooProvider') private readonly foo: string,
private readonly bar: BarProvider
) {}
}
describe('TestModule', () => {
let module: TestingModule
beforeEach(async () => {
module = await Test.createTestingModule({
imports: [],
providers: [FooService]
})
.useMocker(createMock)
.compile()
})
it('works', () => {
const subject = module.get(FooService)
expect(1).toEqual(1) // ... or whatever
})
})
Running the above yields the following error:
TestModule
✕ works (11 ms)
● TestModule › works
TypeError: Cannot create proxy with a non-object as target or handler
at TestingInjector.createMock [as mocker] (../../node_modules/@golevelup/ts-jest/src/mocks.ts:67:17)
at TestingInjector.resolveComponentInstance (../../node_modules/@nestjs/testing/testing-injector.js:24:45)
at resolveParam (../../node_modules/@nestjs/core/injector/injector.js:120:38)
at async Promise.all (index 0)
at TestingInjector.resolveConstructorParams (../../node_modules/@nestjs/core/injector/injector.js:135:27)
at TestingInjector.loadInstance (../../node_modules/@nestjs/core/injector/injector.js:61:13)
at TestingInjector.loadProvider (../../node_modules/@nestjs/core/injector/injector.js:88:9)
at ../../node_modules/@nestjs/core/injector/instance-loader.js:56:13
at async Promise.all (index 3)
Notice how we inject two dependencies, one using the class reference (BarProvider), and the other using a token reference (fooProvider).
Tracking this down it simply boils down to createMock creating a Proxy object, but receiving a string as partial, and the Proxy constructor blowing up because it needs an object. In the above example, if you remove the fooProvider dependency from the FooService constructor, the test passes.
I'm not sure what's the best course of action. My expectation is that all dependencies are mocked regardless of whether they are injected via class or via token. Is that sensible? The author of the Stack Overflow issue resolves the problem by adding another provider that provides the dependency, but that seems to defeat the purpose of mocking.
Thoughts?
Thanks!
The text was updated successfully, but these errors were encountered:
that's because, in your example, createMock will be called like this by nestjs: createMock('fooProvider') which ends up doing new Proxy('fooProvider', ...), which leads to that error because Proxy expects a non-primitive value as a target.
Hi,
I'm encountering what I think is the same problem as this StackOverflow issue. A more generalized repro scenario looks like:
Running the above yields the following error:
Notice how we inject two dependencies, one using the class reference (
BarProvider
), and the other using a token reference (fooProvider
).Tracking this down it simply boils down to
createMock
creating a Proxy object, but receiving a string aspartial
, and the Proxy constructor blowing up because it needs an object. In the above example, if you remove thefooProvider
dependency from theFooService
constructor, the test passes.I'm not sure what's the best course of action. My expectation is that all dependencies are mocked regardless of whether they are injected via class or via token. Is that sensible? The author of the Stack Overflow issue resolves the problem by adding another provider that provides the dependency, but that seems to defeat the purpose of mocking.
Thoughts?
Thanks!
The text was updated successfully, but these errors were encountered: