test-server.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { INestApplication } from '@nestjs/common';
  2. import { NestFactory } from '@nestjs/core';
  3. import * as fs from 'fs';
  4. import * as path from 'path';
  5. import { SqljsConnectionOptions } from 'typeorm/driver/sqljs/SqljsConnectionOptions';
  6. import { populate, PopulateOptions } from '../mock-data/populate';
  7. import { preBootstrapConfig } from '../src/bootstrap';
  8. import { Mutable } from '../src/common/types/common-types';
  9. import { VendureConfig } from '../src/config/vendure-config';
  10. import { testConfig } from './config/test-config';
  11. import { setTestEnvironment } from './test-utils';
  12. // tslint:disable:no-console
  13. /**
  14. * A server against which the e2e tests should be run.
  15. */
  16. export class TestServer {
  17. app: INestApplication;
  18. /**
  19. * Bootstraps an instance of Vendure server and populates the database according to the options
  20. * passed in. Should be called immediately after creating the client in the `beforeAll` function.
  21. *
  22. * The populated data is saved into an .sqlite file for each test file. On subsequent runs, this file
  23. * is loaded so that the populate step can be skipped, which speeds up the tests significantly.
  24. */
  25. async init(options: PopulateOptions): Promise<void> {
  26. setTestEnvironment();
  27. const testingConfig = testConfig;
  28. const dbFilePath = this.getDbFilePath();
  29. (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).location = dbFilePath;
  30. if (!fs.existsSync(dbFilePath)) {
  31. console.log(`Test data not found. Populating database and saving to "${dbFilePath}"`);
  32. await this.populateInitialData(testingConfig, options);
  33. }
  34. console.log(`Loading test data from "${dbFilePath}"`);
  35. this.app = await this.bootstrapForTesting(testingConfig);
  36. }
  37. /**
  38. * Destroy the Vendure instance. Should be called in the `afterAll` function.
  39. */
  40. async destroy() {
  41. await this.app.close();
  42. }
  43. private getDbFilePath() {
  44. const dbDataDir = '__data__';
  45. // tslint:disable-next-line:no-non-null-assertion
  46. const testFilePath = module!.parent!.filename;
  47. const dbFileName = path.basename(testFilePath) + '.sqlite';
  48. const dbFilePath = path.join(path.dirname(testFilePath), dbDataDir, dbFileName);
  49. return dbFilePath;
  50. }
  51. /**
  52. * Populates an .sqlite database file based on the PopulateOptions.
  53. */
  54. private async populateInitialData(testingConfig: VendureConfig, options: PopulateOptions): Promise<void> {
  55. (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).autoSave = true;
  56. const app = await populate(testingConfig, this.bootstrapForTesting, {
  57. logging: false,
  58. ...options,
  59. });
  60. await app.close();
  61. (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).autoSave = false;
  62. }
  63. /**
  64. * Bootstraps an instance of the Vendure server for testing against.
  65. */
  66. private async bootstrapForTesting(userConfig: Partial<VendureConfig>): Promise<INestApplication> {
  67. const config = await preBootstrapConfig(userConfig);
  68. const appModule = await import('../src/app.module');
  69. const app = await NestFactory.create(appModule.AppModule, { cors: config.cors });
  70. await app.listen(config.port);
  71. return app;
  72. }
  73. }