用typescript在jest中扩展全局



所以我想在我的测试文件中使用一些全局变量所以我研究了很多,并设法制作了一个jest设置文件,在我所有的测试文件之前运行初始化全局变量这是setup.ts文件

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"
],
};

如果您想使用它像app(而不是global.app),那么全局声明它:

declare global {
const app: import('supertest').SuperTest<import('supertest').Test>;
}

我有一个类似的用例,我需要在所有测试中全局可用的safeSubscribe助手。
该函数将执行订阅,然后在常规afterEach钩子中执行取消订阅,在任何测试之后执行。

我是这样做到的:

// test/setup/safe-subscribe.ts
import { Observable, Subscription } from 'rxjs';
const subscriptions: Subscription[] = [];
const safeSubscribeFn = <T>(source: Observable<T>): T[] => {
const res: T[] = [];
const subscription = source.subscribe((value) => {
res.push(value);
});
subscriptions.push(subscription);
return res;
};
global.safeSubscribe = safeSubscribeFn; // Add helper implementation
global.afterEach(() => {
while (subscriptions.length > 0) {
const subscription = subscriptions.pop();
subscription?.unsubscribe();
}
});
declare global {
// See: https://stackoverflow.com/a/68328575/12292636
var safeSubscribe: typeof safeSubscribeFn;
}
// jest.config.js
module.exports = {
...,
setupFilesAfterEnv: [
...,
'./test/setup/safe-subscribe.ts', // Import your setup in jest config
],
};
// tsconfig.spec.json
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/spec",
"module": "commonjs",
"types": ["jest", "node"],
"resolveJsonModule": true,
"esModuleInterop": true,
},
"files": ["src/polyfills.ts", "test/setup/*.ts"], // Add it there
"include": ["src/**/*.d.ts", "src/**/*.spec.ts", "test/setup/*.ts"] // And also there
}
现在,可以在测试中使用全局定义的safeSubscribe:
// foo.spec.ts
it('should emit value', () => {
// Given
const outputs = safeSubscribe(service.myObservable$); // global import with typescript stuff ✔️
// When
actions$.next(action);
// Then
expect(outputs).toHaveLength(1);
});

最新更新