question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 😃

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

7reactions
aravindanvecommented, Mar 27, 2020

@Drakota In case you’re still looking for a solution, this worked for me: https://github.com/facebook/jest/issues/5164#issuecomment-376006851

// 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`
4reactions
ecklfcommented, 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/*.*"],
  });
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Jest global setup and global teardown to work in a ...
The typescript environment is not yet defined when globalSetup and globalTeardown are run, which is why jest is failing.
Read more >
cannot find module 'ts-jest/dist/config/config-set' - You.com | The ...
I'm unable to use rewire with ts-jest, it seems to works with .js files, but it does not with .ts files as supposed...
Read more >
no tests were executed! - OSCHINA - 中文开源技术交流社区
OSCHINA.NET 是目前领先的中文开源技术社区。我们传播开源的理念,推广开源项目,为IT 开发者提供了一个发现、使用、并交流开源技术的平台.
Read more >
ts-jest is not transpiling modules imported wit...anycodings
The error was occurring when the anycodings_jestjs globalSetup code was being run. Not sure anycodings_jestjs if ts-jest doesn't transpile ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found