I am trying to setup test environment for a react project written in typescript. I have done it before for react but with ES6.
Following is how the relevant parts of my package.json look like -
"scripts": {
"test": "NODE_ENV=test jest",
"build": "webpack --mode production",
"start": "webpack-dev-server --open --mode development"
},
"jest": {
"moduleNameMapper": {
"\\.(css)$": "jest-css-modules"
},
"transform": {
"\\.(ts|tsx)$": "ts-jest"
},
"testRegex": ".test.(ts|tsx?)$",
"moduleFileExensions": ["ts", "tsx", "js", "jsx", "json", "node"]
},
I would like to mention that I am counting on ts transpiler and not configured babel at all.
Following are the dependencies I have included -
"dependencies": {
"@types/jest": "^23.1.4",
"@types/react": "^16.4.5",
"@types/react-dom": "^16.0.6",
"@types/react-redux": "^6.0.3",
"@types/redux": "^3.6.0",
"deep-freeze": "0.0.1",
"jest": "^23.2.0",
"jest-css-modules": "^1.1.0",
"react-redux": "^5.0.7",
"redux": "^4.0.0",
"ts-jest": "^23.0.0",
"webpack": "^4.12.2"
},
"devDependencies": {
"awesome-typescript-loader": "^5.2.0",
"css-loader": "^0.28.11",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"prop-types": "^15.6.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"style-loader": "^0.21.0",
"typescript": "^2.9.2",
"typings-for-css-modules-loader": "^1.7.0",
"url-loader": "^1.0.1",
"webpack-cli": "^3.0.8",
"webpack-dev-server": "^3.1.4"
}
I am unable to understand following error when I run npm run test
as I have already installed and specified ts-jest in transform -
Validation Error:
Module ts-jest in the transform option was not found.
<rootDir> is: /Users/rahul/ot/Lab/webpack-playlist
Following is how tsconfig.json looks like
{
"compilerOptions": {
"jsx": "react",
"noImplicitAny": true,
"sourceMap": true,
"target": "es5"
}
}
Not sure how I can I resolve the error above.
I also tried configuring ts-jest in tsconfig.json like below but it still gave same error -
// tsconfig.json
"jest": {
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
}
}
// tsconfig.jest.json
{
"extends": "./tsconfig",
"compilerOptions": {
"module": "commonjs"
}
}
Jest supports TypeScript, via Babel. First, make sure you followed the instructions on using Babel above. Next, install the @babel/preset-typescript : npm.
When you run jest with a jest. config. ts file it will use ts-node to compile that file, then it will pass it to ts-jest which will compile your tests, then it will pass those .
Looking through this, there isnt much that is different from my setup here:
https://github.com/jasonraimondi/typescript-react-starter
There is minor differences in your jest config. Below is the contents of my jest.config.js
file.
module.exports = {
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
],
"setupFiles": [
"<rootDir>/test/setupTests.ts"
]
};
I have a post about testing a Typescript React App using ts-jest without "create-react-app" that hopefully may be of some help to.
Here is an example setup for using Jest in a TypeScript React project. I created it myself and I hope it helps you:
jest.config.js
const {defaults} = require('jest-config');
module.exports = {
bail: true,
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
roots: ['src'],
testMatch: ['<rootDir>/src/**/?(*.)test.{ts,tsx}'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
verbose: true,
};
package.json
{
"dependencies": {
"@types/react": "16.7.18",
"@types/react-dom": "16.0.11",
"react": "16.7.0",
"react-dom": "16.7.0"
},
"devDependencies": {
"jest": "23.6.0",
"ts-jest": "23.10.5"
},
"scripts": {
"test": "jest"
},
"version": "0.0.0"
}
src/Welcome.tsx
import * as React from 'react';
interface WelcomeProps {
name: string,
}
const Welcome: React.FunctionComponent<WelcomeProps> = (props: WelcomeProps) => {
return <h1>Hello, {props.name}</h1>;
};
export default Welcome;
src/Welcome.test.tsx
import React from 'react';
import ReactDOM from 'react-dom';
import Welcome from './Welcome';
it('renders without crashing', () => {
const div = document.createElement('div');
ReactDOM.render(<Welcome name="John Doe"/>, div);
ReactDOM.unmountComponentAtNode(div);
});
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