Skip to content
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

Open
jon9090 opened this issue Feb 5, 2023 · 3 comments
Open

How to test a function that using knex? #5475

jon9090 opened this issue Feb 5, 2023 · 3 comments

Comments

@jon9090
Copy link

jon9090 commented Feb 5, 2023

I have this function and I want to write a test.

const getLastUpdatedUsers = async(date) => {
 const users = await knex('users').select('id', 'name').where('update_at', '<',  date);
 return users;
}

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?

@Naddiseo
Copy link
Contributor

Naddiseo commented Feb 9, 2023

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.

@jon9090
Copy link
Author

jon9090 commented Feb 9, 2023

@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.
so I can't use create table or drop table. is it possible to run knex command to get all the schema and data from the database and use it in memory?

@Naddiseo
Copy link
Contributor

Naddiseo commented Feb 9, 2023

to do that I need to create a mock database right

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 test environment.

each entity is connected to different tables and the data is about 1T. so I can't use create table or drop table.

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.

is it possible to run knex command to get all the schema and data from the database and use it in memory?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants