Qunit-nested-modules
提供:Dev Guides
QUnit-ネストされたモジュール
グループ化されたテスト関数を持つモジュールは、ネストされたモジュールを定義するために使用されます。 QUnitは、最初に宣言されている場合でも、ネストされたモジュールを深く調べる前に、親モジュールでテストを実行します。 ネストされたモジュール呼び出しの beforeEach および afterEach コールバックは、LIFO(Last In、First Out)モードで親フックにスタックします。 引数とフックを使用して、各テストの前後に実行するコードを指定できます。
フックを使用して、各テストのコンテキストで共有されるプロパティを作成することもできます。 フックオブジェクトの追加プロパティは、そのコンテキストに追加されます。 コールバック引数を指定してQUnit.moduleを呼び出す場合、フック引数はオプションです。
コンテキストをテスト環境として使用して、モジュールのコールバックが呼び出され、環境のプロパティがモジュールのテスト、フック、ネストされたモジュールにコピーされます。
<html>
<head>
<meta charset = "utf-8">
<title>QUnit basic example</title>
<link rel = "stylesheet" href = "https://code.jquery.com/qunit/qunit-1.22.0.css">
<script src = "https://code.jquery.com/qunit/qunit-1.22.0.js"></script>
</head>
<body>
<div id = "qunit"></div>
<div id = "qunit-fixture"></div>
<script>
QUnit.module( "parent module", function( hooks ) {
hooks.beforeEach( function( assert ) {
assert.ok( true, "beforeEach called" );
});
hooks.afterEach( function( assert ) {
assert.ok( true, "afterEach called" );
});
QUnit.test( "hook test 1", function( assert ) {
assert.expect( 2 );
});
QUnit.module( "nested hook module", function( hooks ) {
//This will run after the parent module's beforeEach hook
hooks.beforeEach( function( assert ) {
assert.ok( true, "nested beforeEach called" );
});
//This will run before the parent module's afterEach
hooks.afterEach( function( assert ) {
assert.ok( true, "nested afterEach called" );
});
QUnit.test( "hook test 2", function( assert ) {
assert.expect( 4 );
});
});
});
</script>
<div id = "console" ></div>
</body>
</html>
出力を確認する
次の結果が表示されます-