全局安装Jest
npm install jest --global
命令行输入Jest命令
$ jest
FAIL packages/Input/__test__/input.spec.ts
● Test suite failed to run
Your test suite must contain at least one test.
at onResult (../../tool/install/node-v12.22.1-win-x64/node_modules/jest/node_modules/@jest/core/build/TestScheduler.js:175:18)
at Array.map (<anonymous>)
FAIL packages/Axios/axios.test.ts
● Test suite failed to run
Jest encountered an unexpected token
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.
By default "node_modules" folder is ignored by transformers.
Here's what you can do:
• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/configuration
For information about custom transformations, see:
https://jestjs.io/docs/code-transformation
Details:
D:\code\vue3-component\packages\Axios\axios.test.ts:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import Axios from './index';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime.createScriptFromCode (../../tool/install/node-v12.22.1-win-x64/node_modules/jest/node_modules/jest-runtime/build/index.js:1479:14)
Test Suites: 2 failed, 2 total
Tests: 0 total
Snapshots: 0 total
Time: 2.08 s
Ran all test suites.
Jest支持Es6、typeScript、vue
创建jest.config.js文件,内容如下
testMatch: [ //匹配测试用例的文件
'<rootDir>/test/*.test.ts'
],
transform: {
'^.+\\.js$': '<rootDir>/node_modules/babel-jest',
'^.+\\.ts$': '<rootDir>/node_modules/ts-jest',
".*\\.(vue)$": "<rootDir>/node_modules/vue-jest"
},
- 安装 npm install vue-jest@next -D
- 安装 npm install babel-jest@^26.3.0 -D
test脚本提示找不到名称"test"
找不到名称 "test"。是否需要安装测试运行器的类型定义? 请尝试使用 `npm i --save-dev @types/jest` 或 `npm i --save-dev @types/mocha`。ts(2582)
any
TypeError: Jest: a transform must export a process function
TypeError: Jest: a transform must export a `process` function.
at ScriptTransformer._getTransformer (node_modules/@jest/transform/build/ScriptTransformer.js:357:15)
at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:419:28)
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:523:40)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
解决办法,将ts-jest的版本从 27.x.x 降级为26.x.x
TypeError: babelJest.getCacheKey is not a function
TypeError: babelJest.getCacheKey is not a function
at Object.getCacheKey (node_modules/vue-jest/lib/index.js:15:19)
at ScriptTransformer._getCacheKey (node_modules/@jest/transform/build/ScriptTransformer.js:266:23)
at ScriptTransformer._getFileCachePath (node_modules/@jest/transform/build/ScriptTransformer.js:293:27)
at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:421:32)
at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:523:40)
at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
解决办法,将babel-jest的版本从27.x.x 降级为 26.x.x