some-table.jsx
这是我正在测试的功能组件:
import React from 'react';
const SomeTable = ({ somethings }) => (
<>
{somethings.map(something => {
return <SomeRow key={something} something={something} />
})}
</>
);
export const SomeRow = ({ something }) => {
return <p>{something}</p>
}
export default SomeTable;
这是我的测试文件:
import { create } from "react-test-renderer";
import SomeTable, { SomeRow } from "../common/ui/some-table";
test("check SomeTable has row", () => {
const renderer = create(<SomeTable somethings={["Hello", "World"]} />);
const allRows = renderer.root.findAllByType(SomeRow);
expect(allRows.length).toBe(2);
});
该测试通过。但是,如果我SomeRow
进入它自己的文件,它就会失败:
some-table.jsx
:
import React from 'react';
import { SomeRow } from './some-row';
const SomeTable = ({ somethings }) => (
<>
{somethings.map(something => {
return <SomeRow key={something} something={something} />
})}
</>
);
export default SomeTable;
some-row.jsx
:
import React from 'react';
export const SomeRow = ({ something }) => {
return <p>{something}</p>
}
失败:
expect(received).toBe(expected) // Object.is equality
Expected: 2
Received: 0
任何想法为什么会这样?感谢您的阅读。