Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Help Configuring Expo 52 with Jest and @testing-library/react-native to Test Elements with expect().toHaveStyle

未关闭
#1,398 7 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@danstepanov 已经在做这个了。

开始于 2026年4月15日。

评估

这个 Issue 还没有评估数据。

描述

deferred (post-5.0) needs more info

Hi there! 👋🏾

I’m currently working on a project with Expo 52 and trying to set up Jest testing with @testing-library/react-native. Everything works fine in development and production, but I’m having trouble running tests that involve NativeWind styles.

In my tests, I’m trying to use expect(text).toHaveStyle() to check if the styles are being applied correctly, but it's not working in the test environment.

I’ve already followed the steps in the documentation, but I’m still facing issues with applying styles during tests.

Example Code

I have the following code using useColorScheme():

import * as React from "react";
import { Text, View } from "react-native";
import { render } from "@testing-library/react-native";

const BlackViewWhiteText = () => {
  return (
    <View
      testID="black-view"
      className="bg-black flex-1 justify-center items-center"
    >
      <Text className="text-white">white text</Text>
    </View>
  );
};

it("should correct styles for black background and white text using NativeWind", () => {
  const { getByText, toJSON, getByTestId } = render(<BlackViewWhiteText />);

  const text = getByText("white text");
  const view = getByTestId("black-view");

  expect(toJSON()).toMatchSnapshot();

  expect(text).toHaveProp("className");
  expect(view).toHaveStyle({ color: "black" }); // same rgb(0,0,0)
  expect(text).toHaveStyle({ color: "white" }); // same rgb(255,255,255)

  /* expect(element).toHaveStyle()
    - Expected
    + Received
    - color: black;
    */
});

However, the test is not behaving as expected when it comes to checking styles, especially with the NativeWind utilities. I would appreciate any help on how to properly configure Expo 52 with Jest and @testing-library/react-native to make these tests pass.

It would also be great to have an example of the Jest configuration and how to test the useColorScheme changing dark mode.
Thanks in advance for your help!

主要语言
JavaScript
星标
8.1k
派生
409
平均合并
1 天 15 小时
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nativewind/nativewind 的其他 Issue

查看 nativewind/nativewind 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。