Tengo un problema extraño con PolyJS y Jest. Tengo 2 pruebas en un solo archivo: la primera pasa y la segunda devuelve un error de la fetch en MyComponent: TypeError: Cannot read property 'status' of undefined"
No importa cuál es el segundo. Cada prueba independiente pasa, pero juntas la segunda falla, por lo que parece que Polly simplemente no está haciendo la búsqueda de sus grabaciones.
Probé varias cosas como context.polly.flush() en afterEach pero parece que no funciona. ¿Algunas ideas?
Configuración de Polly:
export const setupDefaultPolly = () => { const context = setupPolly({ mode: process.env.POLLY_MODE, adapters: [require('@pollyjs/adapter-node-http')], persister: require('@pollyjs/persister-fs'), persisterOptions: { fs: { recordingsDir: path.resolve(__dirname, '../__recordings__'), }, }, }) return contextMi archivo de prueba:
describe('MyComponent', () => { setupDefaultPolly() it('should render correctly', async () => { const fetchSpy = jest.spyOn(global, 'fetch') let wrapper await act(async () => { wrapper = mount(<MyComponent thing="exists" />) }) expectLoadingPage(wrapper).toBeTruthy() await waitFor(() => { wrapper.update() expectLoadingPage(wrapper).toBeFalsy() }) expect(wrapper.text()).toContain('exists') expect(withoutMuiID(wrapper)).toMatchSnapshot() expect(fetchSpy).toHaveBeenCalledTimes(3) }) it('should display error when not found', async () => { let wrapper await act(async () => { wrapper = mount(<MyComponent thing="not-exists" />) }) expectLoadingPage(wrapper).toBeTruthy() await waitFor(() => { wrapper.update() expectLoadingPage(wrapper).toBeFalsy() }) expect(wrapper.text()).toContain('Not found: not-exists') expect(withoutMuiID(wrapper)).toMatchSnapshot() }) })