[英]setting up ts-jest for react typescript project
I am trying to setup test environment for a react project written in typescript.我正在尝试为用打字稿编写的 React 项目设置测试环境。 I have done it before for react but with ES6.
我之前为 React 做过,但使用 ES6。
Following is how the relevant parts of my package.json look like -以下是我的 package.json 的相关部分的样子 -
"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.我想提一下,我指望 ts 转译器,而根本没有配置 babel。
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 -我在运行
npm run test
时无法理解以下错误,因为我已经安装并在转换中指定了 ts-jest -
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以下是 tsconfig.json 的样子
{
"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 中配置 ts-jest,如下所示,但它仍然出现相同的错误 -
// tsconfig.json
"jest": {
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.jest.json"
}
}
}
// tsconfig.jest.json
{
"extends": "./tsconfig",
"compilerOptions": {
"module": "commonjs"
}
}
Looking through this, there isnt much that is different from my setup here:看看这个,与我在这里的设置没有什么不同:
https://github.com/jasonraimondi/typescript-react-starter 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.下面是我的
jest.config.js
文件的内容。
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.我有一篇关于在没有“create-react-app”的情况下使用 ts-jest 测试 Typescript React App的帖子,希望可能对您有所帮助。
Here is an example setup for using Jest in a TypeScript React project.这是在 TypeScript React 项目中使用 Jest 的示例设置。 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包.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源代码/欢迎.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 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);
});
This is the only configuration that worked for me.这是唯一对我有用的配置。 Here are my specs:
这是我的规格:
{
"name": "project",
"version": "1.0.0",
"description": "project",
"jest": {
"modulePathIgnorePatterns": [
"dist/"
],
"snapshotSerializers": [
"enzyme-to-json/serializer"
],
"moduleNameMapper": {
"^.+\\.(css|less|scss)$": "identity-obj-proxy"
},
"setupFilesAfterEnv": [
"./setupTests.ts"
],
"coverageReporters": [
"text",
"lcov"
],
"coverageDirectory": ".",
"transform": {
".(js|jsx)": "babel-jest",
".(ts|tsx)": "ts-jest"
}
},
"peerDependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"devDependencies": {
"@types/enzyme": "^3.10.5",
"@types/enzyme-adapter-react-16": "^1.0.6",
"@types/jest": "^26.0.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"enzyme-adapter-react-16": "^1.12.1",
"ts-jest": "^26.1.0",
"typescript": "~3.9.5"
},
"scripts": {
"tsc": "tsc --build tsconfig.json",
"watch": "tsc --watch",
"test": "jest",
"test-coverage": "jest --coverage"
}
}
import * as Enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
And finally here is my tsconfig.json:最后这里是我的 tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"allowJs": true,
"declaration": true,
"declarationMap": true,
"jsx": "react",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"noImplicitAny": true,
"target": "es5",
"outDir": "./dist",
"declarationDir": "./dist",
"lib": ["es2016", "dom"]
},
"include": ["./src/index.ts"]
}
I had same error and got it fixed by running following commands in terminal: Via npm: 1. "npm i -D jest typescript" 2. "npm i -D ts-jest @types/jest " 3. "npx ts-jest config:init" 4. "npm t or npx jest"我有同样的错误并通过在终端中运行以下命令来修复它:通过 npm: 1. "npm i -D jest typescript" 2. "npm i -D ts-jest @types/jest " 3. "npx ts-jest config:init" 4. "npm t 或 npx jest"
Via yarn: 1. "yarn add --dev jest typescript" 2. "yarn add --dev ts-jest @types/jest" 3. "yarn ts-jest config:init" 4. "yarn test or yarn jest"通过yarn:1.“yarn add --dev jest typescript” 2.“yarn add --dev ts-jest @types/jest” 3.“yarn ts-jest config:init” 4.“yarn test or yarn jest”
I am using ubuntu 18.04.3 LTS.我正在使用 ubuntu 18.04.3 LTS。
According to docs of ts-jest
you should set globals.tsConfig
, but it must be globals.tsconfig
.根据
ts-jest
文档,您应该设置globals.tsConfig
,但它必须是globals.tsconfig
。 For example:例如:
globals: {
'ts-jest': {
// ts-jest configuration goes here
// https://huafu.github.io/ts-jest/user/config/#ts-jest-options
// specify tsconfig.json to be used by ts-jest
tsconfig: '<rootDir>/test/tsconfig.test.json',
},
},
With tsconfig
in works for me, with tsConfig
no. tsconfig
对我tsConfig
, tsConfig
没有。
Source: https://dev.to/ushakovmaksim/comment/1bc8j来源: https : //dev.to/ushakovmaksim/comment/1bc8j
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.