@nestjs/testing#Test TypeScript Examples

The following examples show how to use @nestjs/testing#Test. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: app.controller.spec.ts    From 42_checkIn with GNU General Public License v3.0 6 votes vote down vote up
describe('AppController', () => {
  let appController: AppController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [AppService],
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should return "Hello World!"', () => {
      expect(appController.getHello()).toBe('Hello World!');
    });
  });
});
Example #2
Source File: firebase-admin.module.spec.ts    From nestjs-firebase-admin with MIT License 6 votes vote down vote up
describe('FirebaseAdminModule', () => {
  describe('forRoot', () => {
    beforeEach(async () => {
      await Test.createTestingModule({
        imports: [
          FirebaseAdminModule.forRoot({
            projectId: 'nestjs-firebase-admin-for-test',
          }),
        ],
      }).compile();
    });

    it('should create', () => {
      expect(FirebaseAdminModule).toBeDefined();
    });
  });

  describe('forRootAsync', () => {
    beforeEach(async () => {
      await Test.createTestingModule({
        imports: [
          FirebaseAdminModule.forRootAsync({
            useFactory: () => ({
              projectId: 'nestjs-firebase-admin-for-test',
            }),
          }),
        ],
      }).compile();
    });

    it('should create', () => {
      expect(FirebaseAdminModule).toBeDefined();
    });
  });
});
Example #3
Source File: app.controller.spec.ts    From nr-apm-stack with Apache License 2.0 6 votes vote down vote up
describe('AppController', () => {
  let appController: AppController;
  const handledVal = {};
  const appService = {
    handleKinesisEvent: jest.fn().mockResolvedValue(handledVal),
  };

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [AppService],
    }).overrideProvider(AppService)
      .useValue(appService)
      .compile();

    appController = app.get<AppController>(AppController);
    jest.clearAllMocks();
  });

  describe('root', () => {
    it('should handleKinesisEvent when print false"', async () => {
      expect(await appController.handleData({}, '')).toBe(handledVal);

      expect(appService.handleKinesisEvent).toHaveBeenCalledTimes(1);
      expect(appService.handleKinesisEvent).toHaveBeenCalledWith({}, false);
    });

    it('should handleKinesisEvent when print true"', async () => {
      expect(await appController.handleData({}, 'true')).toBe(handledVal);

      expect(appService.handleKinesisEvent).toHaveBeenCalledTimes(1);
      expect(appService.handleKinesisEvent).toHaveBeenCalledWith({}, true);
    });
  });
});
Example #4
Source File: newsletter.controller.test.ts    From Cromwell with MIT License 6 votes vote down vote up
describe('plugin-newsletter.controller', () => {

    let controller: PluginNewsletterController;

    beforeAll(async () => {
        controller = new PluginNewsletterController();

        const moduleRef = await Test.createTestingModule({
            controllers: [PluginNewsletterController],
            imports: [ThrottlerModule.forRoot({
                ttl: 30,
                limit: 100,
            })],
        }).compile();

        controller = moduleRef.get<PluginNewsletterController>(PluginNewsletterController);
    })

    it(`/GET stats`, async () => {
        const stats = await controller.getStats({
            user: {
                role: 'administrator'
            }
        } as any);

        expect(stats + '').toBe('2')
    });

    it(`/POST subscribe`, async () => {
        const success = await controller.placeSubscription({
            email: '[email protected]'
        });

        expect(success).toBe(true);
    });
})
Example #5
Source File: code-first-federation.spec.ts    From nestjs-mercurius with MIT License 6 votes vote down vote up
graphqlSuite.before.each(async (ctx) => {
  const module = await Test.createTestingModule({
    imports: [AppModule],
  }).compile();

  const app = module.createNestApplication<NestFastifyApplication>(
    new FastifyAdapter(),
  );
  await app.init();
  ctx.app = app;
  ctx.mercuriusClient = createTestClient(module);
});
Example #6
Source File: application.controller.spec.ts    From nest-nuxt-starter with MIT License 6 votes vote down vote up
describe('ApplicationController', () => {
  let controller: ApplicationController;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [ApplicationService],
      controllers: [ApplicationController],
    }).compile();

    controller = module.get<ApplicationController>(ApplicationController);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
  });
});
Example #7
Source File: health.e2e-spec.ts    From nestjs-api-example with MIT License 6 votes vote down vote up
describe('HealthController (e2e)', () => {
  let app: INestApplication;

  beforeAll(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = module.createNestApplication();
    await app.init();
  });

  afterAll(async () => {
    await app.close();
  });

  it('/health (GET)', () => {
    return request(app.getHttpServer()).get('/health').expect(200).expect('OK');
  });
});
Example #8
Source File: status.controller.test.ts    From nest-react with GNU Lesser General Public License v3.0 6 votes vote down vote up
describe('AppController', () => {
  let statusController: StatusController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [StatusController],
      providers: [ConfigService, StatusService],
    }).compile();

    statusController = app.get<StatusController>(StatusController);
  });

  describe('root', () => {
    it('should return a nice status world', () => {
      expect(statusController.getStatus()).toBe(
        'Status world from Nest running on localhost:4000!'
      );
    });
  });
});
Example #9
Source File: app.controller.spec.ts    From NestJs-youtube with MIT License 6 votes vote down vote up
describe('AppController', () => {
  let appController: AppController;

  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      controllers: [AppController],
      providers: [AppService],
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should return "Hello World!"', () => {
      expect(appController.getHello()).toBe('Hello World!');
    });
  });
});
Example #10
Source File: ses.service.spec.ts    From nestjs-ses with MIT License 6 votes vote down vote up
describe('SesService', () => {
  let service: SesService;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      providers: [SesService],
    }).compile();

    service = module.get<SesService>(SesService);
  });

  it('should be defined', () => {
    expect(service).toBeDefined();
  });
});
Example #11
Source File: auth.controller.spec.ts    From nest-js-boilerplate with MIT License 6 votes vote down vote up
describe('Auth Controller', () => {
  let controller: AuthController;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      controllers: [AuthController],
      providers: [
        {
          provide: AuthService,
          useValue: {},
        },
        {
          provide: UsersService,
          useValue: {},
        },
        {
          provide: JwtService,
          useValue: {},
        },
      ],
    }).compile();

    controller = module.get<AuthController>(AuthController);
  });

  it('should be defined', () => {
    expect(controller).toBeDefined();
  });
});
Example #12
Source File: app.controller.spec.ts    From barista with Apache License 2.0 6 votes vote down vote up
describe('AppController', () => {
  let appController: AppController;
  beforeEach(async () => {
    const app: TestingModule = await Test.createTestingModule({
      imports: [
        BullModule.forRoot({
          name: 'test',
          processors: [jest.fn()],
        }),
      ],
      controllers: [AppController],
      providers: [
        AppService,
        LicenseService,
        { provide: getRepositoryToken(License), useClass: mockRepository },
        { provide: getQueueToken('scan-queue'), useValue: { add: jest.fn } },
      ],
    }).compile();

    appController = app.get<AppController>(AppController);
  });

  describe('root', () => {
    it('should instantiate', () => {
      expect(appController).toBeDefined();
    });

    it('should return a result', () => {
      expect(appController.appStatus()).toBeDefined();
    });
  });
});