相关疑难解决方法(0)

如何在Jest中使用ESLint

我正在尝试将ESLint linter与Jest测试框架一起使用.

Jest测试用一些全局变量运行jest,我需要告诉linter; 但是棘手的是目录结构,使用Jest测试嵌入了__tests__文件夹中的源代码,因此目录结构类似于:

src
    foo
        foo.js
        __tests__
            fooTest.js
    bar
        bar.js
        __tests__
            barTest.js
Run Code Online (Sandbox Code Playgroud)

通常情况下,我在单个目录下进行所有测试,我可以在.eslintrc那里添加一个文件来添加全局...但我当然不想.eslintrc在每个__test__目录中添加一个文件.

现在,我刚刚将测试全局变量添加到全局.eslintrc文件中,但由于这意味着我现在可以jest在非测试代码中引用,这似乎不是"正确"的解决方案.

有没有办法让eslint基于某些基于目录名称的模式应用规则,或类似的东西?

javascript eslint jestjs

196
推荐指数
7
解决办法
7万
查看次数

扩展全局公开的第三方模块

我正在尝试在Typescript中为Jest添加自定义匹配器.这工作正常,但我不能让Typescript识别扩展Matchers.

myMatcher.ts

export default function myMatcher (this: jest.MatcherUtils, received: any, expected: any): { pass: boolean; message (): string; } {
  const pass = received === expected;
  return {
    pass: pass,
    message: () => `expected ${pass ? '!' : '='}==`,
  }
}
Run Code Online (Sandbox Code Playgroud)

myMatcher.d.ts

declare namespace jest {
  interface Matchers {
    myMatcher (expected: any): boolean;
  }
}
Run Code Online (Sandbox Code Playgroud)

someTest.ts

import myMatcher from './myMatcher';

expect.extend({
  myMatcher,
})

it('should work', () => {
  expect('str').myMatcher('str');
})
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist/",
    "moduleResolution": "node", …
Run Code Online (Sandbox Code Playgroud)

typescript jestjs

22
推荐指数
2
解决办法
3367
查看次数

Vite / Jest / React 测试

我正在尝试使用 Jest 在 React 应用程序中进行测试;我的应用程序使用 Vite 作为模块捆绑器。问题是,每次运行测试时都会出现以下错误:

\n
   > react-test@0.0.0 test\n> jest\n\n FAIL  src/test/App.test.jsx\n  \xe2\x97\x8f Test suite failed to run\n\n    Jest 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" …
Run Code Online (Sandbox Code Playgroud)

javascript testing node.js reactjs jestjs

9
推荐指数
1
解决办法
1万
查看次数

Webpack 5 和 Jest - 类型“JestMatchers<HTMLElement>”上不存在属性“toBeInTheDocument”

我无法获得 Jest 与 Webpack 5 和 TypeScript 一起使用的类型,我已经尝试了针对同一问题的其他解决方案。在我下面的示例测试中,问题仅在于“screen”和“toBeInTheDocument”。我倾向于认为这是 ESLint 配置问题。

我无法运行测试,它们失败了Property 'toBeInTheDocument' does not exist on type 'JestMatchers<HTMLElement>'.。我正在使用纱线 3.1.0。我已经尝试过这个答案和许多其他答案,包括通过导入类型tsconfig.json。我究竟做错了什么?

测试示例 ( src/components/Example/spec/test.spec.tsx):

import * as React from 'react';
import { render, screen } from '@testing-library/react';
import { Example } from './Example';
import "@testing-library/jest-dom/extend-expect";
import "@testing-library/jest-dom";

test("Renders correctly", () => {
  render(<Example />);
  const example = screen.getByAltText("Example");
  expect(example).toBeInTheDocument();
});
Run Code Online (Sandbox Code Playgroud)

笑话.config.js:

module.exports = {
  // An array of directory names to be searched recursively up …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs eslint webpack react-testing-library

4
推荐指数
1
解决办法
6925
查看次数

react-testing-library为什么toBeInTheDocument()不是函数

谁能帮我,我真的很坚持。这是我的工具提示代码,可切换css属性显示:MouseOver和Mouse Out显示上的块:无

 it('should show and hide the message using onMouseOver and onMouseOut events respectively', () => {
    const { queryByTestId, queryByText } = render(
      <Tooltip id="test" message="test" />,
    )
    fireEvent.mouseOver(queryByTestId('tooltip'))
    expect(queryByText('test')).toBeInTheDocument()
    fireEvent.mouseOut(queryByTestId('tooltip'))
    expect(queryByText('test')).not.toBeInTheDocument()
    cleanup()
  })
Run Code Online (Sandbox Code Playgroud)

我不断收到错误TypeError:Expect(...)。toBeInTheDocument不是一个函数

有谁知道为什么会这样吗?我用于渲染和快照组件的其他测试均按预期工作。就像queryByText和queryByTestId一样。

谢谢

reactjs react-testing-library

3
推荐指数
9
解决办法
1501
查看次数