1
因爲它已經成爲一種常用的模式,將1個特定React組件的標記,樣式和JavaScript整合到1個單個文件中。將單元測試(例如摩卡describe
)放到同一個文件中是否也是一種很好的模式?我應該將UT測試的React組件放到與該組件相同的文件中嗎?
注意:假設捆綁工具可以剝離未使用的import
s和describe
s中的代碼。
例子:
import React from 'react';
import { shallow } from 'enzyme';
import { expect } from 'chai';
const MyComponent = ({ active }) => (
<div style={{ color: active ? 'blue' : 'gray' }}>
{active ? 'this is active' : 'this is inactive'}
</div>
);
export default MyComponent;
// mocha tests (same file)
describe('MyComponent',() => {
context('active = true',() => {
it('should include is active text',() => { ... });
it('should have color blue',() => { ... });
});
context('active = false',() => { ... });
});
這會將您的測試與您的生產版本捆綁...? –
這可能是**不是一個好習慣。這將不必要地膨脹在產品中運行的代碼。更好的做法是將測試分成另一個文件並將其從軟件包中排除。 –
讓我們假設捆綁工具可以剝離未使用的導入並從生產構建 – tungv