This is my jest configuration from the package.json file:
"jest": {
"automock": false,
"browser": true,
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./app/tests/mocks/FileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"js",
"jsx"
],
"moduleDirectories": [
"node_modules"
],
"transform": {
"^.+\\.jsx?$": "./node_modules/babel-jest",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "./app/tests/mocks/FileTransformer.js"
},
"testEnvironment": "jsdom",
"testPathDirs": [
"./app/tests"
],
"testRegex": ".*.test.js",
"verbose": true
}
And the .babelrc file located in my root folder:
{
"plugins": ["syntax-dynamic-import", "transform-runtime"],
"presets": [
[
"es2015",
{
"modules": false
}
],
"react",
"stage-0"
],
"env": {
"start": {
"presets": [
"react-hmre"
]
}
}
}
According to the documentation found at the jest getting started page this is everything I need for babel to work it's magic.
Regardless, this test:
import React from 'react';
import {shallow} from 'enzyme';
import Landing from '../components/Landing.component';
describe('<Landing/>', () => {
it('should render a header to the page', () => {
const landing = shallow(<Landing/>);
expect(landing.find('h1').text()).toBe('This is the Landing component');
});
});
returns:
FAIL app/tests/Landing.component.test.js
● Test suite failed to run
/Users/shooshte/PersonalProjects/surviveJS/app/tests/Landing.component.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules/jest-runtime/build/transform.js:320:12)
What am I doing wrong?
Jest sets the env variable to test, so I had to add my presets to the env setting in .babelrc:
{
"plugins": ["syntax-dynamic-import", "transform-runtime"],
"presets": [
[
"es2015",
{
"modules": false
}
],
"react",
"stage-0"
],
"env": {
"start": {
"presets": [
"react-hmre"
]
},
"test": {
"presets": ["es2015", "react", "stage-0"]
}
}
}
Each yearly preset only compiles what was ratified in that year. babel-preset-env replaces es2015, es2016, es2017, latest
Based on this, on latest configurations you must use/replace your Plugins/Preset of es2015 and any esX to the new one: env.
You must install babel-preset-env with npm install.
In your .babelrc you should update accordingly:
{
"presets": [
"env",
"stage-0",
"react-native"
],
"plugins": ...
}
More information on Babel plugins official Documentation.
☝️ Remember the order of the plugins/preset in the array is important.
In my case, I had the following .babelrc config:
{
"presets": [
["env", { "modules": false }],
"react",
"stage-2"
],
"plugins": [
"transform-runtime",
"transform-class-properties",
"react-hot-loader/babel"
]
}
Even though babel-env was specified I still got the error. To fix it I had to remove the "modules": false flag.
Jest doesn't handle imports so it needs a transform plugin, and this is why I had to add the plugin:
babel-plugin-dynamic-import-node
and update my babel settings to tell jest to use this plugin to transform the code properly:
"env": {
"test": {
"plugins" : ["dynamic-import-node"]
}
}
GitHub thread
You need to install babel-jest. I had the same problem.
Go to your app directory, yarn add babel-jest
Good luck!
The following .babelrc works for me (without additions):
{
"presets": [["env", {
"debug": false,
"modules": false
}], "es2015", "stage-0", "react"],
"plugins": [
"react-hot-loader/babel",
"syntax-dynamic-import",
"dynamic-import-node",
"transform-class-properties",
"transform-decorators-legacy"
]
}
"devDependencies" section of package.json looks like this:
...
"devDependencies": {
"babel-cli": "latest",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-jest": "^22.4.4",
"babel-loader": "latest",
"babel-plugin-dynamic-import-node": "^1.2.0",
"babel-plugin-lodash": "latest",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "latest",
"babel-plugin-transform-dynamic-import": "^2.0.0",
"babel-plugin-transform-flow-strip-types": "^6.22.0",
"babel-plugin-transform-object-rest-spread": "latest",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.7.0",
"babel-preset-flow": "^6.23.0",
"babel-preset-react": "^6.24.1",
"babel-preset-react-app-babel-7": "^4.0.1",
"babel-preset-stage-0": "^6.24.1",
...
Related
I want to add tailwind to storybook. So that Stories will render just like it will render on web.
I used create-react-app project-name --template typescript to create the project.
Then to install the tailwind I followed this https://tailwindcss.com/docs/guides/create-react-app instruction from the documentation of tailwind.
Once I finished it I ran the code npm sb init. Which made sure that storybook ran.
Now I need to tell storybook to use tailwindcss for styling. But I have no idea how.
Every other answer I saw tells to edit postcss.config.js files.
But I followed this https://tailwindcss.com/docs/guides/create-react-app documentation where I didnt even have to create postcss.config.js file. So I am confused to what to do now.
For clarity I will include some configuration file below.
craco.config.js
module.exports = {
style: {
postcss: {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
},
},
}
.storybook/preview.js
import "../src/index.css"
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
}
.storybook/main.js
module.exports = {
"stories": [
"../src/**/*.stories.mdx",
"../src/**/*.stories.#(js|jsx|ts|tsx)"
],
"addons": [
"#storybook/addon-links",
"#storybook/addon-essentials",
"#storybook/preset-create-react-app"
]
}
src/index.css
#tailwind base;
#tailwind components;
#tailwind utilities;
tailwind.config.js
module.exports = {
purge: ['./src/**/*.{js,jsx,ts,tsx}', './public/index.html'],
darkMode: false, // or 'media' or 'class'
theme: {
extend: {},
},
variants: {
extend: {},
},
plugins: [],
}
package.json
`{
"name": "memory",
"version": "0.1.0",
"private": true,
"dependencies": {
"#craco/craco": "^6.0.0",
"#tailwindcss/postcss7-compat": "^2.0.2",
"#testing-library/jest-dom": "^5.11.4",
"#testing-library/react": "^11.1.0",
"#testing-library/user-event": "^12.1.10",
"#types/jest": "^26.0.15",
"#types/node": "^12.0.0",
"#types/react": "^16.14.2",
"#types/react-dom": "^16.9.8",
"autoprefixer": "^9.8.6",
"postcss": "^7.0.35",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.1",
"tailwindcss": "npm:#tailwindcss/postcss7-compat#^2.0.2",
"typescript": "^4.0.3",
"web-vitals": "^0.2.4"
},
"scripts": {
"start": "craco start",
"build": "craco build",
"test": "craco test",
"eject": "react-scripts eject",
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook -s public"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"#storybook/addon-actions": "^6.1.11",
"#storybook/addon-essentials": "^6.1.11",
"#storybook/addon-links": "^6.1.11",
"#storybook/node-logger": "^6.1.11",
"#storybook/preset-create-react-app": "^3.1.5",
"#storybook/react": "^6.1.11"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
Storybook recommends using the #storybook/addon-postcss for customizing the postCSS config from now on (instead of relying on customizing the postcss-loader):
Add the postCSS addon to your installation
npm i -D #storybook/addon-postcss # or
yarn add -D #storybook/addon-postcss
Create the postcss.config.js in the project root
// postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
}
}
Add the plugin to your .storybook/main.js
// .storybook/main.js
module.exports = {
...
addons: [
...
{
name: '#storybook/addon-postcss',
options: {
cssLoaderOptions: {
// When you have splitted your css over multiple files
// and use #import('./other-styles.css')
importLoaders: 1,
},
postcssLoaderOptions: {
// When using postCSS 8
implementation: require('postcss'),
},
},
},
],
};
Import your css file in the .storybook/preview.js
// .storybook/preview.js
import '../src/styles.css';
You're almost there.
The missing piece of your config is to add a webpack configuration to apply tailwind to postcss-loader:
const path = require('path')
module.exports = {
stories: [
'../src/**/*.stories.mdx',
'../src/**/*.stories.#(js|jsx|ts|tsx)'
],
addons: [
'#storybook/addon-links',
'#storybook/addon-essentials',
'#storybook/preset-create-react-app',
],
webpackFinal: async (config) => {
config.module.rules.push({
test: /\.css$/,
use: [
{
loader: 'postcss-loader',
options: {
postcssOptions: {
plugins: [
require('tailwindcss'),
require('autoprefixer'),
],
},
},
},
],
include: path.resolve(__dirname, '../'),
})
return config
},
}
TAILWIND STORYBOOK CRA [2022-2023(hopefully)]
package.json
"devDependencies": {
"#storybook/addon-actions": "^6.5.9",
"#storybook/addon-essentials": "^6.5.9",
"#storybook/addon-interactions": "^6.5.9",
"#storybook/addon-links": "^6.5.9",
"#storybook/addon-postcss": "^2.0.0",
"#storybook/builder-webpack5": "^6.5.9",
"#storybook/manager-webpack5": "^6.5.9",
"#storybook/node-logger": "^6.5.9",
"#storybook/preset-create-react-app": "^4.1.2",
"#storybook/react": "^6.5.9",
"#storybook/testing-library": "^0.0.13",
"#typescript-eslint/eslint-plugin": "^5.28.0",
"#typescript-eslint/parser": "^5.28.0",
"autoprefixer": "^10.4.7",
"babel-plugin-named-exports-order": "^0.0.2",
"eslint": "^8.17.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-react": "^7.30.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-simple-import-sort": "^7.0.0",
"postcss": "^8.4.14",
"tailwindcss": "^3.1.1",
"webpack": "^5.73.0"
}
tailwind.config.js
module.exports = {
content: [
"./src/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
}
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
.storybook/main.js
module.exports = {
stories: [
"../src/**/*.stories.mdx",
"../src/**/*.stories.#(js|jsx|ts|tsx)"
],
addons: [
"#storybook/addon-links",
"#storybook/addon-essentials",
"#storybook/addon-interactions",
"#storybook/preset-create-react-app",
{
name: '#storybook/addon-postcss',
options: {
postcssLoaderOptions: {
implementation: require('postcss'),
},
},
},
],
framework: "#storybook/react",
core: {
"builder": "#storybook/builder-webpack5"
}
}
.storybook/preview.js
import '!style-loader!css-loader!postcss-loader!tailwindcss/tailwind.css';
export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
The answer was right but in the latest CRA I have to config like this:
config.module.rules.push({
test: /\.css$/,
use: [
{
loader: "postcss-loader",
options: {
// HERE: OPTIONS
postcssOptions: {
plugins: [require("tailwindcss"), require("autoprefixer")],
},
},
},
],
include: path.resolve(__dirname, "../"),
});
Similar to ofhouse's answer, but here is a solution if you don't want to have an extra postcss.config.js for only a few lines or if you want use typescript in everything (as the loader doesn't pick up postcss.config.ts)
Add the official postCSS addon
npm i -D #storybook/addon-postcss
yarn add -D #storybook/addon-postcss
Config main.ts & tailwind.config.ts
/* .stories/main.ts */
import postcss from 'postcss';
import * as tailwindcss from '../tailwind.config';
import type { StorybookConfig } from '#storybook/react/types';
export const addons: StorybookConfig['addons'] = [
// other addons,
{
name: '#storybook/addon-postcss',
options: {
postcssLoaderOptions: {
implementation: postcss,
postcssOptions: {
plugins: {
tailwindcss, // or you can nest your options entirely here
autoprefixer: {
// autoprefixer options
},
},
},
},
},
},
];
/* tailwind.config.ts */
import type { TailwindConfig } from 'tailwindcss/tailwind-config';
export const theme: TailwindConfig['theme'] = {
// theme options
}
// other options
This problem gave me headaches too. I followed every solution that can be found on StackOverflow and Github.
But I only could "fix it" by running the tailwind-CLI and building the global.css file and importing it to ./storybook/preview.js
here's the cli command used.
npx tailwindcss -i ./src/styles/global.css -o ./.storybook/global.css --watch
then import it from the output path (-o flag) in the command above.
// ./storybook/preview.js
import './global.css';
...
I've tried all the above answers but unfortunately, I was still getting some weird unknown word error when running the storybook.
This is the only solution that worked for me:
https://github.com/storybookjs/addon-postcss/issues/33#issuecomment-1173042151
I was tasked to make one of the mobile apps written in react compatible with opera mini in extreme mode. During the testing, I learned that nothing that is in react code works there, however by including this babel-polyfill before main.js file it actually is working.
I tried to include it to a Webpack so the code is actually converted instead of including a huge library but without success.
package.json file
"devDependencies": {
"#babel/core": "^7.4.4",
"#babel/plugin-proposal-class-properties": "^7.10.1",
"autoprefixer": "^8.6.3",
"babel-loader": "^8.0.6",
"copy-webpack-plugin": "latest",
"css-loader": "latest",
"file-loader": "latest",
"html-webpack-plugin": "latest",
"mini-css-extract-plugin": "^0.8.0",
"node-sass": "latest",
"postcss-loader": "latest",
"sass-loader": "latest",
"style-loader": "latest",
"url-loader": "latest",
"webpack": "latest",
"webpack-cli": "latest",
"webpack-dev-server": "latest"
},
"dependencies": {
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.2.0"
}
.babelrc file:
{
"presets": ["#babel/env", "#babel/react"],
"plugins": ["#babel/plugin-proposal-class-properties"]
}
webpack.config.js file:
const HtmlWebPackPlugin = require("html-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const htmlPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "index.html",
});
module.exports = {
output: {
publicPath: "/",
filename: "js/main.[hash:8].js",
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: "babel-loader",
},
{
test: /\.(png|jpg|gif|ico|svg|eot|ttf|woff|woff2)$/,
loader: "url-loader",
options: {
limit: 25000,
outputPath: "assets/",
name: "[name].[hash:8].[ext]",
},
},
{
test: /\.(scss|sass|css)$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
},
"css-loader",
"postcss-loader",
"sass-loader",
],
},
],
},
resolve: {
extensions: [".js", ".jsx", ".json"],
},
plugins: [
htmlPlugin,
new MiniCssExtractPlugin({
filename: "css/style.[hash:8].css",
}),
],
devServer: {
historyApiFallback: {
rewrites: [{ from: /^\/$/, to: "/index.html" }],
},
},
};
How these can be modified so I don't have to include entire babel-polyfill.min.js and Webpack + babel will actually convert the code to work?
--EDIT
I had success with following changes:
package.json file
...
"browserslist": [
"dead"
],
...
.babelrc file:
{
"presets": [
[
"#babel/preset-env",
{
"useBuiltIns": "entry",
"corejs": 3
}
],
"#babel/preset-react"
],
"plugins": [
"#babel/plugin-proposal-class-properties",
[
"#babel/transform-runtime",
{
"corejs": 3
}
]
]
}
webpack.config.js file:
...
entry: "./src/index.js",
...
Once this setup is in place, i have to manually include import "core-js"; at the top of every file that is part of project so for example:
index.js
App.js
components/events.jsx
components/popup.jsx
and so on...
is there a way to avoid that inclusion by some configuration in webpack or babel ? (i tried "useBuiltIns": "usage" instead of entry but it doesnt include all needed polyfills)
I was gonna suggest you drop babel-polyfill since it's deprecated as of 7.4.0 but then saw your edit.
If you want to avoid importing core-js everywhere you should use "useBuiltIns": "usage".
I think the reason it doesn't polyfill everything is because your browserslist query doesn't match to opera mini according to (https://browserl.ist/?q=dead), you should specify it, something like:
...
"browserslist": [
"dead",
"op_mini all"
],
...
When I install Mobx and Mobx react I am getting the following error.
./src/index.js
Error: The 'decorators' plugin requires a 'decoratorsBeforeExport' option,
whose value must be a boolean. If you are migrating from Babylon/Babel 6 or
want to use the old decorators proposal, you should use the 'decorators-legacy'
plugin instead of 'decorators'.
Now I have ejected the dependencies and installed the decorators-legacy as a plugin. This is what I have in my package.json file
"babel": {
"plugins": [
"transform-decorators-legacy"
],
"presets": [
"react-app"
]
},
"devDependencies": {
"#babel/plugin-proposal-decorators": "^7.1.2",
"babel-plugin-transform-decorators-legacy": "^1.3.5"
}
Any help would be appreciated as this has been driving my crazy for the last few days.
As Tholle wrote the link was very helpful. My config in my package.json.
"babel": {
"presets": ["#babel/preset-env","react-app"],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }],
["#babel/plugin-proposal-class-properties", { "loose": true }]
]
},
"devDependencies": {
"#babel/plugin-proposal-decorators": "^7.1.2",
"babel-plugin-transform-decorators-legacy": "^1.3.5"
}
i've upgraded my RN app from 0.55.4 to 0.56 that use Babel 7.
In 0.55.4 to use decorators for MOBX i use "babel-plugin-transform-decorators-legacy" but is not compatible with Babel 7...
react-native ver: 0.56.0
mobx ver: 5.0.3
mobx-react ver: 5.2.3
does anyone have the solution?
Thanks
UPDATE:
The app works in DEBUG with this configuration
package.json
...
"devDependencies": {
"#babel/core": "7.0.0-beta.47",
"#babel/plugin-proposal-decorators": "7.0.0-beta.47"
...
}
.babelrc
{
"presets": [
["react-native"]
],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }]
]
}
But in RELEASE xCode crash with this error:
babelHelpers.applyDecoratedDescriptor is not a function.
UPDATE 2, WORKING CONFIG:
This is my working configuration:
package.json
...
"devDependencies": {
"#babel/core": "7.0.0-beta.47",
"#babel/plugin-proposal-decorators": "7.0.0-beta.47",
"#babel/runtime": "7.0.0-beta.47",
"babel-jest": "23.2.0",
"babel-preset-react-native": "5.0.2",
"jest": "23.3.0",
"react-test-renderer": "16.4.1"
}
...
.babelrc
{
"presets": [
["react-native"]
],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }]
]
}
Then in the index.js (main app starting file) i need to import the decorators babel libraries:
index.js
import applyDecoratedDescriptor from '#babel/runtime/helpers/es6/applyDecoratedDescriptor';
import initializerDefineProperty from '#babel/runtime/helpers/es6/initializerDefineProperty';
Object.assign(babelHelpers, {applyDecoratedDescriptor, initializerDefineProperty});
require('./app.js');
app.js
import {AppRegistry} from 'react-native';
import AppName from './app/index';
AppRegistry.registerComponent(appName, () => AppName);
Ok, i solved all the errors by adding #babel/runtime, now the app works in DEBUG and RELEASE too.
Here the correct configuration:
package.json
...
"devDependencies": {
"#babel/core": "7.0.0-beta.47",
"#babel/plugin-proposal-decorators": "7.0.0-beta.47",
"#babel/plugin-transform-runtime": "7.0.0-beta.47",
"#babel/runtime": "7.0.0-beta.47",
"babel-jest": "23.2.0",
"babel-preset-react-native": "5.0.2",
"jest": "23.3.0",
"react-test-renderer": "16.4.1"
}
...
.babelrc
{
"presets": [
"react-native"
],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }],
["#babel/plugin-transform-runtime", {
"helpers": true,
"polyfill": false,
"regenerator": false
}]
]
}
Thanks #Hkan.
I solved this issue by installing #babel/plugin-proposal-decorators#7.0.0-beta.47 and #babel/plugin-transform-runtime#7.0.0-beta.47.
Versions might change for you. I used those versions because #babel/core was also at 7.0.0-beta.47.
Current .babelrc is:
{
"presets": [
"react-native"
],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }],
["#babel/plugin-transform-runtime", {
"helpers": true,
"polyfill": false,
"regenerator": false
}]
]
}
In my tsconfig I currently have the module compilerOption property set to "es6" however, when I run Jest I receive the following error:
Test suite failed to run
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){require('ts-jest').install();import { User } from './models/user;
^^^^^^
SyntaxError: Unexpected token import
at transformAndBuildScript (node_modules\jest-runtime\build\transform.js:320:12)
at process._tickCallback (internal\process\next_tick.js:103:7)
If I switch the module to “commonJS” then the tests run fine. However, I shouldn't need to do this as the babel plugin "transform-es2015-modules-commonjs" should transpile ES modules to commonJS for me (or is my understanding incorrect?).
I suspect I've misconfigured something small but important. Can anyone point out where I'm running into trouble?
Thanks in advance.
.tsconfig
{
"compilerOptions": {
"module": "es6", // Changing this to "commonJS" resolves the error.
"target": "es6",
"moduleResolution": "node",
"baseUrl": "src",
"allowSyntheticDefaultImports": true,
"noImplicitAny": false,
"sourceMap": true,
"outDir": "ts-build",
"jsx": "preserve",
"experimentalDecorators": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"filesGlob": [
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules",
"dist"
]
}
.babelrc
{
"presets": [
"es2015",
"react",
"stage-0",
{"modules": false}
],
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
Jest section of the package.json
"jest": {
"transform": {
"\\.(ts|tsx)$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "<rootDir>/__mocks__/styleMock.js"
},
"testPathDirs": [
"./src"
],
"collectCoverage": true,
"testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js"
}
Note: I've also followed the official recommendations for setting up with webpack 2.
This appears to be a known issue, further reference here.
I was able to workaround the issue by adding separate override tsconfig settings for jest.
"globals": {
"__TS_CONFIG__": {
"module": "commonjs",
jsx": "react"
}
Thus my project can continue to target es6 modules.
this gave me part of the solution. the final solution looked like this
package.json
{
"private": true,
"version": "0.0.0",
"name": "example-typescript",
"dependencies": {
"react": "16.4.1",
"react-dom": "16.4.1",
"lodash-es": "^4.17.11"
},
"devDependencies": {
"babel-cli": "^6",
"babel-core": "^6",
"babel-plugin-source-map-support": "^2.0.1",
"babel-plugin-transform-es2015-classes": "^6.24.1",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-polyfill": "^6",
"babel-preset-env": "^1.6",
"babel-preset-stage-0": "^6",
"babel-runtime": "^6",
"babel-jest": "^22.0.3",
"babel-plugin-transform-imports": "^1.5.1",
"babel-preset-es2015": "^6.24.1",
"#types/jest": "^23.1.1",
"#types/node": "^10.12.3",
"jest": "*",
"typescript": "*",
"ts-jest": "*"
},
"scripts": {
"test": "jest"
},
"jest": {
"moduleFileExtensions": [
"ts",
"tsx",
"js"
],
"transform": {
"^.+\\.(|ts|tsx)$": "ts-jest",
"^.+\\.(js|jsx)$": "babel-jest"
},
"transformIgnorePatterns": [],
"globals": {
"__TS_CONFIG__": {
"target": "es2015",
"module": "commonjs",
"jsx": "react"
}
},
"testMatch": [
"**/__tests__/*.+(ts|tsx|js)"
]
}
}
together with
.babelrc
{
"env": {
"test":{
"passPerPreset": true,
"presets": [
"babel-preset-env"
],
"plugins": [
"transform-es2015-modules-commonjs",
"transform-es2015-classes"
]
}
}
}
Most of the time its cause of the missing to install babel-jest