[#3025] manually trigger the OnBeforeServe hook for tests.ApiScenario

This commit is contained in:
Gani Georgiev 2023-07-31 12:27:22 +03:00
parent 6d2677a5e3
commit f652dc71bb
1 changed files with 9 additions and 0 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/labstack/echo/v5" "github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/apis" "github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
) )
// ApiScenario defines a single api request test case/scenario. // ApiScenario defines a single api request test case/scenario.
@ -55,6 +56,8 @@ func (scenario *ApiScenario) test(t *testing.T) {
var testApp *TestApp var testApp *TestApp
var testAppErr error var testAppErr error
if scenario.TestAppFactory != nil { if scenario.TestAppFactory != nil {
// @todo consider passing the testing instance to the factory and maybe remove the error from the declaration
// (see https://github.com/pocketbase/pocketbase/discussions/3025)
testApp, testAppErr = scenario.TestAppFactory() testApp, testAppErr = scenario.TestAppFactory()
} else { } else {
testApp, testAppErr = NewTestApp() testApp, testAppErr = NewTestApp()
@ -69,6 +72,12 @@ func (scenario *ApiScenario) test(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
// manually trigger the serve event to ensure that custom app routes and middlewares are registered
testApp.OnBeforeServe().Trigger(&core.ServeEvent{
App: testApp,
Router: e,
})
if scenario.BeforeTestFunc != nil { if scenario.BeforeTestFunc != nil {
scenario.BeforeTestFunc(t, testApp, e) scenario.BeforeTestFunc(t, testApp, e)
} }