LoginSignup
1
1

More than 5 years have passed since last update.

power-assert をインストールするときはテストコードの配置場所に注意しよう

Last updated at Posted at 2019-02-11

はじめに

power-assert1 は便利なライブラリですが、インストールでハマったのでその記録と対処について書きます。

エラーの表示が変わらない。。

筆者の環境だと、公式ページの説明の通りインストールしても、エラーの表示が Node.js 標準の assert から変わりませんでした。

package.json
{
  "name": "power-assert-sample",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "mocha --require intelli-espower-loader tests/*.js"
  },
  "devDependencies": {
    "intelli-espower-loader": "^1.0.1",
    "mocha": "^5.2.0",
    "power-assert": "^1.6.1"
  },
  "dependencies": {
  }
}
src/hello.js
module.exports = (person) => {
  return `Hello, ${person}!`;
}
tests/helloTest.js
const assert = require('power-assert');
const hello = require('../src/hello.js');

describe('hello test suit', () => {
  it('greet Taro', () => {
    assert(hello('Taro') === 'Hello, Taro!');
  });
});

テスト実行

yarn test
  1) hello test suit
       greet Taro:
     AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value:

  func.apply(thisObj, args)

原因

power-assert を使うとき、テストコードは test に置くことを想定していますが、私のリソースでは tests/ においていました。

テストのディレクトリが test と異なる場合は package.json に明示的に指定するとのこと。
内部で構文解析をしているので、テストコードの場所を知っている必要があるんですね。

このことは、 power-assert でなく、 intelli-espower-loader の README2に説明がありました。

The default folder is "test/". You must put your test script in this folder.
If you don't put your test code in the right folder, intelli-espower-loader will work incorrectly.
You can change test folder setting in your package.json

package.jsonを以下を追加したらうまく動きました。

...
  "directories": {
    "test": "tests"
  },
...
表示結果
  assert(hello('John Smith').match(/^Hello, Taro/))
         |                   |
         |                   null
         "Hello, John Smith!"

参考

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1