我正在尝试使用Jest和Knex测试GraphQL服务器。我花费了很长时间才弄清楚如何在TypeScript中使用knexfile,但现在一切都在开发和生产环境下正常工作,除了测试。
这是我当前的knexfile.ts
:
// knexfile.ts
const defaults = {
client: 'pg',
connection: {
host: DB_HOST,
user: DB_USER,
password: DB_PASSWORD,
database: DB_DATABASE
},
pool: {
min: 2,
max: 10
},
migrations: {
extension: 'ts',
directory: './migration',
tableName: 'knex_migrations'
},
seeds: {
extension: 'ts',
directory: './seed'
}
};
interface KnexConfig {
[key: string]: object;
}
const knexConfig: KnexConfig = {
local: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
development: {
...defaults,
debug: true,
useNullAsDefault: true
},
production: {
...defaults
}
};
/**
* `export default` does not work, causes `client` missing problem
* at database migration.
*/
export = knexConfig;
这是Jest的全局设置:
// globalSetup.ts
export = async () => {
try {
// Start http server
await httpServer.listen(PORT);
// Rollback and migrate
// await knex.migrate.rollback().then(() => knex.migrate.latest());
knex.migrate.latest();
} catch (err) {
// Log the error
logger.error('', err);
}
};
这是全局拆卸过程:
// globalTeardown.ts
export = async () => {
try {
await knex.migrate.rollback();
// Shutdown server
httpServer.close(() => logger.info('Server closed'));
} catch (err) {
// Log the error
logger.error('', err);
}
};
它一直给我报错:
Unhandled rejection SyntaxError: Unexpected token *
/home/my/knex-graphql/migration/20190821235716_create_user.ts:1
import * as Knex from 'knex';
^
SyntaxError: Unexpected token *
at Module._compile (internal/modules/cjs/loader.js:872:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
at Module.load (internal/modules/cjs/loader.js:790:32)
at Function.Module._load (internal/modules/cjs/loader.js:703:12)
at Function.<anonymous> (/home/my/knex-graphql/node_modules/@sentry/node/src/integrations/console.ts:37:43)
at Function._load (/home/my/knex-graphql/node_modules/@sentry/node/src/integrations/http.ts:73:43)
at Module.require (internal/modules/cjs/loader.js:830:19)
at require (internal/modules/cjs/helpers.js:68:18)
at FsMigrations.getMigration (/home/my/knex-graphql/node_modules/knex/lib/migrate/sources/fs-migrations.js:84:12)
at /home/my/knex-graphql/node_modules/knex/lib/migrate/Migrator.js:82:69
at arrayFilter (/home/my/knex-graphql/node_modules/lodash/lodash.js:582:11)
at filter (/home/my/knex-graphql/node_modules/lodash/lodash.js:9173:14)
at /home/my/knex-graphql/node_modules/knex/lib/migrate/Migrator.js:81:13
at tryCatcher (/home/my/knex-graphql/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/my/knex-graphql/node_modules/bluebird/js/release/promise.js:517:31)
at Promise._settlePromise (/home/my/knex-graphql/node_modules/bluebird/js/release/promise.js:574:18)
From previous event:
at Migrator.latest (/home/my/knex-graphql/node_modules/knex/lib/migrate/Migrator.js:71:8)
at /home/my/knex-graphql/test/global/setup.ts:24:32
at Generator.next (<anonymous>)
at fulfilled (/home/my/knex-graphql/test/global/setup.ts:5:58)
at processTicksAndRejections (internal/process/task_queues.js:85:5)
技术栈: Apollo-server-express,TypeScript, Knex.js,PostgreSQL,Jest