Skip to content

Commit d780f45

Browse files
test(all): improve & greatly speed up tests (#215)
* chore(tests): refactor tests to use common cleanup * chore(lint): fix lint errors * test(all): improve & greatly speed up tests Closes #212, #213, #214 * chore(ci): fix ci tests * test(setup): fix Prisma error correctly, credits to @jellebuitenhuisis
1 parent d4af641 commit d780f45

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

.github/workflows/playwright.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: |
3030
cp .env.example .env
3131
- name: "🎭 Run Playwright tests"
32-
run: npx playwright test
32+
run: npm run test
3333
- name: "📤 Upload Artifacts"
3434
uses: actions/upload-artifact@v4
3535
if: always()

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
"dev": "next dev --turbo",
1515
"lint": "next lint",
1616
"lint:fix": "npm run lint -- --fix",
17-
"start": "node ./node_modules/next/dist/bin/next start",
17+
"start": "next start",
1818
"typecheck": "tsc --noEmit",
19-
"test": "playwright test"
19+
"test": "npm run build && playwright test"
2020
},
2121
"prisma": {
2222
"seed": "node prisma/seed.js"

playwright.config.ts

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ export default defineConfig({
1515
/* Fail the build on CI if you accidentally left test.only in the source code. */
1616
forbidOnly: !!process.env["CI"],
1717
retries: 0,
18-
/* Opt out of parallel tests */
19-
workers: 1,
2018
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
2119
reporter: [["list", { printSteps: true }], ["html"]],
2220
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */

src/auth.ts

+6
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ export const { auth, handlers, signIn, signOut } = NextAuth({
6565
allowDangerousEmailAccountLinking: true,
6666
}),
6767
],
68+
/**
69+
* We trust our deployment provider to set the HOST header safely.
70+
* If you don't trust your deployment provider to set the HOST header safely, you should set this to `false`.
71+
* @see https://authjs.dev/reference/core#trusthost
72+
*/
73+
trustHost: true,
6874
});

tests/helpers/db/index.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,45 @@ import { exec } from "child_process";
88
import { promisify } from "util";
99

1010
export class DbHelper {
11-
private client: PrismaDbClient | null = null;
12-
private container: StartedPostgreSqlContainer | null = null;
13-
private readonly execAsync = promisify(exec);
14-
private readonly cwd = new URL("..", import.meta.url);
11+
#client: PrismaDbClient | null = null;
12+
#container: StartedPostgreSqlContainer | null = null;
13+
readonly #execAsync = promisify(exec);
14+
readonly #cwd = new URL("..", import.meta.url);
1515

1616
static async create(): Promise<DbHelper> {
1717
const dbHelper = new DbHelper();
18-
dbHelper.container = await new PostgreSqlContainer().start();
19-
dbHelper.client = await dbHelper.setupDatabase();
18+
dbHelper.#container = await new PostgreSqlContainer().start();
19+
dbHelper.#client = await dbHelper.setupDatabase();
2020
return dbHelper;
2121
}
2222

2323
getClient(): PrismaDbClient {
24-
if (!this.client) {
24+
if (!this.#client) {
2525
throw new Error("PrismaClient has not been initialized");
2626
}
27-
return this.client;
27+
return this.#client;
2828
}
2929

3030
getContainer(): StartedPostgreSqlContainer {
31-
if (!this.container) {
31+
if (!this.#container) {
3232
throw new Error("Container has not been initialized");
3333
}
34-
return this.container;
34+
return this.#container;
3535
}
3636

3737
async setupDatabase(): Promise<PrismaDbClient> {
38-
await this.execAsync("npm run db:push", {
38+
await this.#execAsync("npm run db:push", {
3939
env: {
4040
...process.env,
41-
DATABASE_URL: this.container!.getConnectionUri(),
41+
DATABASE_URL: this.#container!.getConnectionUri(),
4242
},
43-
cwd: this.cwd,
43+
cwd: this.#cwd,
4444
encoding: "utf-8",
4545
});
4646
return new PrismaDbClient({
4747
datasources: {
4848
db: {
49-
url: this.container!.getConnectionUri(),
49+
url: this.#container!.getConnectionUri(),
5050
},
5151
},
5252
log: [
@@ -65,4 +65,9 @@ export class DbHelper {
6565
await this.getClient().survey.deleteMany();
6666
await this.getClient().role.deleteMany();
6767
}
68+
69+
async stop() {
70+
await this.getClient().$disconnect();
71+
await this.getContainer().stop();
72+
}
6873
}

tests/helpers/test-setup.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class TestSetup {
4343
nextProcess,
4444
cleanup: async () => {
4545
await page.close();
46-
await dbHelper.getContainer().stop();
4746
await killAllProcesses(nextProcess);
47+
await dbHelper.stop();
4848
},
4949
};
5050
}
@@ -57,7 +57,7 @@ export class TestSetup {
5757
(res, rej) => {
5858
const nextProcess = spawn(
5959
"npm",
60-
["run", "dev", "--", "--port", "0"],
60+
["run", "start", "--", "--port", "0"],
6161
{
6262
cwd: this.cwd,
6363
shell: true,

0 commit comments

Comments
 (0)