reactjs - Jest .tsx dosyalarını çalıştırmayı başaramıyor
Create-react-app projemde TypeScript'i uyguladım ve .tsx dosyalarını test etmek için .jsx uzantılı jest test dosyalarını çalıştıramıyorum
örneğin ConsentContainer.spec.jsx ile ConsentContainer.tsx dosyasını test etmek istiyorum ve bu hatayı aldım
test/unit/containers/ConsentContainer.spec.jsx
● Test suite failed to run
Jest encountered an unexpected token
. . .
Details:
C:\src\containers\ConsentContainer.tsx:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { useEffect, useState }
from 'react';
^^^^^^
SyntaxError: Cannot use import statement outside a module
1 | import React from 'react';
2 | import configureStore from 'redux-mock-store';
> 3 | import ConsentContainer from '../../../src/containers/ConsentContainer';
Dosyaların .jsx uzantısı olduğunda jest testleri düzgün çalışıyordu ama yapılandırmayı nasıl yapacağımı bilmiyorum
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"es6",
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"noUnusedParameters": true,
"noUnusedLocals": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"noFallthroughCasesInSwitch": true
},
"include": [
"src"
]
}
jest.config.json
module.exports = {
verbose: true,
testEnvironment: 'jsdom',
setupFiles: ['raf/polyfill'],
setupFilesAfterEnv: ['./test/jest.test-setup.js'],
moduleFileExtensions: ['js', 'jsx', 'tsx'],
moduleNameMapper: {
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'identity-obj-proxy'
},
testPathIgnorePatterns: ['/node_modules/', '/cypress/', '/.cache/Cypress/3.1.0/Cypress/'],
snapshotSerializers: ['enzyme-to-json/serializer'],
transform: {
'^.+\\.jsx?$': 'babel-jest',
'.+\\.(css|css!|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$':
'jest-transform-stub',
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/test/assetsTransformer.js'
},
transformIgnorePatterns: [
'/node_modules/(?!(p-retry)/)',
'/dist/'
]
};
babel.config.json
{
"presets": ["@babel/preset-react", "@babel/preset-env", "@babel/preset-typescript"],
"plugins": [
"react-hot-loader/babel",
"transform-object-rest-spread",
["@babel/plugin-transform-runtime", {
"regenerator": true
}],
[
"@babel/plugin-proposal-decorators",
{
"legacy": true
}
],
["@babel/plugin-proposal-private-property-in-object", { "loose": true }],
["@babel/plugin-proposal-private-methods", { "loose": true }],
["@babel/plugin-proposal-class-properties", { "loose": true }]
]
}
ts-jest paketini kurduğumda, sadece .tsx dosyalarıyla bağlantılı olanlar değil, tüm testlerim başarısız oluyor