Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

globalSetup and globalTeardown doesn't seems to be transpiling correctly (w/ TypeORM) #1391

Closed
Drakota opened this issue Feb 13, 2020 · 6 comments

Comments

@Drakota
Copy link

Drakota commented Feb 13, 2020

Issue :

I'm trying to test a GraphQL API with a in-memory SQLite database with TypeORM. Currently I have to create a connection in every .test file in the beforeAll (this method also has caveats because I'm limited to use --runInBand). I then tried to globally start the database with my fixtures with globalSetup, but I'm getting a weird error:

import { EntitySubscriberInterface, EventSubscriber, InsertEvent } from "typeorm";
       ^

SyntaxError: Unexpected token {

Usually that would mean that it's trying to parse Typescript instead of transpiled Javascript.
I've made a sandbox reproduction here.
Try to run yarn test without modifications and it will work as expected, but by uncommenting the 2 lines in jest.config.js you will get the error above.

I'm not sure if this issue is upstream with Jest or with TypeORM, so feel free to point me where this issue should go if it's not in the right repo.

Thank you 😃

@aravindanve
Copy link

I have the same issue. @Drakota did you find a solution?

@Drakota
Copy link
Author

Drakota commented Mar 6, 2020

@aravindanve Nope, not at the moment unfortunately

@matdehaast
Copy link

I think this relates to this PR jestjs/jest#8751.

It has been long waiting as a PR to be merged but seems the release process for Jest is currently really hampered

@ahnpnl
Copy link
Collaborator

ahnpnl commented Mar 24, 2020

If it is jest issue, can I close this ?

@aravindanve
Copy link

@Drakota In case you're still looking for a solution, this worked for me:
jestjs/jest#5164 (comment)

// globalSetup.ts -- at the top of the file

require('ts-node').register('/path/to/tsconfig.json');
require('tsconfig-paths/register');

// ^^^ I needed this second line because I'm
// using `baseUrl` and `paths` in my `tsconfig.json`

@ecklf
Copy link

ecklf commented Jun 29, 2020

I worked around this by using setupFilesAfterEnv instead. This still allows me to access my TypeORM connection in all of my tests, and I don't need to install ts-node as a dependency.

jest.config.js

module.exports = {
  preset: "ts-jest",
  testEnvironment: "node",
  setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
};

node.d.ts

import { Connection } from "typeorm";

// Ensure file is treated as a module
export {};

declare global {
  namespace NodeJS {
    interface Global {
      testConn: Connection;
    }
  }
}

jest.setup.ts

import { createTestConnection } from "./src/test-utils/createTestConnection";

beforeAll(async () => {
  // Drop database before running tests
  const dropDatabaseConn = await createTestConnection(true);
  await dropDatabaseConn.close();
  global.testConn = await createTestConnection();
});

afterAll(async () => {
  await global.testConn.close();
});

createConnection.ts

import { createConnection } from "typeorm";

export const createTestConnection = (drop: boolean = false) => {
  return createConnection({
    name: "default",
    type: "postgres",
    host: "localhost",
    port: 5432,
    username: "postgres",
    password: "postgres",
    database: "db-test",
    synchronize: drop,
    dropSchema: drop,
    entities: [__dirname + "/../entity/*.*"],
  });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants