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

はじめてのE2Eテストを作成する #42

Merged
merged 52 commits into from
Feb 7, 2025

Conversation

kazuma-naka
Copy link
Contributor

@kazuma-naka kazuma-naka commented Jan 5, 2025

#28

概要

ローカル環境のテストユーザーを使用してログイン、ログアウトをする。

2025年1月5日現在の問題 ログアウトをしても自動的に画面が更新されないため `await page.reload();` が必要となる。 しかし ` await page.waitForTimeout(1500);` を追加しないと flaky なテストとなる(ログアウトは非同期処理でログアウト前にリロードされてしまうため `Timeout` が必要)
2025年1月13日(このPRで修正済み) `login`と`signUp`ページ内のロゴ、`pjord.svg` の `height` が定義されておらずエラーが起こったので新たに高さを定義
2025年1月16日 CI のテスト内で Supabase の立ち上げ、 Next.js のビルドと開始 E2E テストでログイン、ログアウトを行いヘッダーのボタンが変わるのかをテストするため実際に Supabase と Next.js を `ci.yml` の `test` 内で環境を構築しました。 [この記事](https://playwright.dev/docs/ci-intro)を参考に `ci.yml` を修正
.env.localtest のインスタンス内で動的に作成する

npm run build をCI環境で実行したさいに supabaseUrl is required. のエラーが出ました。 .env.local はリモートに追加されないので supabase の url と anon key が定義されずビルドに失敗すると考えました。エラーが起こった個所に createClient() が使用されていたため。

 STATUS_OUTPUT=$(npx supabase status)
 API_URL=$(echo "$STATUS_OUTPUT" | grep "API URL:" | awk '{print $3}')
 ANON_KEY=$(echo "$STATUS_OUTPUT" | grep "anon key:" | awk '{print $3}')
 echo "NEXT_PUBLIC_SUPABASE_URL=$API_URL" > .env.local
 echo "NEXT_PUBLIC_SUPABASE_ANON_KEY=$ANON_KEY" >> .env.local

npx supabase status の例

    API URL: http://localhost:54321
    DB URL: postgresql://postgres:postgres@localhost:54322/postgres
    Studio URL: http://localhost:54323
    Inbucket URL: http://localhost:54324
    anon key: eyJh......
    service_role key: eyJh......
  1. STATUS_OUTPUT の変数にnpx supabase status の結果を代入する
  2. API_URLとANON_KEYをSTATUS_OUTPUTから抜き取って代入する
  3. .env.local を動的に作成して NEXT_PUBLIC_SUPABASE_URL=$API_URL, NEXT_PUBLIC_SUPABASE_ANON_KEY=$ANON_KEY で生成された URL と Anon Key を書き込む
  4. npm run build をする
  5. npm run start をバックグランドで実行する
  6. 10 秒待つ
  7. npm run test を実行する
2025年 1月19日 テストCI環境でデータベースをE2Eテスト実行前、実行後にリセットする。
  • seed.test.sql を作成
    テスト環境のための seed.sql ファイル。
TRUNCATE "auth"."users" RESTART IDENTITY CASCADE;
TRUNCATE companies RESTART IDENTITY CASCADE;

seed を使用する際にテーブルを初期化する

手順

  1. npm init playwright@latest コマンドを実行する
  2. playwright のテストの実行されるディレクトリを e2e に設定する
    3. テストユーザーの設定を .env.localEMAILPASSWORD を設定する。
    EMAIL と PASSWORD は WIKI のローカル環境のユーザーのものを記載する
  3. テストユーザーの情報を login_then_logout.spec.ts 内に定義する

この動画を参考にしてテストを作成しました。

ログイン時

ログアウトのリンクが表示されているかチェックする。

ログアウト時

ログアウト後、リロードをしないと画面が更新されない
ログアウトボタンがクリックされて1.5秒待ちリロードする。
でないとリロードしてもログアウトの状態にならず flaky なテストとなる。
ログインのリンク、ユーザー登録のリンクが表示されているかチェックする。

Playwright の結果

Artifacts としてアップロードする

report

テスト環境では seed.test.sql を使用する
テスト前とテスト後に以下のコマンドを実行して seed.test.sql を取り込む

~~ ~~psql "postgres://postgres:postgres@localhost:54322/postgres" -f ~~~~./src/supabase/seed.test.sql~~ ~~

### E2Eテスト環境のためにユーザーを作成、テスト後にユーザーを削除する
createClient().auth.signUp でユーザーを新規登録後、 email confirmation が必要となりテストが失敗する。そのため createClient().auth.admin.createUser() でユーザーを新規登録、 email_confirm: true にする

Supawright を使用する

Supabase と Playwright を組み合わせた Supawright を使用してテスト用のユーザーをテスト前に作成、テスト後に作成したテストユーザーを自動的に削除してくれます。
README にある通りではうまく動かなかったので Issue で質問させて頂きました。

@kazuma-naka kazuma-naka self-assigned this Jan 5, 2025
Copy link

vercel bot commented Jan 5, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
agent ❌ Failed (Inspect) Feb 6, 2025 11:24pm

@kazuma-naka
Copy link
Contributor Author

@komagata さん、
はじめてのE2Eテストを作成しました🖊️
確認よろしくおねがいします🙇‍♂️

@komagata
Copy link
Member

komagata commented Jan 5, 2025

@kazuma-naka 僕はいい感じだと思います!
念の為フロントエンドのメンターの方々にもみてもらいますね。

@kazuma-naka
Copy link
Contributor Author

@kazuma-naka 僕はいい感じだと思います! 念の為フロントエンドのメンターの方々にもみてもらいますね。

承知しました!
よろしくおねがいします🙇‍♂️

package.json Outdated
@@ -14,6 +14,7 @@
"@supabase/supabase-js": "^2.47.3",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
"dotenv": "^16.4.7",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これだとインストールパッケージに dotenv が入りますが、
運用時に使わないのであれば、
代わりに devDependencies に dotenv-cli を入れて
scripts セクションに

"e2e": "dotenv -e .env.local -- playwright test",

で呼び出せると思います。
(スクリプト内の import も不要になる)

Copy link
Contributor Author

@kazuma-naka kazuma-naka Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@firewood さん、
レビューありがとうございます😊
dotenv を削除dotenv-cli を入れたのですが process.env.EMAIL が not defined となりテストを実行することができませんでした。具体的には
npm run e2e は実行できましたが Playwright Test for VSCode を使用した際にエラーが出て実行できませんでした。
dotenvdevDependencies に移動する対応を今回したのですが dotenv-cli を使用する方がよいでしょうか?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

なるほど、VSCodeから呼ぶときに困るんですね。
devDependencies + dotenv でも良いと思います!

Copy link

@firewood firewood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@komagata
Copy link
Member

komagata commented Jan 8, 2025

@kazuma-naka コンフリクトの修正をお願いします。

@kazuma-naka
Copy link
Contributor Author

@kazuma-naka コンフリクトの修正をお願いします。

@komagata さん、
コンフリクトを解消しました🖊

README.md Outdated
Comment on lines 32 to 33
EMAIL=Wikiのローカル環境ユーザーの Email を設定する
PASSWORD=Wikiのローカル環境ユーザーの Pass を設定する
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

開発環境のPASSWORDなので隠さないで大丈夫です〜

package.json Outdated
Comment on lines 13 to 14
"test": "true",
"e2e": "dotenv -e .env.local -- playwright test"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm run testでE2Eテストも動くようにお願いします。

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI環境でも動くことになるのでヘッドレスブラウザで動くようにお願いします。

@kazuma-naka
Copy link
Contributor Author

@komagata さん、

お疲れ様です。
Supawright を使用してテストユーザーを毎テスト前に作成、テスト後にテストユーザーを Supawright によって自動的に削除してもらう方向性で修正しました🖊
確認よろしくお願いします。

* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
console.log("Loaded ENV:", process.env);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) デバッグ用なら消してもよいかもです

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

