iamasteriix / gqlts-server

Boilerplate typescript server.
1 stars 0 forks source link

Jest global setup not working. #5

Open iamasteriix opened 2 years ago

iamasteriix commented 2 years ago

I am trying to set up global testing for the project, but I keep getting the error

TypeError: Jest: Got error running globalSetup - /<__directory>/graphql-ts-server/src/setup.ts, reason: globalSetup file must export a function at /<__directory>/graphql-ts-server/src/setup.ts
    at /<__directory>/graphql-ts-server/node_modules/@jest/core/build/runGlobalHook.js:120:21
    at ScriptTransformer.requireAndTranspileModule (/<__directory>/graphql-ts-server/node_modules/@jest/transform/build/ScriptTransformer.js:905:24)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runGlobalHook (/<__directory>/graphql-ts-server/node_modules/@jest/core/build/runGlobalHook.js:116:9)
    at async runJest (/<__directory>/graphql-ts-server/node_modules/@jest/core/build/runJest.js:372:5)
    at async _run10000 (/<__directory>/graphql-ts-server/node_modules/@jest/core/build/cli/index.js:338:7)
    at async runCLI (/<__directory>/graphql-ts-server/node_modules/@jest/core/build/cli/index.js:190:3)
    at async Object.run (/<__directory>/graphql-ts-server/node_modules/jest-cli/build/cli/index.js:155:37)

I'm not exactly sure why, and somehow there doesn't seem to be a good solution for the provided reason globalSetup file must export a function at /<__directory>/graphql-ts-server/src/setup.ts.

The current project structure (as far as testing is concerned) is:

|-- src
    |-- modules
        |--register
           |--register.test.ts
    |--utils
        |--confirmEmailLink.test.ts
    |--setup.ts
|--jest.config.ts

Now, I've made a few changes to the setup.ts file, such as trying to define a function that resolves the setup promise then exporting it (guessing from the hint provided by the error message), but I always get the same exact error. So this is the most basic form that produces the endpoint which the tests in register.tests.ts use:

setup.ts

export const setup = async () => {
    const app = await server();
    const url = app.url;
    process.env.TEST_URL = `${url}`;
};

This function is meant to run before all the tests in the src directory.

Then I set up my jest.config.ts as:

import { Config } from '@jest/types';

const config: Config.InitialOptions = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  globalSetup: './src/setup.ts',
  verbose: true,
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
}

export default config;
iamasteriix commented 2 years ago

Apparently Jest does not recognize es6 syntax. I changed setup.ts to

module.exports = async () => {
    const app = await server();
    const url = app.url;
    process.env.TEST_URL = `${url}`;
};

and got a different error:

/<dir>/graphql-ts-server/src/modules/register/resolvers.ts:1
import * as bcrypt from 'bcryptjs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
...