Import from index.ts export not working, ex:
file structure:
app
|--shared
|--index.ts
|--utils.ts
utils.ts:
export function myFunction(){...
index.ts:
export * from 'utils.ts';
import {myFunction} from 'app/shared' doesn't work in jest (works when running the app).
export {myFunction} from 'app/shared.index' works.
ts-jest should work the same was as plain ts
I am experiencing this error too. Getting the Jest encountered an unexpected token error pointing to the first line of my index.ts file which is exporting modules like export * from './moduleFile';. I've tried all sorts of fixes with babel plugins and other configurations with no luck. Here's my jest config (in my package.json file):
"jest": {
"preset": "jest-expo",
"globals": {
"NODE_ENV": "test"
},
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
],
"transform": {
"^.+\\.jsx?$": "<rootDir>/node_modules/babel-jest",
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"transformIgnorePatterns": [
"node_modules/(?!((jest-)?react-native|react-clone-referenced-element|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|sentry-expo|native-base|react-dom|@unimodules))"
]
},
hi, would you please provide a minimum repo ?
I am no longer having this issue. I've gone through a considerable amount of refactoring and changes so I can't pinpoint what fixed it. For anyone running into this here's what my jest.config looks like now:
const { defaults: tsjPreset } = require('ts-jest/presets');
module.exports = {
preset: 'jest-expo',
globals: {
'ts-jest': {
tsConfig: './tsconfig.jest.json',
},
},
transform: {
'^.+\\.(js|jsx)?$': '<rootDir>/node_modules/babel-jest',
...tsjPreset.transform,
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
transformIgnorePatterns: [
'node_modules/(?!((jest-)?react-native|react-clone-referenced-element|expo(nent)?|@expo(nent)?/.*|react-navigation|@react-navigation/.*|sentry-expo|@sentry|native-base|native-base-shoutem-theme|react-dom|unimodules|@unimodules|expo-asset))',
'local_modules',
],
testPathIgnorePatterns: ['/node_modules', '/local_modules/'],
coveragePathIgnorePatterns: [
'/node_modules',
'/local_modules/',
'/src/theme/components/',
'/src/theme/variables/',
'/__test-data__/',
],
moduleNameMapper: {
'\\.(jpg|ico|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
'<rootDir>/__mocks__/fileMock.js',
},
};
I can confirm that explicitly adding index in the import statement works, tsconfig.json is the one automatically generated by create-react-app.
In my case, the erroneous import statement looks like this import {...} from "./App/"; which works fine in the app but not in jest. It works after changing to import {...} from "./App/index";
@vicary do you still encounter the same issue ? If yes, please us provide a repo :) thanks
I am having the same issue and using import {...} from "./module/index" doesn't work I need to specify the module when i import it. I create e minimal repo where I can reproduce it repo in master the problem exist but in this branch remove-import-from-index-export works well and the diff is
diff --git a/src/services/api/http.ts b/src/services/api/http.ts
index 80bd1a1..2f422c4 100644
--- a/src/services/api/http.ts
+++ b/src/services/api/http.ts
@@ -2,3 +2,3 @@ import axios, { AxiosRequestConfig, Method } from "axios"
-import { logger } from "../../loaders"
+import { logger } from "../../loaders/logger"
You can see the runs of github actions to see the errors @ahnpnl
Thanks a lot for the reproduce repo. I will take a look when there are some free times 馃憤
I am having the same issue and using
import {...} from "./module/index"doesn't work I need to specify the module when i import it. I create e minimal repo where I can reproduce it repo inmasterthe problem exist but in this branchremove-import-from-index-exportworks well and the diff isdiff --git a/src/services/api/http.ts b/src/services/api/http.ts index 80bd1a1..2f422c4 100644 --- a/src/services/api/http.ts +++ b/src/services/api/http.ts @@ -2,3 +2,3 @@ import axios, { AxiosRequestConfig, Method } from "axios" -import { logger } from "../../loaders" +import { logger } from "../../loaders/logger"You can see the runs of github actions to see the errors @ahnpnl
hi @jyeros , I have checked your repo and indeed I can see the error. However, I don't think this is ts-jest issue. I have tested with babel-jest and saw the same issue so it's most likely about your codes not accepted by jest.
To test with babel-jest you can do these steps:
transform option in jest.config.jstransform: {
"^.+\\.ts?$": "babel-jest",
},
babel-jest, @babel/preset-env, @babel/preset-typescriptbabel.config.js with this contentmodule.exports = {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "current",
},
},
],
"@babel/preset-typescript",
],
}
jest cache and rerun the test. You will see similar errorTest suite failed to run
TypeError: (0 , _http.httpRequest) is not a function
3 | import { httpRequest } from "../services/api/http"
4 |
> 5 | const request = httpRequest({
| ^
6 | url: "http://localhost:8080",
7 | headers: {
8 | Accept: "application/json",
at Object.<anonymous> (src/routes/index.ts:5:17)
at Object.<anonymous> (src/loaders/index.ts:6:1)
close as this is not ts-jest problem. I have debugged ts-jest codes and everything went well. This is the problem of upstream jest.
@ahnpnl Adding to the context, some recent projects of mine in React Native seems do tests correctly, didn't have chance for a greenfield web project yet.
Since React Native typescript template uses jest directly, at least we know some version of it can handle these imports, upgrading dependent version in ts-jest may very well fix this.
Oh it鈥檚 nice to hear that.
FYI the error comes from jest runtime, not typescript itself and ts-jest internal typescript compiler delivered the compiled js and jest runtime takes care of the rest.
Since it鈥檚 related to how jest runtime accepts the transpiled codes, ts-jest can鈥檛 do anything.
Most helpful comment
Thanks a lot for the reproduce repo. I will take a look when there are some free times 馃憤