Jestで標準出力(console.xxx())を非表示にする

2021.11.26

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

Jestは、JavaScriptのテスティングフレームワークです。Facebookが開発しています。

今回は、Jestで標準出力(console.xxx())を非表示にする方法を確認しました。

さきに結論

jestのコマンドで--silentオプションを使用すれば非表示にできます。

Jestのテスト実行で標準出力が煩わしい

処理のロジックとしては必須ではないですが、デバッグ時のためにconsole.log()などの標準出力をコードに仕込んでおく場合があります。

src/lambda/handlers/sampleHandler.ts

export const handler = (added: number): number => {
  console.log(added);
  console.info('hoge message');
  console.error({ message: ' erroe' });

  return 1 + added;
};

上記のコードを対象にしたテストコードです。

test/sampleHandler.test.ts

import { handler } from '../src/lambda/handlers/sampleHandler';

test('handler', async (): Promise<void> => {
  const response = handler(2);

  expect(response).toBe(3);
});

jestによるテストを実行すると、既定では出力結果に標準出力が表示され、結果の確認の妨げになってしまいます。

$  npx jest
 PASS  test/sampleHandler.test.ts
  ✓ handler (18 ms)

  console.log
    2

      at Object.<anonymous>.exports.handler (src/lambda/handlers/sampleHandler.ts:2:11)

  console.info
    hoge message

      at Object.<anonymous>.exports.handler (src/lambda/handlers/sampleHandler.ts:3:11)

  console.error
    { message: ' erroe' }

      2 |   console.log(added);
      3 |   console.info('hoge message');
    > 4 |   console.error({ message: ' erroe' });
        |           ^
      5 |
      6 |   return 1 + added;
      7 | };

      at Object.<anonymous>.exports.handler (src/lambda/handlers/sampleHandler.ts:4:11)
      at Object.<anonymous> (test/sampleHandler.test.ts:4:20)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        2.836 s, estimated 6 s
Ran all test suites.

標準出力を非表示にする

jestのコマンドで--silentオプションを使用します。

$ npx jest --silent
 PASS  test/sampleHandler.test.ts

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.84 s, estimated 3 s

console.log()info()error()がいずれも非表示にできていますね。

--silent trueでも非表示にできます。

$ npx jest --silent true
 PASS  test/sampleHandler.test.ts

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.816 s, estimated 1 s

--silent falseだと表示されます。

$  npx jest --silent false
 PASS  test/sampleHandler.test.ts
  ✓ handler (19 ms)

  console.log
    2

      at Object.<anonymous>.exports.handler (src/lambda/handlers/sampleHandler.ts:2:11)

  console.info
    hoge message

      at Object.<anonymous>.exports.handler (src/lambda/handlers/sampleHandler.ts:3:11)

  console.error
    { message: ' erroe' }

      2 |   console.log(added);
      3 |   console.info('hoge message');
    > 4 |   console.error({ message: ' erroe' });
        |           ^
      5 |
      6 |   return 1 + added;
      7 | };

      at Object.<anonymous>.exports.handler (src/lambda/handlers/sampleHandler.ts:4:11)
      at Object.<anonymous> (test/sampleHandler.test.ts:4:20)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.79 s, estimated 1 s
Ran all test suites

参考

以上