From d81d1979b0da805bd4106bac8c205536b54e4296 Mon Sep 17 00:00:00 2001 From: chenos Date: Tue, 15 Feb 2022 09:54:57 +0800 Subject: [PATCH] fix(jest): custom cli options not working --- jest.cli.js | 9 +++++ jest.setup.ts | 9 ++--- package.json | 2 +- packages/api/src/index.ts | 73 +++++++++++++++------------------------ 4 files changed, 39 insertions(+), 54 deletions(-) create mode 100644 jest.cli.js diff --git a/jest.cli.js b/jest.cli.js new file mode 100644 index 0000000000..bb39cd9b7c --- /dev/null +++ b/jest.cli.js @@ -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'); diff --git a/jest.setup.ts b/jest.setup.ts index 161bc0b20a..bcf8002016 100644 --- a/jest.setup.ts +++ b/jest.setup.ts @@ -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(); }); })(); + diff --git a/package.json b/package.json index 6e5ac3996b..fe7dca661c 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index df7633d6bb..93f4f4889b 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -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/', },