diff --git a/jest.config.js b/jest.config.js index ed9c3853ec..eaedf6a98c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,8 +9,8 @@ module.exports = { testEnvironment: 'jsdom', preset: 'ts-jest', testMatch: ['**/__tests__/**/*.test.[jt]s?(x)'], - setupFiles: ['dotenv/config'], - setupFilesAfterEnv: [require.resolve('jest-dom/extend-expect'), './jest.setup.ts'], + setupFiles: ['./jest.setup.ts'], + setupFilesAfterEnv: [require.resolve('jest-dom/extend-expect'), './jest.setupAfterEnv.ts'], moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '/', }), diff --git a/jest.setup.ts b/jest.setup.ts index 2c95f50812..cecad9c505 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -1,13 +1,4 @@ -import prettyFormat from 'pretty-format'; +import dotenv from 'dotenv'; +import path from 'path'; -global['prettyFormat'] = prettyFormat; - -jest.setTimeout(300000); - -// 把 console.error 转换成 error,方便断言 -(() => { - const spy = jest.spyOn(console, 'error'); - afterAll(() => { - spy.mockRestore(); - }); -})(); +dotenv.config({ path: path.resolve(process.cwd(), '.env.test') }); diff --git a/jest.setupAfterEnv.ts b/jest.setupAfterEnv.ts new file mode 100644 index 0000000000..2c95f50812 --- /dev/null +++ b/jest.setupAfterEnv.ts @@ -0,0 +1,13 @@ +import prettyFormat from 'pretty-format'; + +global['prettyFormat'] = prettyFormat; + +jest.setTimeout(300000); + +// 把 console.error 转换成 error,方便断言 +(() => { + const spy = jest.spyOn(console, 'error'); + afterAll(() => { + spy.mockRestore(); + }); +})();