简体   繁体   English

array.sort 与 wallaby.js 的行为异常

[英]array.sort behaving strangely with wallaby.js

I have a function that constructs an array like, [{index: 1}, {index: 4}, {index: 7}] .我有一个构造数组的函数,如[{index: 1}, {index: 4}, {index: 7}] The array is ordered by the objects' index value.该数组按对象的索引值排序。 I have narrowed the scope of the function to just a sorting of an array and wallaby indicates the order of the array is incorrect, however mocha continues to indicate passing tests.我已将函数的范围缩小到仅对数组进行排序,并且小袋鼠表示数组的顺序不正确,但是 mocha 继续表示通过了测试。

The spec is:规格是:

import expect from 'expect';
import sort from './sort';

describe("Given an array", ()=> {
    let array;
    beforeEach(() => {
        array = [
            { index: 7, owner: 1 },
            { index: 2, owner: 1 },
            { index: 3, owner: 1 },
            { index: 5, owner: 1 },
            { index: 1, owner: 1 }
        ];
    });

    describe("When sorting the array of elements by id", () => {

        let actual;
        beforeEach(() => {
            actual = sort(array);
        });

        it('should order the array of objects by ascending id',()=> {
            let expected = [
                { index: 1, owner: 1 },
                { index: 2, owner: 1 },
                { index: 3, owner: 1 },
                { index: 5, owner: 1 },
                { index: 7, owner: 1 }
            ];

            expect(actual).toEqual(expected);
        });
    });
});

The implementation of sort.js is: sort.js 的实现是:

export default function(array){
   return array.sort((x, y) => { return x.index > y.index});
}

My wallaby config looks like:我的小袋鼠配置如下:

process.env.NODE_ENV = 'test';

var wallabyWebpack = require('wallaby-webpack');
var packageConfig = require('./package.json');

module.exports = function(wallaby) {

  var specFilePattern = 'src/shared/**/*.spec.js';
  var srcFilePattern = 'src/shared/**/*.js*';

  var babelProcessor = wallaby.compilers.babel(packageConfig['babel']);

  var webpackPostProcessor = wallabyWebpack({
    resolve: {
          extensions: ['', '.js', '.jsx']
      }
  });

  return {
    testFramework: 'mocha',
    debug: true,
    files: [
      { pattern: 'node_modules/babel-polyfill/dist/polyfill.js', instrument: false },
      { pattern: srcFilePattern, load: false },
      { pattern: specFilePattern, ignore: true }
    ],
    tests: [
      { pattern: specFilePattern, load: false }
    ],
    compilers: {
      '**/*.js*': babelProcessor
    },
    postprocessor: webpackPostProcessor,
    bootstrap: function(){
      window.__moduleBundler.loadTests();
    }
  };
};

Wallaby.js is using PhantomJs behind the scenes by default, which uses the same JavaScript engine that Safari does. Wallaby.js 默认在幕后使用 PhantomJs,它使用与 Safari 相同的 JavaScript 引擎。 And if you run this snippet in Safari Dev Tools :如果您在Safari Dev Tools 中运行此代码段:

屏幕截图 2016-03-22 at 12 42 21 pm

you'll notice that it also doesn't sort the array as expected.您会注意到它也没有按预期对数组进行排序。 Chrome Dev Tools will show you a different result: Chrome Dev Tools会显示不同的结果:

屏幕截图 2016-03-22 at 12 42 43 pm

So if you'd like your sort implementation to work on all platforms , you need to change it to be compliant with the spec and return 1 , -1 or 0 , as opposed to just true or false .因此,如果您希望sort实现适用于所有平台,则需要对其进行更改以符合规范并返回1-10 ,而不仅仅是truefalse

So if you rewrite your sort function this way:因此,如果您以这种方式重写排序函数:

export default function (array) {
  return array.sort((x, y) => {
    if (x.index > y.index) {
      return 1;
    }
    if (x.index < y.index) {
      return -1;
    }

    return 0;
  });

then it will work everywhere.那么它会在任何地方工作。 If you'd prefer a shorter (but a bit less readable) way, you may use this one:如果您更喜欢更短(但可读性稍差)的方式,您可以使用以下方法:

export default function(array) {
  return array.sort((x, y) => +(x.index > y.index) || +(x.index === y.index) - 1);
}

If for some reason you would only like to support platforms where your original code works and wouldn't like to change it, then I suggest switching the default PhantomJs runner to the Electron runner that wallaby also supports.如果出于某种原因,您只想支持原始代码可以工作的平台而不想更改它,那么我建议将默认的 PhantomJs 运行器切换到小袋鼠也支持的Electron 运行器 It uses V8 and your original code works fine with it.它使用 V8,您的原始代码可以很好地使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM