惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
WordPress大学
WordPress大学
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
J
Java Code Geeks
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
Google DeepMind News
Google DeepMind News
小众软件
小众软件
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
B
Blog

szhshp 的第三边境研究所

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 如果我用手搓了个暗物质雏形 | szhshp 的第三边境研究所
Web-Automation Testing | szhshp 的第三边境研究所
2022-06-25 · via szhshp 的第三边境研究所

Meta

目录

Web - Automation Testing

Hints

  • Make your test FAIL AT BEGINNING. Otherwise it may not work properly as you thought.
  • (For UI Test) Test the feature like a user instead of a developer. We don’t need to test the implementation details.
  • Only write test for current layer, do not drill down, we can create another test case to cover deeper layers/components
  • Do not include more dependencies if you can do it with Vanilla JS + Jest + Mock
  • All unit tests should be able to run WITHOUT NETWORK. Disable your network and run it to make sure no request sent.
    • You can print log in HTTP module to check if any request are sent
  • Run unit tests in PR pipeline and run integration tests (+E2E tests) on deploy pipeline.
  • Modularization: Input and output should be clear for a module, which could be more testing-friendly
  • Try to use hardcoded string on testing, if the constant variables changed, the test case will find the change and notify you
  • Try to write new feature together with test case (or follow the TDD pattern from start of your project)

Best Practice

Commands

PurposeCommandComment
Run single test filenpx jest src\folder\yourComponent1.test.jsFind the file first
Run single test case by case name (Recommended)npx jest -t='keyword' --watchProvide a keyword and run test with keyword

Debug

  • screen.debug():
    • Prints the HTML to CLI, usually it will only print part of the content, you can set the env var to show more: DEBUG_PRINT_LIMIT=20000. Or you can set the env var in test cases: process.env.DEBUG_PRINT_LIMIT = 20000;
  • screen.logTestingPlaygroundURL():
    • Generate an URL powered by testing playground, you can open the url within browser.
    • Note that theme provider may not work
  • it.only/describe.only to run only single test case/suite

UI Testing

https://kentcdodds.com/blog/common-mistakes-with-react-testing-library

Wait For Appearance

Usually used for feature with animation or loading indicator.

  • WaitFor must return an boolean
  • Remember to add await before WaitFor, otherwise it may not work
  • Animation can be tests with waitFor + timeout
RTL
  1. Testing Library Query Priority
  2. Types of Queries
  3. Appearance and Disappearance
  4. Use title case for test ID (data-testid="This-Is-A-Test-Id")
JEST
  1. jest.mock will be hoist to the top so mocking is used for whole file. We can use jest.spyOn to mock the implementation on each test case.
  2. jest.mock will ignore the original behaviour but jest.spyOn won’t. We can use both to check if certain function have been called.
  3. Put spyOn on the top of each test case, especially before the render()
  4. When create new test:
    • Check if we can mock the input or implementation
    • Is this module too big? Can we split it into smaller modules and write test for each?

Dev - Jest Cheatsheet

Manual Mocks

https://jestjs.io/docs/manual-mocks#mocking-user-modules

  • Mocking user modules:
    1. __mocks__/module.js subdirectory immediately adjacent to the module
    2. Add jest.mock('./moduleName') at top of the test
  • Mocking node modules:
    1. __mocks__/module.js at ROOT path
    2. NO need to call jest.mock explicitly, it will be mocked automatically

Partially Mock

https://jestjs.io/docs/mock-functions#mocking-partials

SpyOn would be more convenient to do the mock.

Spy and Mock

/* import all */
import * as RequestUtils from 'src/utils';

/* DO NOT DO THIS */
// import RequestUtils from 'src/utils'; 

jest.spyOn(RequestUtils, 'requestUtil').mockImplementationOnce(() => {
  return Promise.reject('mocked error');
})

Another example:

import { createChannel, createClient } from "nice-grpc";
const foo = { createChannel, createClient };

jest.spyOn(foo, "createClient").mockReturnValue({});
jest.spyOn(foo, "createChannel").mockReturnValue({});

React Testing Library

Basic Test
import React from "react";
import { render } from "@testing-library/react";
import { FixedButton } from "src/components/button";

describe("Button Component", () => {
  it("should renders correctly", () => {
    const { container } = render(<FixedButton />);
    expect(container).not.toBeUndefined();
    expect(container).toMatchSnapshot();
  });
});
Nested Query
const title = await findByRole('heading', { name: /search results/i });
expect(await title).toBeVisible();
within(title).findByRole('progressbar');

(?) Mock a React Hook

import { useData } from "src/hooks";

/* required to set mockdata into a variable with name pattern "mock*" */
const mockData = {
  data: 123456
}

jest.mock("src/hooks", () => ({
  useData: jest.fn(),
}));

describe('Test for the mock data', () => {
  it('should return mocked data', () => {
    (useData as any).mockReturnValue(mockData);
    /* assertions */
  })

})

Mock Component

Named Export
jest.mock(
  "src/NativeCommentBox",
  () => ({
    /* make sure for the correct export name */
    NativeCommentContainer: () => (
      <span data-testid={nativeCommentContainerTestId}>children</span>
    ),
  })
);
Default Export
jest.mock("react-markdown", () => ({
  default: () => <>children</>,
}));

Troubleshoot

Test case are skiped

Delete .only()

State are shared between tests

Similar Scenario:

  • Testing are fail on second test.
  • Testing fails randomly

Potentially you are import the component with providers.

Try to write a renderer function with isolated providers.

export const renderWithState = (
  component,
  { initialState, ...renderOptions } = {}
) => {
  const initialStore = getInitialStore(initialState);
  const Wrapper = ({ children }) => {
    return (
      <StoreProvider store={initialStore}>
        <OtherProvider>
          {children}
        </OtherProvider>
      </StoreProvider>
    );
  };

  return render(component, { wrapper: Wrapper, ...renderOptions });
};
Important Note: Custom renderer with Providers
const { container } = renderer(
  <MyComponent />
);

expect(container).not.toBeNull(); /* This works */
const { container, getByRole } = renderWithState( /* renderWithState may render 'MyComponent' with Providers  */
  <MyComponent />
);

/* Potentially this will be always true because we have wrap 'MyComponent' with Providers */
expect(container).not.toBeNull(); 


/* Try to do other assertions on the elements within container instead of just check container */

Jest Hints

jest.mock() hoists mocks to the top of your module’s scope

TypeError: Expected container to be an Element, a Document or a DocumentFragment but got string

import {
  fireEvent, 
  queryByRole,   /* DO NOT DO THIS */
  render, 
  waitFor
} from '@testing-library/react';

const {
  container, queryByRole  /* DO THIS */
} = render(<App />);

data-testid shows in React Dev Tools but not in Element Tab

Prevent to put the data-testid to custom components.

Otherwise we need to pass the data-testid to children components explicitly. ß

const Panel = ({
  attr1,
  attr2 
  ...rest
}) => {
  return (
    <PanelHeader
      attr1={attr1}
      attr2={attr2}
      {...rest}
    >
  )
}

When testing, code that causes React state updates should be wrapped into act(…)

https://davidwcai.medium.com/react-testing-library-and-the-not-wrapped-in-act-errors-491a5629193b

Warning: You seem to have overlapping act() calls, this is not supported. Be sure to await previous act() calls before making a new one.

This is usually same request happend in same period.

Check if you forget to add ‘await’ before find* or waitFor

Jest.SpyOn.Mock doesn’t work

SpyOn and should write before the render.

References