【问题标题】:array.sort behaving strangely with wallaby.jsarray.sort 与 wallaby.js 的行为异常
【发布时间】:2016-07-01 14:34:26
【问题描述】:

我有一个构造像[{index: 1}, {index: 4}, {index: 7}] 这样的数组的函数。该数组按对象的索引值排序。我已将函数的范围缩小到仅对数组进行排序,而 wallaby 指示数组的顺序不正确,但 mocha 继续指示通过测试。

规格是:

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);
        });
    });
});

sort.js的实现是:

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

我的小袋鼠配置如下:

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();
    }
  };
};

【问题讨论】:

    标签: javascript wallaby.js


    【解决方案1】:

    Wallaby.js 默认在后台使用 PhantomJs,它使用与 Safari 相同的 JavaScript 引擎。如果你在 Safari Dev Tools 中运行这个 sn-p:

    您会注意到它也没有按预期对数组进行排序。 Chrome 开发工具会显示不同的结果:

    因此,如果您希望您的 sort 实现在所有平台上工作,您需要将其更改为 compliant with the spec 并返回 1-10 ,而不仅仅是truefalse

    所以如果你用这种方式重写你的排序函数:

    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;
      });
    

    那么它将在任何地方工作。如果您更喜欢较短(但可读性较差)的方式,您可以使用以下方式:

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

    如果出于某种原因您只想支持您的原始代码工作的平台并且不想更改它,那么我建议将默认 PhantomJs 运行器切换到小袋鼠也支持的Electron runner。它使用 V8,您的原始代码可以正常使用。

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 2012-05-05
      相关资源
      最近更新 更多