vite
The use ofjest
node
Version: v14.17.3yarn
: 1.22.10vite
: 2.5.20- Preferred to use
yarn
Install dependencies
-
Vite creates the project
NPM init vite # select vue vue-ts # Download dependency runCopy the code
-
Creating the Tests folder
// mkdir tests -> touch test.spec.ts import { add } from '.. / utils/tools' go in (' 1 + 1, () = > {it (' 1 + 1 = 2, () = > {expect (add (1, 1)). The place (2)})})Copy the code
-
New utils
// mkdir utils -> touch tools.ts function add(a: number, b: number): number{ return a + b } export { add } Copy the code
-
Installing dependencies
Yarn add @babel/core @babel/preset-env -d # Support TS file YARN add @babel/preset-typescript -d # babel-jest yarn add babel-jest -d # Install jest dependencies yarn add jest ts-jest vue-jest @types/jest @vue/test-utils -d # Ts-jest TS file JEST supported # VUe-jest Vue file Supported # @types/jest type declaration file # @vue/test-utils yarn add [email protected] [email protected] [email protected] -d # Fixed versionCopy the code
-
Package. The json configuration
{ ... "scripts": { "test": "jest" }, "jest": { "transform": { "^.+\.[t|j]sx?$": "babel-jest" } }, ... } Copy the code
-
tsconfig.json
{... "types": ["vite/client", "jest"] }Copy the code
-
New jest. Config. Js
Module. Exports = {testMatch: [" * * /? (*) + (spec | test | unit). [jt] s? (x) "/ / matching test file], the transform: {" ^. + \. [j] | t sx? $": "babel-jest", "^.+\.vue? $": "vue-jest", "^.+\.tsx$": "ts-jest" } }Copy the code
-
To build Babel. Config. Json
{"presets": [["@babel/preset-env"], "@babel/preset-typescript"]}Copy the code
\