How to use @golevelup/nestjs-testing - 10 common examples

To help you get started, we’ve selected a few @golevelup/nestjs-testing examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jmcdo29 / testing-nestjs / apps / mongo-sample / src / cat / cat.controller.spec.ts View on Github external
it('should get a cat back', async () => {
      expect(controller.getByName('Ventus')).resolves.toEqual({
        name: 'Ventus',
        breed: testBreed1,
        age: 4,
      });
      // using the really cool @golevelup/nestjs-testing module's utility function here
      // otherwise we need to pass `as any` or we need to mock all 54+ attributes of Document
      const aquaMock = createMock({
        name: 'Aqua',
        breed: 'Maine Coon',
        age: 5,
      });
      const getByNameSpy = jest
        .spyOn(service, 'getOneByName')
        .mockResolvedValueOnce(aquaMock);
      const getResponse = await controller.getByName('Aqua');
      expect(getResponse).toEqual(aquaMock);
      expect(getByNameSpy).toBeCalledWith('Aqua');
    });
  });
github jmcdo29 / testing-nestjs / apps / mongo-sample / src / cat / cat.service.spec.ts View on Github external
it('should update a cat successfully', async () => {
    jest.spyOn(model, 'update').mockResolvedValueOnce(true);
    jest.spyOn(model, 'findOne').mockReturnValueOnce(
      createMock>({
        exec: jest.fn().mockResolvedValueOnce({
          _id: lasagna,
          name: 'Garfield',
          breed: 'Tabby',
          age: 42,
        }),
      }),
    );
    const updatedCat = await service.updateOne({
      _id: lasagna,
      name: 'Garfield',
      breed: 'Tabby',
      age: 42,
    });
    expect(updatedCat).toEqual(mockCat('Garfield', lasagna, 42, 'Tabby'));
  });
github jmcdo29 / testing-nestjs / apps / typeorm-sample / src / cat / cat.service.create-mock.spec.ts View on Github external
describe('CatService using createMock without DI', () => {
  let service: CatService;
  const repo = createMock>();

  beforeEach(async () => {
    const module = await Test.createTestingModule({
      providers: [
        CatService,
        {
          provide: getRepositoryToken(Cat),
          useValue: repo,
        },
      ],
    }).compile();

    service = module.get(CatService);
  });

  it('should have the repo mocked', async () => {
github jmcdo29 / testing-nestjs / apps / complex-sample / src / cat / cat.guard.spec.ts View on Github external
it('should return true with auth', () => {
    const context = createMock();

    context.switchToHttp().getRequest.mockReturnValue({
      headers: {
        authorization: 'auth',
      },
    });

    expect(guard.canActivate(context)).toBeTruthy();
  });
  it('should return false without auth', () => {
github jmcdo29 / zeldaPlay / apps / api / src / app / guards / google.guard.spec.ts View on Github external
it('should return true for canActivate', async () => {
    expect(
      await guard.canActivate(createMock()),
    ).toBeTruthy();
  });
});
github jmcdo29 / zeldaPlay / apps / api / src / app / auth / google-user / google-user.service.spec.ts View on Github external
it('should successfully insert the user', (done) => {
      service
        .createNewGoogleUser(
          createMock({
            id: 'googleId',
            name: {
              familyName: 'test',
              givenName: 'test',
            },
            emails: [{ value: 'test@test.email', verified: true }],
          }),
        )
        .subscribe({
          next: (val) => {
            expect(val).toEqual(googleUser);
          },
          error: (err) => {
            throw err;
          },
          complete: complete(insertSpy, done),
github jmcdo29 / zeldaPlay / apps / api / src / app / auth / auth / auth.service.spec.ts View on Github external
it('should not find, but still create ad return a Google User', (done) => {
        jest
          .spyOn(googleService, 'getByGoogleId')
          .mockReturnValueOnce(of(undefined));
        const createSpy = jest
          .spyOn(googleService, 'createNewGoogleUser')
          .mockReturnValueOnce(of(mockGoogleUser));
        service
          .findOrCreateGoogleUser(createMock())
          .subscribe(googleSubscriber(done, createSpy, 1));
      });
    });
github jmcdo29 / zeldaPlay / apps / api / src / app / guards / is-logged-in.guard.spec.ts View on Github external
it('should return that the user is authenticated', () => {
    expect(
      guard.canActivate(
        createMock({
          switchToHttp: jest.fn().mockReturnValue({
            getRequest: jest.fn().mockReturnValue({
              isAuthenticated: jest.fn().mockReturnValue(true),
            }),
          }),
        }),
      ),
    ).toBe(true);
  });
  it('should return the user is **not** authenticate', () => {
github jmcdo29 / zeldaPlay / apps / api / src / app / guards / local.guard.spec.ts View on Github external
GqlExecutionContext.create = jest.fn((context: ExecutionContext) => {
      return createMock({
        getContext: () => ({
          req: {
            body: {
              variables: {
                email: testEmail,
              },
              somethingElse: 'some other value',
            },
          },
        }),
      });
    });

    expect(guard.getRequest(createMock())).toEqual({
      body: {
        variables: {
          email: testEmail,
        },
        somethingElse: 'some other value',
        email: testEmail,
      },
    });
  });
  it('should return true for `canActivate`', async () => {
github jmcdo29 / zeldaPlay / apps / api / src / app / auth / user / user.service.spec.ts View on Github external
beforeEach(async () => {
    const module = await Test.createTestingModule({
      providers: [
        UserService,
        {
          provide: DatabaseService,
          useValue: {
            query: jest.fn(),
            insert: jest.fn(),
            update: jest.fn(),
            updateMany: jest.fn(),
          },
        },
        {
          provide: OgmaService,
          useValue: createMock(),
        },
      ],
    }).compile();

    service = module.get(UserService);
    db = module.get>(DatabaseService);
  });

@golevelup/nestjs-testing

Reusable utilties to help level up NestJS Testing

MIT
Latest version published 5 years ago

Package Health Score

59 / 100
Full package analysis

Popular @golevelup/nestjs-testing functions