the problem is i'm using jest to test some functionalities and i want to do a global setup to log user and initialize prisma connection and use them across the tests, but when a extend global interface visual code says everything is okey but i get error: has no index signature
i mad a simple repo for the issue is anyone could help please:
u can just run yarn test to check the errors
https://github.com/Anas-Qadil/issueTS/tree/main/__tests__
#extending global interface in typescript if anyone
1 messages · Page 1 of 1 (latest)
multiple things
- about your tsconfig
- you don't need the
pathssetting in your case, it doesn't do what you think it does, so can (and should) remove it
- in the
includeproperty,"jest.config.ts"is already included in the**/*pattern, you can remove"jest.config.ts"
- for the
includeproperty, you don't need to include everything, only include what you need, basically["src/**/*.ts", "__tests__/**/*.ts", "jest.config.ts"](also I don't think you need to includejest.config.tsbut it can stay for now)
- since you nbow included only what you need, you don't need to exclude the
node_modulesfolder, you can remove the"exclude"property
- folder naming conventions
- naming your test folder
__tests__isn't common, I'd suggest using the more commontestortestsname
- typings
- in your
__tests__/globals.d.ts, you should use the exact types for your objects, don't useany, it provides no information at all, and might cause problem further in your code; you can always import the types you need from other TS files
that's why you get this error:// this gives error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
- still in
__tests__/globals.d.ts, don't usevar, instead, use the more modernletorconstkeywords, especially if that global variable is a constant and can't be reassigned later
@rotund sphinx
@radiant herald i created a basic ts repo and most of configs came with it names, ts/jest configs ...
i'm using all of this to avoid any cz i can just use (global as any).user = "user". i used it there just to make the matter clear
i tried everything o stack overflow gpt everithing and none solved this samll problem
- using let instead of var turns everything red
var has some utility if you're doing it intentionally
mainly inside of functions, not in the gloabl scope
but declaring a let works just like declaring a var, in the end, it's the already existing variable that matters, TS declaration files don't create any variables
if it's being assigned as a global what does it matter
xD can anyone fix the main problem that i cant assign values to the vars inside the global
he's making a global declaration file
why does it matter if he uses var or let
cause it's a declaration file, .d.ts, not .ts
pretty sure OP wasn't talking about that file, but global-setup.ts
ah, my mistake then
i just saw globals.d.ts, sorry
well it's not an async function
it's not between var and let, it's between var and const
var gives no information about if the variable a constant or not
const tells the variable is a constant and cannot be reassigned
and since you are using const for constants, you might as well use let form mutable variables
I just use let for everything personally, just a matter of preference
i get your point though
most people like const
if i use let or const visual code yell saying : Property 'user' does not exist on type 'typeof globalThis'
while using var its good but there is a runtime error
and i saw some stackoverflow blogs saying in my case i have to use var
Property 'user' does not exist on type 'typeof globalThis'
it means your definition file isn't included correctly
where do you call that global-setup function
can you link the issue, it shouldn't be required
that function get called first when u run yarn test its a part of jest
but tbh, don't use global variables that way
its supposed to be async? I dont know it that well
don't add anything to the global scope, you don't need to
just seems weird
its just a typing in that function, i just removed the all the code and forgot it there
you can jusy create variables and export them
const globalSetup = async () => {
// this gives error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
global.user = "user";
global.agentRole = "agentRole";
global.role = "role";
global.prisma = "prisma";
};
im wondering why this is async
it can be removed its not required
you're sure it makes no difference? cause you're not doing anything asynchronous there. I'm just trying to eliminate anything weird 1 by 1
these solutions seems legit but they dont work
try changing this to explicitly include `./tests/globals.d.ts'
i was earlier but i removed the code that does asyncrounse and i removed it now, same thing
it's included in the tests/**/*.ts, so no need to
also, if you read the issue, the type not found is for the global object
yes
not the typings
will ** include 0 intermediate directories?
can you try adding a lib?
i can never keep all the damn glob patterns straight
yes, ** is any dir
wdym
From allowJs to useDefineForClassFields the TSConfig reference includes information about all of the active compiler flags setting up a TypeScript project.
"lib": ["ES6"]
that's helpful, thank you. I remember running into a problem with ** requiring at least one subdirectory with some glob syntax, that's good to know it doesnt in TS
i did now, still same error
i kinda found a way around:
i created another file called type.ts i declared the type there and imported it in global-setup.
and it works
but idk it doesnt in the previous case
it has someting to do with file name, when i remove .d and use just global.ts it works. but why?
Configuring Jest
The Jest philosophy is to work great by default, but sometimes you just need more configuration power.
It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named jest.config.js|ts|mjs|cjs|json. You can use --config flag to pass an explicit path to the file.
import type {Config} from 'jest';
const config: Config = {
globals: {
DEV: true,
},
};
export default config;
my 2c is it thinks you're trying to add global variables to your entire NODE app but really you just want them to apply to the test env
which seems to take a particular syntax and filename setup
Any global variables that are defined through globalSetup can only be read in globalTeardown. You cannot retrieve globals defined here in your test suites.