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

knex-mock can not work correctly #36

Closed
dadiorchen opened this issue Sep 26, 2020 · 5 comments
Closed

knex-mock can not work correctly #36

dadiorchen opened this issue Sep 26, 2020 · 5 comments

Comments

@dadiorchen
Copy link
Collaborator

dadiorchen commented Sep 26, 2020

Currently, we are using knex-mock to mock the knex, now we can use it to mock the db without problem, the problem is: once we mocked it, we can not UNMOCK it correctly. So, it would lead to test after the DB test (mocked) fail, it's because the previous DB tests break the real knex DB object.

How to reproduce the problem: modify the npm command:

    "testWatch": "NODE_ENV=test mocha --timeout 10000 --require co-mocha -w -b --ignore './server/repositories/**/*.spec.js'  './server/**/*.spec.js' './__tests__/supertest.js'"

to

    "testWatch": "NODE_ENV=test mocha --timeout 10000 --require co-mocha -w -b  './server/**/*.spec.js' './__tests__/supertest.js'"

Actually, just because of the problem, we must ignore the repository tests, they would affect others.

@ZavenArra
Copy link
Contributor

const mockDb = require('mock-knex');
mockDb.mock(Database.getKnex())

then later..
mockDb.unmock(Database.getKnex())

I use this mock/unmock in my unit tests and it's fine. Do you see some other problem?

@dadiorchen
Copy link
Collaborator Author

Thanks, Zaven, yes, my unit tests works well, the problem is, after the unit test, if I run the integration test in the same process, then the tests will fail, because the DB object can not unmock correctly, I guess.

image

@ZavenArra
Copy link
Contributor

ZavenArra commented Sep 26, 2020 via email

@dadiorchen
Copy link
Collaborator Author

image
The var 'knex' here bings a little confusion, it's not require('knex'), it's require('kenx')({client:'pg'...

@ZavenArra
Copy link
Contributor

one thing I notice is that you can just do mock and unmock in before() and after(), that than in beforeEach() afterEach().

I also don't uninstall the tracker, and everything is working. Try it??

before( function(done) {
mockDb.mock(Database.getKnex())
tracker.install();
done()
});

after( function(done) {
mockDb.unmock(Database.getKnex())
done()
});

@Kpoke Kpoke closed this as completed Jun 27, 2023
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

3 participants