I have watched many tutorial videos on testing, but usually those tests test passed props to component or uses data-testid="". Is there a way to test non-exported functions? I read about rewire module which does this but problem with that is that Jest will not consider functions called from rewire in coverage reports.
// Random.js
import React from 'react'
export function sayHi() {
return '👋';
}
function ListBox() {
function saySecret() {
return '🤫';
}
return (
<div>ListBox</div>
)
}
export default ListBox
First one which has export would be :
// Random.test.js
import { sayHi } from './Random.js';
describe('sayHi', () => {
it('returns wave emoji', () => {
expect(sayHi()).toBe('👋');
});
});
How should I test saySecret?
data-testid=""" - to put it another way, those tests exercise the component through its public API. That's what you're supposed to do, otherwise you couple your tests to implementation details rather than behaviour.