【发布时间】:2022-02-11 20:24:56
【问题描述】:
所以我试图在我的测试文件中轻松使用一些全局变量,所以我进行了很多研究并设法制作了一个在我的所有测试文件之前运行的笑话设置文件以初始化全局变量,这就是 @ 987654321@文件
import app from'../src/express';
import request from 'supertest';
//Set Express app as global
global.app = request(app);
//TODO: Add global data
它工作正常,但自动完成在我的测试文件中不起作用,所以在搜索问题后我发现我必须将新添加的变量合并到 NodeJS.Global 并最终在一个名为 global.d.ts 的文件中这样做
declare global {
namespace NodeJS {
interface Global {
app: import('supertest').SuperTest<import('supertest').Test>;
}
}
}
但是,尝试其他解决方案仍然没有任何效果。
注意
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"
],
};
【问题讨论】:
标签: typescript jestjs