title: Typescript with Jest - "ReferenceError: beforeAll is not defined"
body: <p>So I have a project where I am using:</p>
  <ul>
  <li>Typescript</li>
  <li>TypeORM</li>
  <li>Type-graphql</li>
  <li>Jest</li>
  </ul>
  <p>I had it working just fine until I started writing my tests. The test files are inside of each entity folder. For example:</p>
  <pre><code>  Student
    |- Student.ts
    |- Student.test.ts
    |- StudentService.ts
  </code></pre>
  <p>When I run Jest to execute my tests, everything is just fine and works as expected. However, if I run <code>nodemon --exec ts-node src/index.ts</code> I get an error for the first Jest related function I have, whether it is beforeAll(), afterAll(), describe()...</p>
  <p>My tsconfig.json is:</p>
  <pre><code>{
    &quot;compilerOptions&quot;: {
      &quot;target&quot;: &quot;es6&quot;,
      &quot;module&quot;: &quot;commonjs&quot;,
      &quot;lib&quot;: [&quot;dom&quot;, &quot;es6&quot;, &quot;es2017&quot;, &quot;esnext.asynciterable&quot;],
      &quot;sourceMap&quot;: true,
      &quot;outDir&quot;: &quot;./dist&quot;,
      &quot;moduleResolution&quot;: &quot;node&quot;,
  
      &quot;types&quot;: [&quot;jest&quot;, &quot;node&quot;],
  
      &quot;removeComments&quot;: true,
      &quot;noImplicitAny&quot;: true,
      &quot;strictNullChecks&quot;: true,
      &quot;strictFunctionTypes&quot;: true,
      &quot;noImplicitThis&quot;: true,
      &quot;noUnusedLocals&quot;: true,
      &quot;noUnusedParameters&quot;: true,
      &quot;noImplicitReturns&quot;: true,
      &quot;noFallthroughCasesInSwitch&quot;: true,
      &quot;allowSyntheticDefaultImports&quot;: true,
      &quot;esModuleInterop&quot;: true,
      &quot;emitDecoratorMetadata&quot;: true,
      &quot;experimentalDecorators&quot;: true
    },
    &quot;include&quot;: [&quot;**/*.ts&quot;],
    &quot;exclude&quot;: [&quot;node_modules&quot;, &quot;**/*.test.ts&quot;, &quot;**/*.spec.ts&quot;]
  }
  
  </code></pre>
  <p>My jest.config.js is:</p>
  <pre><code>module.exports = {
    preset: &quot;ts-jest&quot;,
    testEnvironment: &quot;node&quot;,
    roots: [&quot;./src&quot;],
    testMatch: [
      &quot;**/__tests__/**/*.+(ts|tsx|js)&quot;,
      &quot;**/?(*.)+(spec|test).+(ts|tsx|js)&quot;,
    ],
    transform: {
      &quot;^.+\\.(ts|tsx)$&quot;: &quot;ts-jest&quot;,
    },
  };
  </code></pre>
  <p>From what I can understand, typescript is trying to compile my test files even when they are set to be excluded inside of the tsconfig.</p>
  <p>More information that might be useful:
  I have jest, ts-jest and @types/jest as devdependencies.
  I have only one tsconfig.json file and one jest.config.js</p>
  <p>Any idea on how to fix this?</p>
  <h3>Update 1</h3>
  <p>I forgot to post the one of the test files, although I don't think it is related to the issue or is the cause of my problem...</p>
  <pre><code>import { createTestConnection } from &quot;../../test/createTestConnection&quot;;
  import { graphqlCall } from &quot;../../test/graphqlCall&quot;;
  import { Connection } from &quot;typeorm&quot;;
  
  let conn: Connection;
  
  beforeAll(async () =&gt; {
    console.log(&quot;Test started&quot;);
  
    conn = await createTestConnection(true);
  });
  
  afterAll(() =&gt; {
    conn.close();
  });
  
  const addStudentWithoutGuardian = `
    mutation addStudentWithoutGuardian($studentInput: StudentInput!) {
    addStudent(studentInput: $studentInput) {
      id
    }
  }
  `;
  
  describe(&quot;Student&quot;, () =&gt; {
    it(&quot;create only with basic information&quot;, async () =&gt; {
      console.log(
        await graphqlCall({
          source: addStudentWithoutGuardian,
          variableValues: {
            studentInput: {
              firstName: &quot;Harry&quot;,
              lastName: &quot;Smith&quot;,
              dateOfBirth: &quot;1997-01-30&quot;,
              pronoun: 0,
              gender: 0,
              phone: &quot;1231231234&quot;,
              email: &quot;harry@gmail.com&quot;,
              addressLine1: &quot;123 Avenue&quot;,
              city: &quot;Toronto&quot;,
              state: &quot;Ontario&quot;,
              postalCode: &quot;X1X1X1&quot;,
              gradeLevel: &quot;grade level&quot;,
            },
          },
        })
      );
    });
  });
  </code></pre>
  <h3>Update 2</h3>
  <p>Error stack</p>
  <pre><code>$ nodemon --exec ts-node src/index.ts
  [nodemon] 2.0.7
  [nodemon] to restart at any time, enter `rs`
  [nodemon] watching path(s): *.*
  [nodemon] watching extensions: ts,json      
  [nodemon] starting `ts-node src/index.ts`   
  (node:11716) UnhandledPromiseRejectionWarning: ReferenceError: beforeAll is not defined
      at Object.&lt;anonymous&gt; (G:\Documents\repos\tms\server\src\model\Student\Student.test.ts:7:1)
      at Module._compile (internal/modules/cjs/loader.js:1063:30)
      at Module.m._compile (G:\Documents\repos\tms\server\node_modules\ts-node\src\index.ts:1056:23)
      at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
      at Object.require.extensions.&lt;computed&gt; [as .ts] (G:\Documents\repos\tms\server\node_modules\ts-node\src\index.ts:1059:12)
      at Module.load (internal/modules/cjs/loader.js:928:32)
      at Function.Module._load (internal/modules/cjs/loader.js:769:14)
      at Module.require (internal/modules/cjs/loader.js:952:19)
      at require (internal/modules/cjs/helpers.js:88:18)
      at G:\Documents\repos\tms\server\src\util\DirectoryExportedClassesLoader.ts:41:22
  (Use `node --trace-warnings ...` to show where the warning was created)
  (node:11716) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
  (node:11716) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
  [nodemon] clean exit - waiting for changes before restart
  </code></pre>
  <h3>Update 3</h3>
  <p>My index.ts</p>
  <pre><code>import &quot;reflect-metadata&quot;;
  import { createConnection } from &quot;typeorm&quot;;
  import express from &quot;express&quot;;
  import { ApolloServer } from &quot;apollo-server-express&quot;;
  import cors from &quot;cors&quot;;
  import { createSchema } from &quot;./utils/createSchema&quot;;
  
  (async () =&gt; {
    const app = express();
  
    app.use(
      cors({
        origin: [&quot;http://localhost:3000&quot;],
        credentials: true,
      })
    );
  
    await createConnection();
  
    const apolloServer = new ApolloServer({
      schema: await createSchema(),
      context: ({ req, res }) =&gt; ({ req, res }),
    });
  
    apolloServer.applyMiddleware({ app, cors: false });
  
    app.listen(4000, () =&gt; {
      console.log(&quot;Express server started&quot;);
    });
  })();
  </code></pre>
  <h3>Update 4</h3>
  <p>My dependenciesand devDependencies</p>
  <pre><code>&quot;dependencies&quot;: {
      &quot;apollo-server-express&quot;: &quot;^2.19.2&quot;,
      &quot;class-validator&quot;: &quot;^0.13.1&quot;,
      &quot;cors&quot;: &quot;^2.8.5&quot;,
      &quot;dotenv&quot;: &quot;^8.2.0&quot;,
      &quot;express&quot;: &quot;^4.17.1&quot;,
      &quot;faker&quot;: &quot;^5.2.0&quot;,
      &quot;graphql&quot;: &quot;^15.4.0&quot;,
      &quot;pg&quot;: &quot;^8.5.1&quot;,
      &quot;reflect-metadata&quot;: &quot;^0.1.13&quot;,
      &quot;type-graphql&quot;: &quot;^1.1.1&quot;,
      &quot;typeorm&quot;: &quot;^0.2.30&quot;,
      &quot;typeorm-seeding&quot;: &quot;^1.6.1&quot;
    },
    &quot;devDependencies&quot;: {
      &quot;@types/express&quot;: &quot;^4.17.11&quot;,
      &quot;@types/faker&quot;: &quot;^5.1.6&quot;,
      &quot;@types/jest&quot;: &quot;^26.0.20&quot;,
      &quot;@types/node&quot;: &quot;^14.14.21&quot;,
      &quot;jest&quot;: &quot;^26.6.3&quot;,
      &quot;nodemon&quot;: &quot;^2.0.7&quot;,
      &quot;ts-jest&quot;: &quot;^26.5.0&quot;,
      &quot;ts-node&quot;: &quot;^9.1.1&quot;,
      &quot;typescript&quot;: &quot;^4.1.3&quot;
    },
  </code></pre>