const mockData = { ResultCode: '200', Msg: true, Info: { OpenId: 99, UserId: 92003, }, } AxiosPlugin.__setMockData(mockData) (wrapper.vm as any).login({ code: '29992' }).then(() => { expect(wrapper.vm.$store.state.userInfo.OpenId).toBe(mockData.Info.OpenId) expect(wrapper.vm.$store.state.userInfo.UserId).toBe(mockData.Info.UserId) })
完美通过,vue的单元测试框架算是基本搭好了,也能给领导说说了
给领导看还得有个覆盖率报告
yarn test:unit --coverage
覆盖的文件比较少啊,不包含所有的源文件啊,需要加入collectCoverageFrom配置项,至此整个单元测试就比较完备了
下面是完整jest的配置
{ "jest": { "moduleFileExtensions": [ "js", "jsx", "json", "vue", "ts", "tsx" ], "transform": { "^.+\\.vue$": "vue-jest", ".+\\.(css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$": "jest-transform-stub", "^.+\\.tsx?$": "ts-jest" }, "moduleNameMapper": { "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js", "^@/(.*)$": "<rootDir>/src/$1" }, "snapshotSerializers": [ "jest-serializer-vue" ], "testMatch": [ "**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)" ], "testURL": "http://localhost/", "collectCoverageFrom": [ "**/*.{vue,ts}", "!**/node_modules/**", "!**/*.d.ts" ] } }