Node.jsでJasmineを使用する
jasmine
モジュールは、Node.js環境でJasmineスペックを実行するためのコマンドラインインターフェースとサポートコードです。 Jasmine 5.xは、Node.jsバージョン18、20、および22をサポートしています。(奇数番号のNode.jsバージョンはサポートされていませんが、多くのバージョンで動作します。)
インストール
npmを使用して、プロジェクトにJasmineをローカルインストールできます。
npm install --save-dev jasmine
上記のローカルインストールでは、npx jasmine ...
コマンドを使用してCLIツールを呼び出すことができます。
オプションとして、npx
なしでCLIツールを呼び出すことができるように、jasmineをグローバルにインストールすることもできます。 ただし、グローバルにインストールされた jasmine
のバージョンを、それを使用する各プロジェクトと同期させるのが難しいため、これは推奨されません。
npm install -g jasmine
プロジェクトの初期化
specディレクトリと設定用のJSONファイルを作成して、Jasmineのプロジェクトを初期化します。
npx jasmine init
サンプルの生成
サンプルのspecファイルとソースファイルを生成します。
npx jasmine examples
この時点で、最初のテストスイートを作成できるはずです。
設定
spec/support/jasmine.json
をカスタマイズして、Jasmineランナーに含めるソースファイルとspecファイルを列挙します。 ディレクトリglob文字列を使用できます。
!
で始まるパスは除外されます。たとえば、!**/*nospec.js
です。
spec_dir
は、すべての spec_files
と helpers
のプレフィックスとして使用されます。 ヘルパーは、すべてのスペックの前に一度実行されます。 ヘルパーの例については、Reactチュートリアルを参照してください。
{
// Spec directory path relative to the current working dir when jasmine is executed.
// The value "" represents the current working directory.
"spec_dir": "spec",
// Array of filepaths (and globs) relative to spec_dir to include and exclude
"spec_files": [
"**/*[sS]pec.?(m)js",
"!**/*nospec.js"
],
// Array of filepaths (and globs) relative to spec_dir to include before jasmine specs
"helpers": [
"helpers/**/*.?(m)js"
],
// Configuration of the Jasmine environment
// "env" is optional, as are all of its properties.
"env": {
// Whether to fail a spec that ran no expectations
"failSpecWithNoExpectations": false,
// Stop execution of a spec after the first expectation failure in it
"stopSpecOnExpectationFailure": false,
// Stop execution of the suite after the first spec failure
"stopOnSpecFailure": false,
// Run specs in semi-random order
"random": false
}
}
--config
コマンドライン引数または JASMINE_CONFIG_PATH
環境変数を使用して、別の設定ファイルを指定することもできます。 設定ファイルは、.json
または .js
のいずれかです。 .js
設定ファイルは、デフォルトエクスポートが設定オブジェクトであるモジュールである必要があります。
jasmine JASMINE_CONFIG_PATH=relative/path/to/your/jasmine.json
jasmine --config=relative/path/to/your/jasmine.json
スペックの実行
jasmine.json
を設定したら、プロジェクトのルートから jasmine
を実行することで、すべてのスペックを実行できます(ローカルにインストールした場合は npx jasmine
)。
1つのspecのみ、または特定のglobパターンに一致するファイル内のspecのみを実行する場合は、次のように実行できます。
npx jasmine spec/appSpec.js
npx jasmine "**/model/**/critical/**/*Spec.js"
スペックのフィルタリング
ファイル名が指定されたglobに一致するspecのみを実行します。
npx jasmine "spec/**/critical/*Spec.js"
または、単一のファイル。
npx jasmine spec/currentSpec.js
または、名前が特定の正規表現に一致するspecのみを実行します。
npx jasmine --filter "adapter21*"
(specの*名前*は、describe()
に渡される最初のパラメーターです)
ESモジュールの使用
Jasmineは動的インポートを使用してコードを読み込みます。これは、ESモジュールとCommonJSモジュールの両方と互換性があるはずです。 つまり、スクリプトの名前が .mjs
で終わるか、ファイルを含むパッケージの package.json
に "type": "module"
が含まれている場合、スクリプトはESモジュールとしてロードされます。
デフォルト設定は、ほぼすべてのCommonJSプロジェクトとESモジュールを使用するプロジェクトで正常に機能するはずです。 ただし、必要に応じて、Jasmine設定ファイルに "jsLoader": "require"
を追加することで、require
を使用してスクリプトを読み込むようにJasmineを設定できます。 "jsLoader": "require"
を使用しないと動作しないコードがある場合は、お知らせください。 .mjs
で終わる名前のファイルは、jsLoader
が "require"
に設定されている場合でも、動的インポートを介してロードされます。
CLIオプション
JASMINE_CONFIG_PATH=
設定ファイルへの相対パスまたは絶対パスを指定します。 オプションとして使用するか、環境変数として設定できます。
JASMINE_CONFIG_PATH=spec/config/jasmine.json jasmine
npx jasmine --config=spec/config/jasmine.json
--no-color
spec出力の色をオフにします。
npx jasmine --no-color
--filter=
指定された文字列に一致するspecのみを実行します。
npx jasmine --filter="a spec name"
--fail-fast
最初の期待値の失敗またはその他のエラーの後、スイートの実行を停止します。
npx jasmine --fail-fast=true
--random=[true|false]
jasmine.json
をオーバーライドして、この実行でspecをセミランダムな順序で実行するかどうかをjasmineに指示します。
npx jasmine --random=true
--seed=
ランダム化がオンになっている場合、ランダム化シードを設定します。
npx jasmine --seed=4321
--reporter=
デフォルトのレポーターを設定します。 値は、デフォルトエクスポートがレポーターコンストラクターであるモジュールへの有効なインポート指定子である必要があります。
npm i --save-dev jasmine-ts-console-reporter
npx jasmine --reporter=jasmine-ts-console-reporter
ライブラリの使用
設定をよりきめ細かく制御したい場合は、Jasmineをプロジェクトのライブラリとして使用することもできます。 これにより、複数の設定ファイルを読み込んだり、設定をさまざまな方法で制御したりできます。
const Jasmine = require('jasmine');
const jasmine = new Jasmine();
ファイルまたはオブジェクトから設定を読み込みます。
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.loadConfig({
spec_dir: 'spec',
spec_files: [
'appSpec.js',
'requests/**/*[sS]pec.js',
'utils/**/*[sS]pec.js'
],
helpers: [
'helpers/**/*.js'
]
});
カスタム完了ハンドラー
デフォルトでは、Jasmineはスイートの実行が完了するとNode.jsプロセスを終了させます。 終了コードは、スイートの全体的なステータスが 'passed'
の場合は0になり、それ以外の場合は0以外になります。 完了を別の方法で処理する場合は、Jasmineインスタンスの exitOnCompletion
プロパティを false
に設定し、execute
から返されたpromiseを使用します。 これは、gruntなどのタスクランナーにステータスをメッセージで伝えるためによく使用されます。
jasmine.exitOnCompletion = false;
const result = await jasmine.execute();
if (result.overallStatus === 'passed') {
console.log('All specs have passed');
} else {
console.log('At least one spec has failed');
}
レポーター
他のレポーターが追加されていない場合は、ConsoleReporterが含まれています。 configureDefaultReporter
を使用してデフォルトのレポーターを設定できます。 デフォルト値は例に示されています。
jasmine.configureDefaultReporter({
// The `timer` passed to the reporter will determine the mechanism for seeing how long the suite takes to run.
timer: new jasmine.jasmine.Timer(),
// The `print` function passed the reporter will be called to print its results.
print: function() {
process.stdout.write(arguments);
},
// `showColors` determines whether or not the reporter should use ANSI color codes.
showColors: true
});
addReporter
を使用してカスタムレポーターを追加できます。 addReporter
を介してレポーターを追加すると、デフォルトのConsoleReporterは追加されません。 複数のレポーターを追加できます。
const CustomReporter = require('./myCustomReporter');
jasmine.addReporter(new CustomReporter());
テストを実行する
execute
を呼び出すと、specが実行されます。
jasmine.execute();
execute
は、オプションで、現在の作業ディレクトリを基準としたspecファイルパスのリストと、spec名でフィルタリングするための文字列を指定して呼び出すことができます。
jasmine.execute(['fooSpec.js'], 'a spec name');
ライブラリを使用した簡単な例
const Jasmine = require('jasmine');
const jasmine = new Jasmine();
jasmine.loadConfigFile('spec/support/jasmine.json');
jasmine.configureDefaultReporter({
showColors: false
});
jasmine.execute();