So I'm trying to have some global variables to use easily in my test files so I researched a lot and managed to make a jest setup file that runs before all my test files to initialize the global variables and this is the setup.ts
file
import app from'../src/express';
import request from 'supertest';
//Set Express app as global
global.app = request(app);
//TODO: Add global data
It's working fine but the autocomplete isn't working in my test files so after searching for the problem I found out I had to merge my new added variables to NodeJS.Global
and ended up doing so in a file called global.d.ts
declare global {
namespace NodeJS {
interface Global {
app: import('supertest').SuperTest<import('supertest').Test>;
}
}
}
but still, nothing is working tried other solutions but none worked.
Note
ts.config
"target": "es2019",
"moduleResolution": "node",
"module": "commonjs",
"lib": ["es2019"],
"sourceMap": true,
"outDir": "dist",
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noImplicitThis": true,
"resolveJsonModule": true,
"alwaysStrict": true,
"removeComments": true,
"noImplicitReturns": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"allowJs": true /* Allow javascript files to be compiled. */,
"typeRoots": [
"./types",
"node_modules/@types",
] /* List of folders to include type definitions from. */,
"types": [ "node","jest"],
},
"include": ["./src/**/*", "./utils/**/*", "localization", "./tests/**/*"],
"exclude": ["./seeds/**/*"]
jest.config.ts
/*
* For a detailed explanation regarding each configuration property and type check, visit:
* https://jestjs.io/docs/configuration
*/
export default {
clearMocks: true,
coverageProvider: "v8",
coverageDirectory: "coverage",
collectCoverage: true,
setupFiles: ["<rootDir>/tests/setup.ts"],
testMatch: [
"<rootDir>/tests/**/*.test.ts"
],
};