AVA单元测试未通过Javascript测试规范

时间:2019-06-15 14:36:27

标签: javascript node.js unit-testing ava mjs

我目前正在使用扩展为.mjs的ES6模块,并为某些功能创建测试用例。

我之所以选择AVA是因为它支持这种扩展类型,但是测试执行未按预期运行。

我认为脚本未正确转换 要么 我的package.json

中缺少配置

我非常感谢经验丰富的--experimental-modules

人士提供的任何帮助。

package.json

{
    "scripts": {
        "test": "ava --init"
    },
    "ava": {
        "require": [
      "esm"
    ],
        "babel": false,
        "extensions": [
      "mjs"
    ]
    }
}

test.spec.mjs

import rotate from './index.mjs'
import test from 'ava';

test('rotate img', t => {
    var m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
    rotate(m);

    t.is(m, [[7, 4, 1], [8, 5, 2], [9, 6, 3]]);
});

index.js

 var rotate =function(matrix) {
    let cols = 0,
     original = JSON.parse(JSON.stringify(matrix));

    for (let i=0; i < matrix.length; i++){
        for (let j = matrix.length; j > 0; j--){
            matrix[i][cols]=original[j-1][i]; 

            cols+=1;
            if(cols == matrix.length){
                cols= 0;
            }
        }
    }
}
export default rotate;

在运行npm test时,如程序包脚本中所定义

  

错误:

 1 test failed

  rotate

   12: rotate(m);
   13:     t.is(m, [[7,4,1],[8,5,2],[9,6,3]
   14: ]);

  Values are deeply equal to each other, but they are not the same:

  [[7,4,1,],[8,5,2,],[9,6,3,],] <<fails

npm ERR! Test failed.  See above for more details.

1 个答案:

答案 0 :(得分:1)

AVA不支持开箱即用的.mjs,但看起来您已经确定了配置。

对于test脚本,只需使用ava,而不使用--init

所有这些,由于您使用了错误的断言,因此测试失败。 t.is(actual, expected)使用Object.is(actual, expected)(几乎是actual === expected)。而且你不能像那样比较数组。

改为使用t.deepEqual()