ts-node transpiles createConnection in typeorm to (0, typeorm_1.createConnection)

Viewed 183

I was facing the same issue as Getting Jest global setup and global teardown to work in a typescript project, so I went with the solution provided in the answer and wrote 'require(ts-node/register)' at the top of my global setup file. But, during transpilation, the following error occurs: Screenshot of error.

My global setup file looks like,

require('ts-node/register');
import { Connection } from "typeorm";
import { closeTestingConnection, createTestingConnection, dropTestingDatabases, runMigrationsOnDatabase } from "./utils/util";

/**
 * Creates database connection before running any test case
 */
const globalSetup = async () => {
    return new Promise(async (resolve, reject) => {
        let testConnection: Connection | undefined;
        try {
            testConnection = await createTestingConnection();
            await runMigrationsOnDatabase(testConnection);
            resolve(true);
        } catch (error) {
            if(testConnection) {
                await dropTestingDatabases(testConnection);
                await closeTestingConnection(testConnection);
            }
            reject(error);
        }
    })
}

export default globalSetup;

which uses a util function 'createTestingConnection',

export function getDBConfig(envName?: string): ConnectionOptions {
    let file: string;
    if (envName) {
        file = __dirname + "/../../test/settings/ormconfig." + envName + ".ts";
    } else {
        file = __dirname + "/../../test/settings/ormconfig.local.ts";
    }
    try {
        return require(file);
    } catch (err) {
        throw new Error("Unable to locate ++ DB Config.");
    }
}

/**
 * Creates a testing connections based on the configuration in the ormconfig.json
 * and given options that can override some of its configuration for the test-specific use case.
 */
export async function createTestingConnection(): Promise<Connection> {
    const testEnvName: string = process.env.TEST_ENV || "local";
    return createConnection(getDBConfig(testEnvName));
}

0 Answers
Related