导入到test.js时,AVA测试给出了undefined

时间:2017-01-30 06:35:07

标签: javascript node.js testing ava

我使用AVA作为节点和javascript的测试。

on test.js

import test from 'ava';
import {valid, output, input} from './dependency.js';

test("Input is not a Empty String", t => {
    t.not(input, ''); t.pass();
})

test("Correct output", t => {
    var testInput = ['KittenService: CameCaser', 'CamelCaser: '];
    var expected = 'CamelCaser, KittenService';
    var actual = output;
    t.deepEqual(actual, expected, "Result did match");
})

在第一次测试时,即使我的

也会通过
  

var input ='';

同样在我的第二次测试中它会抛出:

t.deepEqual(actual, expected, "Result did match")
              |       |
              |       "CamelCaser, KittenService"
              undefined

on dependency.js

module.exports = {valid, input, output};
var input = '';
var output = [];

我确实在函数之后有输出值,但它似乎在test.js上它没有从依赖性测试中获取输入或输出值。 我不确定如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

AVA使用Babel编译import语句。由于dependency.js不是使用Babel创建的,因此导入时module.exports对象会被视为默认导出。

请改为:

import test from 'ava';
import dependency from './dependency.js';

const {valid, output, input} = dependency;

test("Input is not a Empty String", t => {
    t.not(input, '');
})

test("Correct output", t => {
    var testInput = ['KittenService: CameCaser', 'CamelCaser: '];
    var expected = 'CamelCaser, KittenService';
    var actual = output;
    t.deepEqual(actual, expected, "Result did match");
})

P.S。您通常不需要t.pass()