I have a very simple Typescript project with following structure
project/
├── build/
│ ├── index.d.ts
│ ├── index.js
│ └── index.js.map
├── docs/
│ └── README.md
├── source/
│ └── index.ts
├── tests/
│ └── index.test.ts
├── types/
│ └── global.d.ts
├── jest.config.json
├── package.json
└── tsconfig.json
I have a custom declaration file under /types directory. The content looks like this.
export {};
declare global {
interface Error {
deepClone(): this;
}
}
I have updated my tsconfig.json to respect my type declaration.
{
"compilerOptions": {
"module": "ES2022",
"target": "ES2022",
"allowJs": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"strict": true,
"outDir": "./build",
"sourceMap": true,
"isolatedModules": false
},
"include": ["source/**/*.ts", "types/global.d.ts"],
"exclude": ["node_modules", "tests", "build"]
}
I can perfectly build the project. But when i want to write tests for this JEST seems not beeing able to pick up the global.d.ts.
In my index.test.ts there is following line.
const deepCloneSpy = jest.spyOn(Error.prototype, "deepClone");
JEST now complains that the deepClone method is not on the Error.prototype even tough it should because of the custom type declaration.
When i copy the global.d.ts to the build/ directory it works. I am confused why JEST is not pickung up the global.d.ts because it’s specified in the include
section of the tsconfig.json file. Why does it need to be in the build/ folder?
1