test-server.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 & { disableAuth?: boolean }): Promise<void> {
  26. setTestEnvironment();
  27. const testingConfig = testConfig;
  28. const dbFilePath = this.getDbFilePath();
  29. testingConfig.disableAuth = options.disableAuth;
  30. (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).location = dbFilePath;
  31. if (!fs.existsSync(dbFilePath)) {
  32. console.log(`Test data not found. Populating database and saving to "${dbFilePath}"`);
  33. await this.populateInitialData(testingConfig, options);
  34. }
  35. console.log(`Loading test data from "${dbFilePath}"`);
  36. this.app = await this.bootstrapForTesting(testingConfig);
  37. }
  38. /**
  39. * Destroy the Vendure instance. Should be called in the `afterAll` function.
  40. */
  41. async destroy() {
  42. await this.app.close();
  43. }
  44. private getDbFilePath() {
  45. const dbDataDir = '__data__';
  46. // tslint:disable-next-line:no-non-null-assertion
  47. const testFilePath = module!.parent!.filename;
  48. const dbFileName = path.basename(testFilePath) + '.sqlite';
  49. const dbFilePath = path.join(path.dirname(testFilePath), dbDataDir, dbFileName);
  50. return dbFilePath;
  51. }
  52. /**
  53. * Populates an .sqlite database file based on the PopulateOptions.
  54. */
  55. private async populateInitialData(testingConfig: VendureConfig, options: PopulateOptions): Promise<void> {
  56. (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).autoSave = true;
  57. const app = await populate(testingConfig, this.bootstrapForTesting, {
  58. logging: false,
  59. ...options,
  60. });
  61. await app.close();
  62. (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).autoSave = false;
  63. }
  64. /**
  65. * Bootstraps an instance of the Vendure server for testing against.
  66. */
  67. private async bootstrapForTesting(userConfig: Partial<VendureConfig>): Promise<INestApplication> {
  68. const config = await preBootstrapConfig(userConfig);
  69. const appModule = await import('../src/app.module');
  70. const app = await NestFactory.create(appModule.AppModule, { cors: config.cors });
  71. await app.listen(config.port);
  72. return app;
  73. }
  74. }