I'm hoping to get some insight into an issue I'm having.
I have a mixin that produces a guard. The resulting guard uses a service that is injected. Here's the code for the mixin:
import {
CanActivate,
ExecutionContext,
Injectable,
mixin,
} from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AccountService } from 'src/modules/account/account.service';
export const ForAccountGuard = (
paramName: string,
{ required = false }: { required?: boolean } = {}
) => {
@Injectable()
class _ForAccountGuard implements CanActivate {
constructor(private readonly accountService: AccountService) {}
async canActivate(context: ExecutionContext) {
const ctx = GqlExecutionContext.create(context);
const accountId = ctx.getArgs()[paramName];
const currentUser = ctx.getContext().user;
if (required && !accountId) {
return false;
}
if (accountId) {
const account = await this.accountService.findUserAccount(
accountId,
currentUser.id
);
return !!account;
}
return true;
}
}
return mixin(_ForAccountGuard);
};
In my tests for a resolver that uses this mixin as a guard I'm doing the following:
@Query(() => [PropertyEntity])
@UseGuards(ForAccountGuard('accountId'))
async allProperties(@Args() { accountId }: AllPropertiesArgs) {
// <implementation removed>
}
So, the issue I'm running into is that I get the following error when running tests:
Cannot find module 'src/modules/account/account.service' from 'modules/common/guards/for-account.guard.ts'
Require stack:
modules/common/guards/for-account.guard.ts
modules/property/property.resolver.spec.ts
It looks like the injected AccountService isn't being resolved.
I'm not exactly sure how to tell Nest's testing module to override a guard that is a mixin. I've been trying it like this, but it doesn't seem to be working:
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
PropertyResolver,
...
],
})
.overrideGuard(ForAccountGuard)
.useValue(createMock<typeof ForAccountGuard>())
.compile();
);
});
So, how am I supposed to mock out a guard that is a mixin?