test-server.ts 3.9 KB

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