削除しました!

@firewood
Copy link

firewood commented Feb 6, 2025

うまく動いているようです 👍

| { [key: string]: Json | undefined }
| Json[]

export type Database = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: モデルが二重管理になっていますが、このファイルって必要でしょうか?
src/lib/database.types をimportしてもテストは動作していそうです。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/libのほうをimportすると Type 'Database' does not satisfy the constraint 'GenericDatabase'. というエラーになるんですが、もしかして型定義が古いためですか? このE2Eのほうをsrc/lib/の方で使えばよさそう

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

レビューありがとうございます😊
Supawright を使用するために Supabase の型のタイプを変更する必要がありました。
src/lib/databaase.types.ts に変更しました。

https://github.com/isaacharrisholt/supawright#:~:text=%2D%20export%20interface%20Database%20%7B%0A%2B%20export%20type%20Database%20%3D%20%7B

不必要なログを削除
Copy link
Member

@komagata komagata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大変なIssueお疲れ様です。
確認させて頂きました。OKです〜🙆‍♂️

@komagata komagata merged commit fb0edc1 into main Feb 7, 2025
7 of 9 checks passed
@komagata komagata deleted the chore/create-first-e2e-test branch February 7, 2025 02:56
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

Successfully merging this pull request may close these issues.

3 participants