test: should load the .env.test (#1678)

This commit is contained in:
被雨水过滤的空气-Rairn 2023-04-10 08:48:26 +08:00 committed by GitHub
parent bbc2db424f
commit 39204e8149
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 14 deletions

View File

@ -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: '<rootDir>/',
}),

View File

@ -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') });

13
jest.setupAfterEnv.ts Normal file
View File

@ -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();
});
})();