I have been struggling for a couple of days now to get tests running for simple react-native with expo + typescript + jest + ts-jest. I have already asked a related question here Here is the setup of my project:
{
"compilerOptions": {
"noEmit": true,
"lib": ["dom", "esnext"],
"jsx": "react-native",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true
}
}
module.exports = function(api) {
api.cache(true);
return {
presets: ["babel-preset-expo"]
};
};
const { defaults: tsjPreset } = require("ts-jest/presets");
module.exports = {
...tsjPreset,
preset: "react-native",
transform: {
...tsjPreset.transform,
"\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
globals: {
"ts-jest": {
babelConfig: true
}
},
cacheDirectory: ".jest/cache"
};
I get this error
ReferenceError: React is not defined
because I am importing react like this in my file:
import React from 'react'
If I import like import * as React from 'react'
it works.
Any help will be greatly appreciated as I have spent already a few days in this project.
I had the exact same issue and fixed it by changing my tsconfig.json
from
{
"compilerOptions": {
/* Basic Options */
"target": "esnext" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"es6"
] /* Specify library files to be included in the compilation. */,
...
}
}
to
{
"compilerOptions": {
/* Basic Options */
"target": "es2017" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
"module": "ESNext" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
"lib": [
"es2017"
] /* Specify library files to be included in the compilation. */,
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With