如何运行与操作系统无关的Jest测试文件来检查路径? [英] How to run OS-agnostic Jest test files that check paths?

查看:50
本文介绍了如何运行与操作系统无关的Jest测试文件来检查路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下内容:

expect(func).toHaveBeenCalledWith('/path/to/file');

这在NIX操作系统上可以正常工作.但是,此测试在Windows平台上将失败,因为我应该拥有

This would work fine on NIX operating systems. However, this test would fail on Windows platform because I should instead have

expect(func).toHaveBeenCalledWith('\path\to\file');

编写测试以使其与操作系统无关的最佳方法是什么?我正在看这个

What's the best way to write tests so they are OS agnostic? I was looking at this article but that is basically saying write different tests for different OSes.

推荐答案

通常,您可以扩展 expect 以添加所需的匹配行为,其中有很多 jest-extended 中的示例.对于这种情况,也许使用 path 中可用的工具进行测试针对测试运行在哪个操作系统上的适当路径:

In general, you can extend expect to add the matching behaviour you want, there are lots of examples in jest-extended. For this case, perhaps using the tools available in path to test against the appropriate path for whatever OS the tests are running on:

import { matcherHint, printExpected, printReceived } from "jest-matcher-utils";
import * as path from "path";

expect.extend({
    toMatchPath: (actual, expected) => {
        const normalised = path.join(...expected.split("/"));
        return actual === normalised
            ? { pass: true, message: passMessage(actual, normalised) }
            : { pass: false, message: failMessage(actual, normalised) };
    },
});

const passMessage = (actual, expected) => () => `${matcherHint(".not.toMatchPath")}

Expected value not to match:
  ${printExpected(expected)}
Received:
  ${printReceived(actual)}`;

const failMessage = (actual, expected) => () => `${matcherHint(".toMatchPath")}

Expected value to match:
  ${printExpected(expected)}
Received:
  ${printReceived(actual)}`;

在测试中,您总是 编写POSIX样式的路径/path/to/thing ,而 path 负责提供适当的路径.当前操作系统的路径分隔符.使用中:

In your tests you then always write POSIX-style paths /path/to/thing, and path takes care of providing the appropriate path separator for the current OS. In use:

describe("path matching", () => {
    const actual = path.join("path", "to", "thing");

    it("normalises paths for matching", () => {
        expect(actual).toMatchPath("path/to/thing");
    });

    it("can be negated", () => {
        expect(actual).not.toMatchPath("path/to/other/thing");
    });

    it("can be used asymmetrically", () => {
        const fn = jest.fn();
        fn(actual);
        expect(fn).toHaveBeenCalledWith(expect.toMatchPath("path/to/thing"));
    });

    it("fails usefully", () => {
        const fn = jest.fn();
        fn(actual);
        expect(fn).toHaveBeenCalledWith(expect.not.toMatchPath("path/to/thing"));
    });
});

输出:

  path matching
    ✓ normalises paths for matching (3 ms)
    ✓ can be negated
    ✓ can be used asymmetrically (2 ms)
    ✕ fails usefully (3 ms)

  ● path matching › fails usefully

    expect(jest.fn()).toHaveBeenCalledWith(...expected)

    Expected: not.toMatchPath<path/to/thing>
    Received: "path/to/thing"

    Number of calls: 1

      44 |              const fn = jest.fn();
      45 |              fn(actual);
    > 46 |              expect(fn).toHaveBeenCalledWith(expect.not.toMatchPath("path/to/thing"));
         |                         ^
      47 |      });
      48 | });

      at Object.toHaveBeenCalledWith (server/demo.test.js:46:14)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 3 passed, 4 total
Snapshots:   0 total

这篇关于如何运行与操作系统无关的Jest测试文件来检查路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