Integration test suggestion? #603
-
I'm using pocketbase with a simple nodejs api server. Since pocketbase is being actively developed. I'd like to add some integration test in my project. However, the only way I can do is mock all the pocketbase js sdk in my jest test case now. Is there any better suggestion to run pocketbase like sqlite with :memory config so the data would be seperated from each test case? I really like the concept of pocketbase. Every decision made consist with the priciple of ease of use and simplicity. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hm, complete integration testing with an external backend is difficult to do. PocketBase provides some test helpers, but they are useful only when you embed PocketBase in your Go app - https://pocketbase.io/docs/testing/. You can't start PocketBase with in memory sqlite, but even if that was possible you still will not have a separate db while running the same PocketBase instance. For full integration tests, you'll need not only the database but the file storage abstraction and therefore it may be easier to just start PocketBase with an entire ./pocketbase serve --dir="/path/to/test_pb_data/" The internal integration tests uses similar approach, but since we have access to the Go code, we can easily copy the test data to a new location and run the tests against the temporary copy without interfering with the other tests. With nodejs I'm not sure how this could be achieved. I would try the following:
const { execSync } = require("node:child_process");
// you can use any other library for copying directories recursively
const fse = require("fs-extra");
let controller; // this will be used to terminate the PocketBase process
const srcTestDirPath = "/path/to/test_pb_data";
const tempTestDirPath = "/path/to/temp_test_pb_data";
beforeEach(() => {
// copy test_pb_data to a temp location
fse.copySync(srcTestDirPath, tempTestDirPath);
controller = new AbortController();
// start PocketBase with the test_pb_data
execSync("./pocketbase serve --dir=" + tempTestDirPath, { signal: controller.signal});
});
afterEach(() => {
// stop the PocketBase process
controller.abort();
// clean up the temp test directory
fse.removeSync(tempTestDirPath);
});
test('test against an actual PocketBase server running on localhost:8090', () => {
...
}); The above would work only if your tests are executed synchronously (for parallel execution, you'll need additional setup to switch between the different instances). But PocketBase startup time is almost instantaneous, so it shouldn't affect too much your tests performance. |
Beta Was this translation helpful? Give feedback.
Hm, complete integration testing with an external backend is difficult to do. PocketBase provides some test helpers, but they are useful only when you embed PocketBase in your Go app - https://pocketbase.io/docs/testing/.
You can't start PocketBase with in memory sqlite, but even if that was possible you still will not have a separate db while running the same PocketBase instance. For full integration tests, you'll need not only the database but the file storage abstraction and therefore it may be easier to just start PocketBase with an entire
test_pb_data
directory:./pocketbase serve --dir="/path/to/test_pb_data/"
The internal integration tests uses similar approach, but since we have a…