|
|
@@ -2,14 +2,10 @@ import { INestApplication, INestMicroservice } from '@nestjs/common';
|
|
|
import { NestFactory } from '@nestjs/core';
|
|
|
import { DefaultLogger, Logger, VendureConfig } from '@vendure/core';
|
|
|
import { preBootstrapConfig } from '@vendure/core/dist/bootstrap';
|
|
|
-import fs from 'fs';
|
|
|
-import path from 'path';
|
|
|
-import { MysqlConnectionOptions } from 'typeorm/driver/mysql/MysqlConnectionOptions';
|
|
|
-import { PostgresConnectionOptions } from 'typeorm/driver/postgres/PostgresConnectionOptions';
|
|
|
-import { SqljsConnectionOptions } from 'typeorm/driver/sqljs/SqljsConnectionOptions';
|
|
|
|
|
|
import { populateForTesting } from './data-population/populate-for-testing';
|
|
|
-import { Mutable, TestServerOptions } from './types';
|
|
|
+import { getInitializerFor } from './initializers/initializers';
|
|
|
+import { TestServerOptions } from './types';
|
|
|
|
|
|
// tslint:disable:no-console
|
|
|
/**
|
|
|
@@ -35,19 +31,18 @@ export class TestServer {
|
|
|
async init(options: TestServerOptions): Promise<void> {
|
|
|
const { type } = this.vendureConfig.dbConnectionOptions;
|
|
|
const { dbConnectionOptions } = this.vendureConfig;
|
|
|
- switch (type) {
|
|
|
- case 'sqljs':
|
|
|
- await this.initSqljs(options);
|
|
|
- break;
|
|
|
- case 'mysql':
|
|
|
- await this.initMysql(dbConnectionOptions as MysqlConnectionOptions, options);
|
|
|
- break;
|
|
|
- case 'postgres':
|
|
|
- await this.initPostgres(dbConnectionOptions as PostgresConnectionOptions, options);
|
|
|
- break;
|
|
|
- default:
|
|
|
- throw new Error(`The TestServer does not support the database type "${type}"`);
|
|
|
+ const testFilename = this.getCallerFilename(1);
|
|
|
+ const initializer = getInitializerFor(type);
|
|
|
+ try {
|
|
|
+ await initializer.init(testFilename, dbConnectionOptions);
|
|
|
+ const populateFn = () => this.populateInitialData(this.vendureConfig, options);
|
|
|
+ await initializer.populate(populateFn);
|
|
|
+ await initializer.destroy();
|
|
|
+ } catch (e) {
|
|
|
+ console.error(e);
|
|
|
+ process.exit(1);
|
|
|
}
|
|
|
+
|
|
|
const [app, worker] = await this.bootstrapForTesting(this.vendureConfig);
|
|
|
if (app) {
|
|
|
this.app = app;
|
|
|
@@ -74,111 +69,6 @@ export class TestServer {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private async initSqljs(options: TestServerOptions) {
|
|
|
- const dbFilePath = this.getDbFilePath(options.dataDir);
|
|
|
- (this.vendureConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).location = dbFilePath;
|
|
|
- if (!fs.existsSync(dbFilePath)) {
|
|
|
- if (options.logging) {
|
|
|
- console.log(`Test data not found. Populating database and caching...`);
|
|
|
- }
|
|
|
- await this.populateInitialData(this.vendureConfig, options);
|
|
|
- }
|
|
|
- if (options.logging) {
|
|
|
- console.log(`Loading test data from "${dbFilePath}"`);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private async initMysql(connectionOptions: MysqlConnectionOptions, options: TestServerOptions) {
|
|
|
- const filename = this.getCallerFilename(2);
|
|
|
- const dbName = this.getDbNameFromFilename(filename);
|
|
|
- const conn = await this.getMysqlConnection(connectionOptions);
|
|
|
- (connectionOptions as any).database = dbName;
|
|
|
- (connectionOptions as any).synchronize = true;
|
|
|
- await new Promise((resolve, reject) => {
|
|
|
- conn.query(`DROP DATABASE IF EXISTS ${dbName}`, err => {
|
|
|
- if (err) {
|
|
|
- console.log(err);
|
|
|
- reject(err);
|
|
|
- return;
|
|
|
- }
|
|
|
- resolve();
|
|
|
- });
|
|
|
- });
|
|
|
- await new Promise((resolve, reject) => {
|
|
|
- conn.query(`CREATE DATABASE IF NOT EXISTS ${dbName}`, err => {
|
|
|
- if (err) {
|
|
|
- console.log(err);
|
|
|
- reject(err);
|
|
|
- return;
|
|
|
- }
|
|
|
- resolve();
|
|
|
- });
|
|
|
- });
|
|
|
- await this.populateInitialData(this.vendureConfig, options);
|
|
|
- conn.destroy();
|
|
|
- }
|
|
|
-
|
|
|
- private async initPostgres(connectionOptions: PostgresConnectionOptions, options: TestServerOptions) {
|
|
|
- const filename = this.getCallerFilename(2);
|
|
|
- const dbName = this.getDbNameFromFilename(filename);
|
|
|
- (connectionOptions as any).database = dbName;
|
|
|
- (connectionOptions as any).synchronize = true;
|
|
|
- const client = await this.getPostgresConnection(connectionOptions);
|
|
|
- await client.query(`DROP DATABASE IF EXISTS ${dbName}`);
|
|
|
- await client.query(`CREATE DATABASE ${dbName}`);
|
|
|
- await this.populateInitialData(this.vendureConfig, options);
|
|
|
- await client.end();
|
|
|
- }
|
|
|
-
|
|
|
- private async getMysqlConnection(
|
|
|
- connectionOptions: MysqlConnectionOptions,
|
|
|
- ): Promise<import('mysql').Connection> {
|
|
|
- const { createConnection } = await import('mysql');
|
|
|
- const conn = createConnection({
|
|
|
- host: connectionOptions.host,
|
|
|
- port: connectionOptions.port,
|
|
|
- user: connectionOptions.username,
|
|
|
- password: connectionOptions.password,
|
|
|
- });
|
|
|
- await new Promise((resolve, reject) => {
|
|
|
- conn.connect(err => {
|
|
|
- if (err) {
|
|
|
- reject(err);
|
|
|
- return;
|
|
|
- }
|
|
|
- resolve();
|
|
|
- });
|
|
|
- });
|
|
|
- return conn;
|
|
|
- }
|
|
|
-
|
|
|
- private async getPostgresConnection(
|
|
|
- connectionOptions: PostgresConnectionOptions,
|
|
|
- ): Promise<import('pg').Client> {
|
|
|
- const { Client } = require('pg');
|
|
|
- const client = new Client({
|
|
|
- host: connectionOptions.host,
|
|
|
- port: connectionOptions.port,
|
|
|
- user: connectionOptions.username,
|
|
|
- password: connectionOptions.password,
|
|
|
- database: 'postgres',
|
|
|
- });
|
|
|
- await client.connect();
|
|
|
- return client;
|
|
|
- }
|
|
|
-
|
|
|
- private getDbFilePath(dataDir: string) {
|
|
|
- // tslint:disable-next-line:no-non-null-assertion
|
|
|
- const testFilePath = this.getCallerFilename(3);
|
|
|
- const dbFileName = path.basename(testFilePath) + '.sqlite';
|
|
|
- const dbFilePath = path.join(dataDir, dbFileName);
|
|
|
- return dbFilePath;
|
|
|
- }
|
|
|
-
|
|
|
- private getDbNameFromFilename(filename: string): string {
|
|
|
- return 'e2e_' + path.basename(filename).replace(/[^a-z0-9_]/gi, '_');
|
|
|
- }
|
|
|
-
|
|
|
private getCallerFilename(depth: number): string {
|
|
|
let pst: ErrorConstructor['prepareStackTrace'];
|
|
|
let stack: any;
|
|
|
@@ -209,12 +99,6 @@ export class TestServer {
|
|
|
testingConfig: Required<VendureConfig>,
|
|
|
options: TestServerOptions,
|
|
|
): Promise<void> {
|
|
|
- const isSqljs = testingConfig.dbConnectionOptions.type === 'sqljs';
|
|
|
- if (isSqljs) {
|
|
|
- (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).autoSave = true;
|
|
|
- (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).synchronize = true;
|
|
|
- }
|
|
|
-
|
|
|
const [app, worker] = await populateForTesting(testingConfig, this.bootstrapForTesting, {
|
|
|
logging: false,
|
|
|
...options,
|
|
|
@@ -223,10 +107,6 @@ export class TestServer {
|
|
|
await worker.close();
|
|
|
}
|
|
|
await app.close();
|
|
|
-
|
|
|
- if (isSqljs) {
|
|
|
- (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).autoSave = false;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/**
|