冗談の要約: いつ何が実行されるのか?
Jul 19, 2024 pm 03:59 PMTL;DR: 執行命令
- トップレベルおよびdescribe()ブロック内のすべてのもの(describe()ブロックは基本的にIIFEです)
-
beforeAll()
- トップレベル
- 第1レベルのdescribe()
- N レベルの description()
-
beforeEach()
- トップレベル
- 第1レベルのdescribe()
- N レベルの description()
- テスト()
-
afterEach()
- N レベルの description()
- 第1レベルのdescribe()
- トップレベル
-
結局()
- N レベルの description()
- 第1レベルのdescribe()
- トップレベル
免責事項
あなたは冗談と単体テストの基本を理解していると思います。フックの意味については説明しません。これはチートシート/参照タイプの投稿です。
ルールがあります
最初は、冗談は魔法のように物事を行うように見えます。何がいつ実行されるのか?しかし、少し考えてみれば、それは明らかであり、混乱は少なくなります。
これらの単純な「ルール」が役立つかもしれません:
- 各ファイルは独立して実行されます。A.test.js で何をしても B.test.js には影響しません。 (外部リソースへのアクセスを開始しない限り)
- description() コールバックはすぐに実行されます。
- フック (beforeAll/afterAll、beforeEach/afterEach) は、外側のスコープ (トップレベル/モジュール) から内側のスコープ (describe) まで実行されます。
基本的な例
console.log("./<start>"); beforeAll(() => { console.log("./beforeAll"); }) beforeEach(() => { console.log("./beforeEach"); }) afterAll(() => { console.log("./afterAll"); }) afterEach(() => { console.log("./afterEach"); }) describe("foo", () => { console.log("./describe(foo)/<start>"); beforeAll(() => { console.log("./describe(foo)/beforeAll"); }) beforeEach(() => { console.log("./describe(foo)/beforeEach"); }) afterAll(() => { console.log("./describe(foo)/afterAll"); }) afterEach(() => { console.log("./describe(foo)/afterEach"); }) test("testFoo", () => { console.log("./describe(foo)/test(testFoo)"); }) console.log("./describe(foo)/<end>"); }) describe("bar", () => { console.log("./describe(bar)/<start>"); beforeAll(() => { console.log("./describe(bar)/beforeAll"); }) beforeEach(() => { console.log("./describe(bar)/beforeEach"); }) afterAll(() => { console.log("./describe(bar)/afterAll"); }) afterEach(() => { console.log("./describe(bar)/afterEach"); }) test("testBar", () => { console.log("./describe(bar)/test(testBar)"); }) test("testOtherBar", () => { console.log("./describe(bar)/test(testOtherBar)"); }) console.log("./describe(bar)/<end>"); }) console.log("./<end>");
ログイン後にコピー
これは結果です (他の出力を削除した後):
./<start> ./describe(foo)/<start> ./describe(foo)/<end> ./describe(bar)/<start> ./describe(bar)/<end> ./<end> ./beforeAll ./describe(foo)/beforeAll ./beforeEach ./describe(foo)/beforeEach ./describe(foo)/test(testFoo) ./describe(foo)/afterEach ./afterEach ./describe(foo)/afterAll ./describe(bar)/beforeAll ./beforeEach ./describe(bar)/beforeEach ./describe(bar)/test(testBar) ./describe(bar)/afterEach ./afterEach ./beforeEach ./describe(bar)/beforeEach ./describe(bar)/test(testOtherBar) ./describe(bar)/afterEach ./afterEach ./describe(bar)/afterAll ./afterAll
ログイン後にコピー
何が起こるのですか?
トップレベルと記述コールバック内のすべてがすぐに実行されます:
./<start> ./describe(foo)/<start> ./describe(foo)/<end> ./describe(bar)/<start> ./describe(bar)/<end> ./<end> [...]
ログイン後にコピー
beforeAll と afterAll は、すべてのテストを囲む「中括弧」です。それぞれは 1 回だけ実行されます。
[...] ./beforeAll [...] ./afterAll
ログイン後にコピー
./describe(*)/beforeAll および ./describe(*)/afterAll は、コールバックを記述する that 内のすべてのテストを囲む中括弧です。それぞれは 1 回だけ実行されます。
[...] ./describe(foo)/beforeAll [...] ./describe(foo)/afterAll ./describe(bar)/beforeAll [...] ./describe(bar)/afterAll [...]
ログイン後にコピー
beforeEach と afterEach は、各テストを囲む中括弧です。最上位は外側中括弧です。記述レベルは 内側 中括弧です。
[...] ./beforeEach ./describe(*)/beforeEach [...] ./describe(*)/afterEach ./afterEach [...]
ログイン後にコピー
アドバンストバージョン
これは、ネストされた記述ブロックを使用した高度な例です。実行ステップの階層的な性質を強調するために、XML 風の結果が生成されます。
console.log("<top-level>"); beforeAll(() => { console.log("<all>"); }) beforeEach(() => { console.log("<each>"); }) afterAll(() => { console.log("</all>"); }) afterEach(() => { console.log("</each>"); }) describe("foo", () => { console.log("<describe id=\"foo\">"); beforeAll(() => { console.log("<all in=\"foo\">"); }) beforeEach(() => { console.log("<each in=\"foo\">"); }) afterAll(() => { console.log("</all> <!-- in=\"foo\" -->"); }) afterEach(() => { console.log("</each> <!-- in=\"foo\" -->"); }) test("testFoo", () => { console.log("<test in=\"foo\" id=\"testFoo\" />"); }) console.log("</describe> <!-- id=\"foo\" -->"); }) describe("bar", () => { describe("barinner", () => { console.log("<describe id=\"barinner\">"); beforeAll(() => { console.log("<all in=\"barinner\">"); }) beforeEach(() => { console.log("<each in=\"barinner\">"); }) afterAll(() => { console.log("</all> <!-- in=\"barinner\" -->"); }) afterEach(() => { console.log("</each> <!-- in=\"barinner\" -->"); }) test("testBarInner", () => { console.log("<test in=\"barinner\" id=\"testBarInner\" />"); }) console.log("</describe> <!-- id=\"barinner\" -->"); }) console.log("<describe id=\"bar\">"); beforeAll(() => { console.log("<all in=\"bar\">"); }) beforeEach(() => { console.log("<each in=\"bar\">"); }) afterAll(() => { console.log("</all> <!-- in=\"bar\" -->"); }) afterEach(() => { console.log("</each> <!-- in=\"bar\" -->"); }) test("testBar", () => { console.log("<test in=\"bar\" id=\"testBar\" />"); }) test("testOtherBar", () => { console.log("<test in=\"bar\" id=\"testOtherBar\" />"); }) console.log("</describe> <!-- id=\"bar\" -->"); }) console.log("</top-level>");
ログイン後にコピー
これはクリーンアップとフォーマット後の出力です:
<top-level> <describe id="foo"> </describe> <!-- id="foo" --> <describe id="barinner"> </describe> <!-- id="barinner" --> <describe id="bar"> </describe> <!-- id="bar" --> </top-level> <all> <all in="foo"> <each> <each in="foo"> <test in="foo" id="testFoo" /> </each> <!-- in="foo" --> </each> </all> <!-- in="foo" --> <all in="bar"> <all in="barinner"> <each> <each in="bar"> <each in="barinner"> <test in="barinner" id="testBarInner" /> </each> <!-- in="barinner" --> </each> <!-- in="bar" --> </each> </all> <!-- in="barinner" --> <each> <each in="bar"> <test in="bar" id="testBar" /> </each> <!-- in="bar" --> </each> <each> <each in="bar"> <test in="bar" id="otherBar" /> </each> <!-- in="bar" --> </each> </all> <!-- in="bar" --> </all>
ログイン後にコピー
実行命令について知っておくべきことは以上です。
以上が冗談の要約: いつ何が実行されるのか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

人気の記事
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
レポ:チームメイトを復活させる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
1週間前
By 尊渡假赌尊渡假赌尊渡假赌

人気の記事
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
レポ:チームメイトを復活させる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
1週間前
By 尊渡假赌尊渡假赌尊渡假赌

ホットな記事タグ

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7134
9


Java チュートリアル
1534
14


Laravel チュートリアル
1257
25


PHP チュートリアル
1205
29


CakePHP チュートリアル
1154
46

