-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to test a function that using knex? #5475
Comments
If you want to test the actual sql query you'll need a test database. knex's own test suite has integration testing similar to what you're looking for. |
@Naddiseo, to do that I need to create a mock database right? my problem is my database is too large and very complex with custom types and a lot of rules enforced by fk. each entity is connected to different tables and the data is about 1T. |
Yes, don't use a production database with actual data for testing. The knexfile allows for different configs depending on which environment you're in, you can create a separate
Usually with integration testing you create test data specifically for what you're testing during the test itself. If you need to clear data state between tests, I've found truncating the tables to be fastest. Integration testing tends to be very slow though.
Your test database should be just the schemas, custom types, triggers etc, but no data from production. You should be able to use your existing knex migration files to create the test database. |
I have this function and I want to write a test.
How do I write a test for this function? do I mock the knex? do I have to connect to the "test database" and run the actual SQL query?
Basically, I want to make sure I write my knex query right. this is mean if the next developer change to
update_at > date
it should cause an error in tests.In knex, I build some logic (
where
) and I want to test it. How to do it?The text was updated successfully, but these errors were encountered: