Open keithdarragh opened 3 years ago
Hi @keithdarragh, I think I've solved this problem, at least for me.
I was running the project with pg-mem
using Node 12, and I was getting an error when running jest saying that current_database()
was not a Postgres function or something like this. What I ended up doing was changing my Node version to a most recent one (in my case, version 16).
I hope this helps you.
Thanks for the feedback 🙂 Appreciate it.
I'll try to investigate when I have more time.
Happy to know you circumvented this issue.
+1 I'm seeing exactly the same issue but updating to the latest node did not help. I get the error when running the example here https://github.com/oguimbal/pg-mem/blob/master/samples/typeorm/simple.ts. Thanks a lot.
That may also be a Typorm version issue. I know that 0.2.29
works. Have you installed this version ? (cat node_modules/typeorm/package.json | grep version
... the minor version counts, I know that 0.2.30 has issues)
Thanks @oguimbal yes its the same issue. The error goes away when downgrading to 0.2.29.
I was able to fix the issue by using the registerFunction
for my use case (TypeORM with NestJS)
I was getting the error like others in this thread.
QueryFailedError: ERROR: function current_database() does not exist
HINT: 🔨 Please note that pg-mem implements very few native functions.
👉 You can specify the functions you would like to use via "db.public.registerFunction(...)"
🐜 This seems to be an execution error, which means that your request syntax seems okay,
but the resulting statement cannot be executed → Probably not a pg-mem error.
*️⃣ Failed SQL statement: SELECT * FROM current_database();
👉 You can file an issue...
But by setting up my tests like the following, everything is working splendidly on Node v14.15.0
and "pg-mem": "^1.9.17"
import { Test, TestingModule } from '@nestjs/testing';
import { IMemoryDb, newDb } from 'pg-mem';
import { Connection, Repository } from 'typeorm';
import { UsersService } from './users.service';
import { User } from './users.entity';
import { usersFixtures } from './fixtures';
describe('UsersService', () => {
let db: IMemoryDb;
let got: Connection;
let users: Repository<User>;
let service: UsersService;
beforeAll(async () => {
//==== create a memory db
db = newDb({
autoCreateForeignKeyIndices: true,
});
//==== define current_database
db.public.registerFunction({
implementation: () => 'test',
name: 'current_database',
});
//==== create a Typeorm connection
got = await db.adapters.createTypeormConnection({
type: 'postgres',
entities: [User],
});
//==== create tables
await got.synchronize();
users = got.getRepository(User);
//==== create entities
for (const user of usersFixtures()) {
const u = users.create(user);
await users.save(u);
}
});
afterAll(async () => {
await got.close();
});
const mockFindOne = jest.fn((id) => users.findOne(id));
const fakeUserService: Partial<UsersService> = {
findOne: mockFindOne,
};
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [{ provide: UsersService, useValue: fakeUserService }],
}).compile();
service = module.get<UsersService>(UsersService);
});
afterEach(() => {
mockFindOne.mockClear();
});
it('should be defined', () => {
expect(service).toBeDefined();
});
describe('find', () => {
it('should find one', async () => {
const user = await service.findOne(1);
expect(user).toMatchObject({
email: 'a',
password: 'b',
});
const user2 = await service.findOne(2);
expect(user2).toMatchObject({
email: 'bono',
password: 'u2sux',
});
expect(mockFindOne).toBeCalledTimes(2);
});
});
});
To add to the information on this, my experience is that:
QueryFailedError: column "columns.table_name" does not exist
QueryFailedError: ERROR: function current_database() does not exist
despite having registered the function, as described in the pg-mem docs (and above).I understand that typeorm 0.2.29 is listed as strict peer dependency, possibly for these reasons.
You're right.
This might also be usefull to workaround this problem.
@oguimbal thanks for the reply (and the great tool!). Unfortunately that workaround doesn't help for my situation. Ideally I need to get it working with the later versions (0.2.37 +) of typeorm as there are typeorm fixes that are needed in an application. And with those later versions I get no more error than the function isn't there despite registering it.
I'm currently in the process of testing a typescript app using Typeorm. Tried to create a few simple examples at first but it seems that pg-mem is throwing an error when trying to write the test. Application code works fine. Have I implemented wrong/is there a simple fix?
Thanks!
Stack trace
Typeorm entity:
Test