0

yargs を使用して構築された基本的な CLI プログラムがあります。アプリケーションでエクスポートされた関数のテスト ケースをカバーできます。

以下に示すように、テスト カバレッジは行12-18which からは行われません。のようなサードパーティ パッケージの単体テスト カバレッジをどのように記述しますyargsか?

index.js

const yargs = require('yargs');
const { hideBin } = require('yargs/helpers');
const greet = (name) => {
  return `Welcome ${name}`;
};
yargs(hideBin(process.argv)).command(
  'run [name]',
  'print name',
  (yargs) => {
    yargs.positional('name', { describe: 'Your name', type: 'string' });
  },
  (args) => {
    const { name } = args;

    const greetMsg = greet(name);

    console.log(greetMsg);
  }
).argv;

module.exports = { greet };

index.test.js

const { greet } = require('./index')

describe.only('greeting', () => {
  it('greet', async () => {
    const greetMsg = greet('test')

    expect(greetMsg).toBe('Welcome test')
  })
})

テスト範囲

PASS  ./index.test.js
greeting
  ✓ greet (5 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |   63.64 |      100 |   33.33 |   63.64 |                   
index.js  |   63.64 |      100 |   33.33 |   63.64 | 12-18            
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        1.316 s, estimated 2 s
Ran all test suites.
4

1 に答える 1