unit-testing - “意外的令牌 <” 将 'npm test' 与 vue-dropdowns 和 Vue + jest 一起使用
问题描述
我不断收到来自开玩笑的错误消息“SyntaxError: Unexpected token <”
这是我在spec.js
文件中的测试。
import { shallowMount, createLocalVue } from '@vue/test-utils'
import promotionsForm from '@/components/promotions/PromotionsForm.vue'
import router from '@/router';
const localVue = createLocalVue();
jest.mock('@/store')
jest.mock('@/helpers', () => {
return {
startDate: jest.fn().mockReturnValue(
new Date(Date.now())
),
Helper: {
getCountryCurrencyCode () {
return 'USD'
}
}
}
})
describe('promotionsForm.vue', () => {
it('Past dates should be disabled', () => {
const wrapper = shallowMount(promotionsForm, {localVue, router})
expect(wrapper.find('.date-inline-picker:first-child input').exists()).toBe(true)
})
})
这是我的jest.config.js
设置
module.exports = {
moduleFileExtensions: ['js', 'json', 'vue', 'html'],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.js$': 'babel-jest',
'^.+\\.html?$': 'html-loader-jest'
},
transformIgnorePatterns: [
'/node_modules/'
],
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1'
},
snapshotSerializers: ['<rootDir>/node_modules/jest-serializer-vue'],
setupFiles: ['<rootDir>/test/unit/setup'],
testMatch: ['**/test/unit/**/*.spec.js'],
coverageDirectory: '<rootDir>/test/unit/coverage',
collectCoverageFrom: [
'src/**/*.{js,vue}',
'!src/main.js',
'!src/router/index.js',
'!**/node_modules/**'
]
}
我错过了什么或做错了什么?
解决方案
在我看来,问题在于 babel 没有设置为转换你的 vue 文件。
没有看到设置很难提出具体的建议,但我建议你阅读这里的文档:https://vue-test-utils.vuejs.org/guides/testing-single-file-components-with-jest。 html
推荐阅读
- javascript - Shopify:编辑大型菜单以在单击时打开/关闭,而不是在悬停时?
- react-native - 错误:重复的文件或模拟。请查看控制台了解更多信息(React Native Amplify)
- javascript - 将基于其角色的用户添加到单表继承用户表(TypeORM 和 NestJs)
- angular - jsPDF html() 方法拆分 HTML 元素错误
- android - 如何在android Activity中将数据绑定布局附加到root?
- android - 我想在我的笔记本电脑上访问和使用我的安卓 (Ubuntu 20.04)
- sql - 在 sql 中加入,错误 ORA-01722:invalid number
- postgresql - 一个表是否可以引用另一个表并具有引用表中没有的额外键值?
- c - 将2个对象输入C中的链表后出现无限循环
- ios - Firebase Firestore - 带宽费用