根据文档,可以使用jest-object将默认异步超时从 5000 毫秒增加
更具体地说,通过使用jestsettimeouttimeout
我面临的问题是我正在针对一个非常慢的 API 运行一系列测试,响应时间为 5-15 秒,在每个测试的顶部配置这个 jest 对象非常烦人。
是否可以在运行所有测试文件之前声明一次这些设置?
我正在使用 Jest 为我的项目编写单元测试。项目基于 Vue、Vuetify (1.5) 和 TypeScript(以及 vue-property-decorator)。
我有基本的包装器<v-btn>。它看起来像这样:
<template>
  <v-btn
    :round="!square"
    v-on="$listeners"
  >
    <slot />
  </v-btn>
</template>
<script lang="ts">
import { Vue, Prop, Component } from 'vue-property-decorator';
import { VBtn } from 'vuetify/lib';
@Component({
  components: {
    VBtn,
  },
})
export default class Btn extends Vue {
  @Prop({ default: false, type: Boolean }) private square!: boolean;
}
</script>
Run Code Online (Sandbox Code Playgroud)
我写了基本测试,只是为了挂载这个组件:
import { mount } from '@vue/test-utils'
import Vue from 'vue';
import Btn from './Btn.vue';
describe('Btn', () => {
  it('should …Run Code Online (Sandbox Code Playgroud) 我尝试了很多方法来用玩笑来模拟通用函数,但没有成功。这是我认为正确的方法:
interface ServiceInterface {
    get<T>(): T;
}
class Service implements ServiceInterface {
    get = jest.fn(<T>(): T => {
        throw new Error("Method not implemented.");
    });
}
Run Code Online (Sandbox Code Playgroud)
编译时会抛出以下错误:
error TS2416: Property 'get' in type 'Service' is not assignable to the same property in base type 'ServiceInterface'.
  Type 'Mock<{}, any[]>' is not assignable to type '<T>() => T'.
    Type '{}' is not assignable to type 'T'.
Run Code Online (Sandbox Code Playgroud)
你能告诉我正确的方法吗?
谢谢
我正在尝试将 Visual Studio Code 配置为使用 TypeScript(在 Node 包中),我也在使用ts-jest/Jest来创建测试,此外,我还安装了 Jest 扩展。
我已经能够弄清楚如何使断点工作,但现在我注意到我无法正确地介入我的代码,我什至不能 100% 确定断点工作完美。
单击 step 按钮时,将恢复执行,并且不会在下一行再次停止,唯一让它停止的方法是在我想要的位置放置其他断点。代码会在断点设置为 AFAICT 的正确位置停止。
编辑:实际上,步进有时会再次停止,但似乎是来自实现的 js 代码(例如,console.log 步骤到与日志相关的代码,但永远不会再次踩到我的代码)
编辑#2:通过尝试使用更简单的项目重现问题,我意识到不再导入我正在使用的库(Phaser)修复了问题,我希望这可以帮助隔离问题的原因。
编辑 #3:这是一个链接,可以重现我正在谈论的问题:https : //github.com/adtrevor/VSCodeDebugIssue
如果要测试它,只需克隆它,npm install然后启动“调试玩笑测试”运行/调试面板中的任务。设置断点line 23的hey.ts(第一指令barFunction()),调试器将停在那里,但如果你在执行代码,你应该注意到,在某些时候你离开当前范围(执行后console.log("B"),再也不会回到它在我的情况)。
编辑 #4:这是一个更完整的示例,显示了https://github.com/adtrevor/TSVSCodeSOQuestion/问题。如果您克隆它,安装依赖项,并在 ChunkManager.ts 的第 156 行设置断点,您会注意到即使我应用了@GiacomoDeLiberali 的更改,步进也无法正常运行。不过,也许这个错误是相关的。
我完全不知道配置有什么问题,也不知道首先要看哪里,因为我没有关于调试的其他错误消息。这是我的配置文件的内容:
启动文件
{
  // Use IntelliSense to learn about possible Node.js debug attributes.
  // Hover to view descriptions of existing …Run Code Online (Sandbox Code Playgroud) 尝试在 TS 项目中使用纯 ESM 的文件类型模块,但我的笑话失败了。我已按照此处所示设置 ESM 选项,但仍然出现SyntaxError: Cannot use import statement outside a module错误。
我这里做了一个沙箱。
我的代码摘要:
import { fileTypeFromBuffer } from "file-type";
Run Code Online (Sandbox Code Playgroud)
笑话配置:
export default {
  testEnvironment: "jest-environment-node",
  globals: {
    extensionsToTreatAsEsm: [".ts"],
    "ts-jest": {
      useESM: true,
    },
  },
  transform: {
    "^.+\\.(ts|tsx|js|jsx)?$": "ts-jest",
    //"^.+\\.tsx?$": "ts-jest",
  },
  moduleNameMapper: {
    "^(\\.{1,2}/.*)\\.js$": "$1",
  },
  preset: "ts-jest",
  //preset: 'ts-jest/presets/default-esm' // or other ESM presets
};
Run Code Online (Sandbox Code Playgroud)
TS配置:
{
  "extends": "@tsconfig/node14/tsconfig.json",
  "compilerOptions": {
    "target": "ES2018",
    "module": "commonjs",
    "lib": ["es2018"],
    "declaration": true, …Run Code Online (Sandbox Code Playgroud) Jest 24的发行说明强调了一个我想使用的新功能:test.todo. 但是,对于我的一生,我无法使用它。
例如,我想在我的subscriptions.test.ts文件中勾画测试,所以我创建了以下内容:
describe('Subscription Tests', () => {
    test.todo('... a name');
});
Run Code Online (Sandbox Code Playgroud)
然后,TypeScript 编译器会立即向我显示一条红线,下面todo说:Property 'todo' does not exist on type 'It'.
我确定我遗漏了一些明显的东西,但此时我已经撞墙了。有人可以帮忙吗?
我有一个如下所示的函数:
function connect() {
   const secret = 'secret';
   const key = 'key';
   const region = 'region';
   const client = new AWS.DynamoDB({
      secret,
      key,
      region
   });'
   return new AWS.DynamoDB.DocumentClient({ service: client })
}
Run Code Online (Sandbox Code Playgroud)
我想测试一下连接功能。我像这样嘲笑了 DynamoDB 构造函数:
// See /sf/ask/3332458181/
jest.mock('aws-sdk', () => {
  const DynamoDB = jest.fn().mockImplementation(() => {
    return {};
  });
  return {
    DynamoDB,
  };
});
Run Code Online (Sandbox Code Playgroud)
然而,这意味着DocumentClient构造函数失败。我又该如何嘲笑呢?
我想将 Jest 与 TypeScript 和 React 一起使用,但测试套件因以下错误而失败:
\nJest encountered an unexpected token\n\n    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.\n\n    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.\n\n    By default "node_modules" folder is ignored by transformers.\n\n    Here's what you can do:\n     \xe2\x80\xa2 If …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Jest 测试期间使用顶级等待,但 Jest 抱怨说
\nerror TS1378: Top-level \'await\' expressions are only allowed when the \'module\' option is set to \'es2022\', \'esnext\', \'system\', or \'nodenext\', and the \'target\' option is set to \'es2017\' or higher.\nRun Code Online (Sandbox Code Playgroud)\n这是有问题的文件 ( src/lib/mongodb.ts):
import { MongoClient } from \'mongodb\';\nimport mongoUri from \'mongo-uri-tool\';\nimport { getMongoURI } from \'./settings/settings.js\';\n\nconst getConnectionOptions = () => {\n  const uri = getMongoURI();\n\n  return {\n    uri,\n    parsedUri: mongoUri.parseUri(uri),\n  };\n};\n\nconst connectToMongoDB = async () => {\n  const connectionOptions = …Run Code Online (Sandbox Code Playgroud) 当我使用react-testing-library时,它说错误:不变的预期应用程序路由器要安装,在开发环境中运行时没有这样的问题。
测试逻辑在这里
import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import NavBar from "@/components/NavBar";
describe("<NavBar>", () => {
    it ("the login pop out displayed after click the login/sign up button", async () => {
        render(<NavBar />);
        const loginButton = screen.getByRole("link", {
            name: "LOGIN/SIGN UP"
        });
        const loginCard = screen.getByTestId("loginCard");
        await userEvent.click(loginButton);
        expect(loginButton).toBeCalled();
        expect(loginCard).toBeVisible();
    })
});
Run Code Online (Sandbox Code Playgroud)
组件在这里:导航栏:
"use client"
import React, { Dispatch, SetStateAction } from "react";
import { useRouter } from "next/navigation";
interface NavBarProps {
  setVisibleLogin?: Dispatch<SetStateAction<boolean>>; …Run Code Online (Sandbox Code Playgroud) ts-jest ×10
jestjs ×9
typescript ×8
babeljs ×1
es6-modules ×1
javascript ×1
next.js ×1
node.js ×1
vue.js ×1
vuetify.js ×1