Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
_this.store.getState is not a function when testing react component with enzyme and mocha

I'm trying to test a React component with enzyme and mocha as follows

import { mount, shallow } from 'enzyme';
import React from 'react';
import chai, { expect } from 'chai'
import chaiEnzyme from 'chai-enzyme'
import sinon from 'sinon'

import MyComponent from 'myComponent'

chai.use(chaiEnzyme())
describe('MyComponent', () => {
  const store = {
    id: 1
  }
  it ('renders', () => {
    const wrapper = mount(<MyComponent />, {context: {store: store}})
  })
})

haven't actually written the test as it fails at the declaration of wrapper

Error message: TypeError: _this.store.getState is not a function

No idea what the problem is and cant find anything addressing this!

Any help would be great!

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

This error means that store can't get the state correctly. I would recommend mocking the store using redux-mock-store and import configureStore

import configureStore from 'redux-mock-store';

then mock the state by doing this

    const initialState = { id: 1 };
    const mockStore = configureStore();

and you can continue by wrapping your component with provider

import { Provider } from 'react-redux'; // add this to the top of your file

const wrapper = mount(
  <Provider store={mockStore(initialState)}>
    <MyComponent />
  </Provider>,
);
over 4 years ago · Santiago Trujillo Relatório

0

Also, shouldn't chai.user() be chai.use() in your code example?

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda