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

218
Visualizações
Next.js Cannot use import statement outside a module in third party library

Summary

I'm using Next.js to create both API and and Frontend app. Frontend uses react. To unit testing I'm using next/jest preset and jest as an unit testing library. I'm using @tenstack/react-query to state management and when I want to test page, it throws an error. In tests where I'm using jsdom environment I add

/**
 * @jest-environment jsdom
 */

in tests to API I'm using babel-jest that is default and no need anything

error that throws


    /path/to/project/node_modules/@tanstack/react-query/src/QueryClientProvider.tsx:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import * as React from 'react'
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

The error appers in test where I want to render page that uses react-query

jest.config.js

// jest.config.js
const nextJest = require('next/jest');

const createJestConfig = nextJest({
  dir: './'
});

const customJestConfig = {
  moduleDirectories: ['node_modules', '<rootDir>/'],
  transform: {
    '^.+\\.[t|j]sx?$': 'ts-jest'
  }
};

module.exports = createJestConfig(customJestConfig);

.babelrc

{
  "presets": ["next/babel","@babel/preset-env", "@babel/preset-typescript"],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    "babel-plugin-parameter-decorator"
  ]
}

I've tried these answers but unfortunatelly it didn't help.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I solved myself an issue by mocking library @tenstack/react-query. Here is an sample code

jest.mock('@tanstack/react-query/src/QueryClientProvider', () => ({
  QueryClientProvider: ({ children }) => children
}));

jest.mock('@tanstack/react-query', () => ({
  QueryClient: jest.fn(),
  useQueryClient: jest.fn(),
  useQuery: jest.fn(),
  useMutation: jest.fn().mockReturnValue({
    mutate: jest.fn(),
    isLoading: false
  })
}));
about 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