我正在构建一个 nodejs |使用 typeorm 表达 API,当通过 postman 或某些客户端调用路由时,一切正常。该问题出现在用 jest 进行的测试中,我收到以下错误:“未为此实体设置数据源”。我错过了什么吗?
- Node :16.15.1
- "超测": "^6.2.4",
- "ts-node": "^10.9.1",
- "typeorm": "^0.3.7",
- “ typescript ”:“^4.7.4”
- "node-postgres": "^0.6.2",
- "表单":"^7.1.0",
- "pg": "^8.7.3",
- “开 Jest ”:“^28.1.3”,
- "ts-jest": "^28.0.7"
- "表达": "^4.18.1",
我正在使用 Postgres 数据库进行测试。
开 Jest 配置:
module.exports = async () => {
return {
preset: "ts-jest",
testEnvironment: "node",
verbose: true,
moduleNameMapper: {
"@exmpl/(.*)": "<rootDir>/src/$1"
},
};
}
数据源文件:
import { DataSource } from "typeorm"
import path from "path"
export const AppDataSource = new DataSource({
name: "default",
migrationsTableName: 'migrations',
type: "postgres",
host: process.env.POSTGRES_HOST,
port: Number(process.env.POSTGRES_PORT),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
synchronize: false,
logging: false,
entities: [path.join(__dirname, '../entities/**/*.entity.js')],
migrations: [path.join(__dirname, '../migrations/**/*.js')],
subscribers: [],
cache: true
})
测试文件:
import path from "path";
import request from "supertest"
import { AppDataSource } from "../src/db/DataSource"
import { Role } from "../src/entities/User/Role.enum"
import app from "../src/App"
let connection: any;
describe('Auth', () => {
beforeAll(async () => {
AppDataSource.setOptions({
entities: [path.join(__dirname, '../dist/entities/**/*.entity.js')],
migrations: [path.join(__dirname, '../dist/migrations/**/*.js')],
synchronize: true,
dropSchema: true,
})
connection = await AppDataSource.initialize()
await connection.synchronize(true)
})
afterAll(async () => {
await connection.destroy()
})
test('should signUp', async () => {
const response = await request(app)
.post('/auth/register')
.send({
firstName: "test",
lastName: "test",
email: "test@test.cc",
role: Role.USER,
status: "active",
password: "Pa$$w0rd"
})
console.log(response.body)
//Temporary pass
expect(2+2).toBe(4)
})
})
Babel 配置:
module.exports = {
presets: [
['@babel/preset-env', {targets: {node: 'current'}}],
'@babel/preset-typescript',
],
plugins: [
['@babel/plugin-syntax-decorators', { decoratorsBeforeExport: true }],
['@babel/plugin-proposal-decorators', { decoratorsBeforeExport: true }],
['@babel/plugin-proposal-class-properties', { "loose": true }],
]
};
配置文件:
{
"compilerOptions": {
/* Language and Environment */
"target": "es2016",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
/* Modules */
"module": "commonjs",
"moduleResolution": "node",
"rootDirs": ["src/", "config/"],
"typeRoots": ["./node_modules/@types", "src/typings"],
/* JavaScript Support */
/* Emit */
"declaration": true,
"sourceMap": true,
"outDir": "dist",
"removeComments": true,
/* Interop Constraints */
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
/* Type Checking */
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictPropertyInitialization": false,
/* Completeness */
"skipLibCheck": true
},
"include": ["src/**/*"]
}
最佳答案
经过一番研究,我发现问题出在实体文件夹的路径错误。它不应该是编译文件的路径,而是 typescript 源文件的路径。
我发布这个答案以防有人遇到同样的问题。
我替换了这个:
AppDataSource.setOptions({
entities: [path.join(__dirname, '../dist/entities/**/*.entity.js')],
migrations: [path.join(__dirname, '../dist/migrations/**/*.js')],
synchronize: true,
dropSchema: true,
})
用这个:
AppDataSource.setOptions({
entities: entities: [path.join(__dirname, '../../src/entities/**/*.entity.ts')],
migrations: [path.join(__dirname, '../../src/migrations/**/*.ts')],
synchronize: true,
dropSchema: true,
})
我不确定为什么会这样,所以如果有人知道解释它会很棒。
关于node.js - Jest 测试失败,错误为 "DataSource is not set for this entity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73304907/