From 39204e814965c238fa2f34311885217289f44f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=AB=E9=9B=A8=E6=B0=B4=E8=BF=87=E6=BB=A4=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E6=B0=94-Rairn?= <958414905@qq.com> Date: Mon, 10 Apr 2023 08:48:26 +0800 Subject: [PATCH] test: should load the .env.test (#1678) --- jest.config.js | 4 ++-- jest.setup.ts | 15 +++------------ jest.setupAfterEnv.ts | 13 +++++++++++++ 3 files changed, 18 insertions(+), 14 deletions(-) create mode 100644 jest.setupAfterEnv.ts diff --git a/jest.config.js b/jest.config.js index ed9c3853e..eaedf6a98 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 2c95f5081..cecad9c50 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 000000000..2c95f5081 --- /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(); + }); +})();