在邮政编码插件的开玩笑单元测试中测试平等的正确方法



我正在编写一个Postcss插件来支持CSS中的嵌套导入语句,并且我遇到了这个问题,在此问题中,我的单位测试返回的输出略有不同,从而导致测试。失败:

var postcss = require('postcss');
var plugin = require('./');
function run(input, output, opts) {
  return postcss([ plugin(opts) ]).process(input)
    .then(result => {
      expect(result.css).toEqual(output);
      expect(result.warnings().length).toBe(0);
  });
}

it('replaces @import with the contents of the file being imported.', () => {
  return run('@import "./vendor.css";',
             ".vendor { background: silver; }");
});

./vendor.css看起来像这样:

.vendor { background: silver; }

导致:

FAIL  ./index.test.js
replaces @import with the contents of the file being imported.
expect(received).toEqual(expected)
Expected value to equal:
  ".vendor { background: silver; }"
Received:
  ".vendor {
    background: silver
}"

这是插件代码:https://github.com/eriklharper/postcss-nested-import/blob/master/master/index.js#l36-l57

知道我做错了什么?我无法判断这是Postcss还是Jest的问题,还是两者?

Postcss也没有开玩笑!

没有问题!

PostCSS正在返回带有缩进CSS的字符串,而JEST将其与单行声明进行比较。结果是比较2个不同的字符串。

幸运的是,您可以以无尽的不同方式修复它。

最微不足道的可能是在比较之前缩小实际和预期结果。

如果您想为项目凉爽的东西,您可以扩展jest默认匹配器,以比较不同的格式化CSS字符串。

最新更新