Jestjs 测试POSTSS插件的Jest单元测试中的相等性的正确方法

Jestjs 测试POSTSS插件的Jest单元测试中的相等性的正确方法,jestjs,postcss,Jestjs,Postcss,我正在编写一个POSTSS插件来支持CSS中的嵌套导入语句,我遇到了这个问题,单元测试返回的输出与我预期的略有不同,导致测试失败: var postcss = require('postcss'); var plugin = require('./'); function run(input, output, opts) { return postcss([ plugin(opts) ]).process(input) .then(result => { exp

我正在编写一个POSTSS插件来支持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
}"
以下是插件代码:


知道我做错了什么吗?我不知道这是一个与PostSS或Jest有关的问题,还是两者都有?

PostSS或Jest中都没有问题

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

幸运的是,你可以用无数种不同的方法来修复它

最琐碎的可能是在比较之前对实际结果和预期结果进行比较

如果你想让你的项目更酷,你可以,能够比较不同格式的CSS字符串