测试返回不同对象的普通javascript文件

时间:2017-11-21 21:55:35

标签: javascript unit-testing es6-modules jasmine2.0

是否可以使用Jasmine测试工具或任何其他npm模块(如rewire或类似模板)测试下面的代码?

const AuthValidatorDumb = require('./src/AuthValidatorDumb');
const AuthValidator = require('./src/AuthValidator');
const config = require('../config');

let instance;

if (!instance) {
  if (config.get('auth.enabled')) {
    instance = AuthValidator;
  } else {
    instance = AuthValidatorDumb;
  }
}

module.exports = instance;

1 个答案:

答案 0 :(得分:0)

我有一个用于测试上述代码的变体。假设你有:

1)上述问题中index.js的代码。

2) AuthValidator.js

$createdTime = Carbon::parse($specialist->created_at)
                        ->diff(Carbon::now());

$final_string = '';
$year = '';
$month = '';
$days = '';

if ($createdTime->y) {
    $year = $createdTime->y . ' yr';
}

if ($createdTime->m) {
    $month = ' '. $createdTime->m . ' mo';
}

if ($createdTime->d) {
    $days = ' ' . $createdTime->d . ' days';
}

$final_string = trim($year . $month . $days);

3) AuthValidatorDumb.js

class AuthValidator {}
module.exports = AuthValidator;

这是 test / index.spec.js

class AuthValidatorDumb {}
module.exports = AuthValidatorDumb;