fix(jest): custom cli options not working

This commit is contained in:
chenos 2022-02-15 09:54:57 +08:00
parent 31691e1f8e
commit d81d1979b0
4 changed files with 39 additions and 54 deletions

9
jest.cli.js Normal file
View File

@ -0,0 +1,9 @@
const yargs = require('yargs');
if (yargs.argv.dbDialect) {
process.env.DB_DIALECT = yargs.argv.dbDialect;
}
console.log('DB_DIALECT: ', process.env.DB_DIALECT);
require('jest-cli/bin/jest');

View File

@ -2,7 +2,6 @@ import dotenv from 'dotenv';
import { existsSync } from 'fs';
import { resolve } from 'path';
import prettyFormat from 'pretty-format';
import yargs from 'yargs';
const envFile = existsSync(resolve(__dirname, '.env.test')) ? '.env.test' : '.env';
@ -10,10 +9,6 @@ dotenv.config({
path: resolve(__dirname, envFile),
});
if (yargs.argv.dbDialect) {
process.env.DB_DIALECT = yargs.argv.dbDialect as any;
}
global['prettyFormat'] = prettyFormat;
jest.setTimeout(300000);
@ -22,13 +17,13 @@ jest.setTimeout(300000);
(() => {
const spy = jest.spyOn(console, 'error');
beforeAll(() => {
spy.mockImplementation((message) => {
spy.mockImplementation((message) => {
console.log(message);
throw new Error(message);
});
});
afterAll(() => {
spy.mockRestore();
});
})();

View File

@ -13,7 +13,7 @@
"start-server": "ts-node-dev -r dotenv/config -r tsconfig-paths/register ./packages/api/src/index.ts",
"build": "lerna run build",
"build-docs": "dumi build",
"test": "jest",
"test": "node ./jest.cli.js",
"lint": "eslint ."
},
"resolutions": {

View File

@ -4,52 +4,33 @@ import { resolve } from 'path';
const start = Date.now();
const api = new Application({
database:
process.env.DB_DIALECT === 'sqlite'
? {
dialect: process.env.DB_DIALECT as any,
storage: resolve(process.cwd(), './db.sqlite'),
pool: {
max: 5,
min: 0,
acquire: 60000,
idle: 10000,
},
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
define: {},
sync: {
force: false,
alter: {
drop: false,
},
},
}
: {
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: 'postgres',
host: process.env.DB_HOST,
port: process.env.DB_PORT as any,
dialect: process.env.DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
pool: {
max: 5,
min: 0,
acquire: 60000,
idle: 10000,
},
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
define: {},
sync: {
force: false,
alter: {
drop: false,
},
},
},
database: {
storage: resolve(process.cwd(), './db.sqlite'),
username: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
host: process.env.DB_HOST,
port: process.env.DB_PORT as any,
dialect: process.env.DB_DIALECT as any,
dialectOptions: {
charset: 'utf8mb4',
collate: 'utf8mb4_unicode_ci',
},
pool: {
max: 5,
min: 0,
acquire: 60000,
idle: 10000,
},
logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
define: {},
sync: {
force: false,
alter: {
drop: false,
},
},
},
resourcer: {
prefix: process.env.API_BASE_PATH || '/api/',
},