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

O.O*_*O.O 1 javascript unit-testing node.js ava mjs

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

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

我认为脚本没有正确转换,或者我在我的 package.json

我感谢任何有使用 AVA 经验的人的任何帮助 --experimental-modules

包.json

{
    "scripts": {
        "test": "ava --init"
    },
    "ava": {
        "require": [
      "esm"
    ],
        "babel": false,
        "extensions": [
      "mjs"
    ]
    }
}
Run Code Online (Sandbox Code Playgroud)

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]]);
});
Run Code Online (Sandbox Code Playgroud)

索引.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;
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

Mar*_*ben 6

AVA 不支持.mjs开箱即用,但看起来您已经弄清楚了配置。

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

综上所述,测试失败是因为您使用了错误的断言。t.is(actual, expected)使用Object.is(actual, expected)(几乎是actual === expected)。你不能这样比较数组。

使用t.deepEqual()来代替。