首页 > 解决方案 > 使用橡木进行 deno 测试仅检查一个文件

问题描述

问题:测试检测到所有测试文件,但只运行一个文件。

环境

deno 1.12.2 (release, aarch64-apple-darwin)
v8 9.2.230.14
typescript 4.3.5

---

export {
  assert,
  assertEquals,
} from "https://deno.land/std@0.100.0/testing/asserts.ts";

export {
  Application,
  Router,
  composeMiddleware,
  testing,
} from "https://deno.land/x/oak@v7.7.0/mod.ts";
export type {
  Context,
  Middleware,
  State,
} from "https://deno.land/x/oak@v7.7.0/mod.ts";

代码

脚本与官方文档中的脚本相同

import { testing, Middleware, assert, assertEquals } from "/deps.ts";

const mw: Middleware = async (ctx, next) => {
  await next();
  if (ctx.request.url.pathname === "/a") {
    ctx.response.body = "Hello a";
    ctx.response.headers.set("x-hello-a", "hello");
  }
};

Deno.test({
  name: "example test",
  async fn() {
    const ctx = testing.createMockContext({
      path: "/a",
    });
    const next = testing.createMockNext();

    await mw(ctx, next);

    assertEquals(ctx.response.body, "Hello a");
    assert(ctx.response.headers.has("x-hello-a"));
  },
});

脚本

deno 测试 --allow-all --importmap importmap.json --unstable 测试*

文件

输出

Check file:///Users/dd/dd/dd/test1.test.ts
Check file:///Users/dd/dd/dd/test2.test.ts
running 1 test from file:///Users/ekwon/dev/search-api/test1.test.ts
test example test ... ok (8ms)

如果我在下面运行代码,则测试会运行这两个文件。因此,我认为 testing.createMockContext、testing.createMockNext、assertEquals 正在制造问题。当我删除它们时,测试会检查这两个文件。

import { delay } from "https://deno.land/std@0.104.0/async/delay.ts";

Deno.test("async hello world", async () => {
  const x = 1 + 2;

  // await some async task
  await delay(100);

  if (x !== 3) {
    throw Error("x should be equal to 3");
  }
});

任何关于这个问题的想法将不胜感激

标签: denooak

解决方案


推荐阅读