【发布时间】:2019-05-02 16:08:13
【问题描述】:
我还没有真正接触过vue-cli 为单元测试创建的配置文件,这些文件是:
// jest.config.js
module.exports = {
moduleFileExtensions: ['js', 'jsx', 'json', 'vue'],
transform: {
'^.+\\.vue$': 'vue-jest',
'.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub',
'^.+\\.jsx?$': 'babel-jest',
'^.+\\.js$': 'babel-jest',
},
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/src/$1',
},
snapshotSerializers: ['jest-serializer-vue'],
testMatch: ['**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)'],
testURL: 'http://localhost/',
};
然而,当尝试使用npm run test:unit 运行以下测试时:
import { shallowMount } from '@vue/test-utils';
import App from '@/App.vue';
describe('App.vue', () => {
it('renders props.msg when passed', () => {
const msg = 'new message';
const wrapper = shallowMount(App);
expect(wrapper.text()).toMatch(msg);
});
});
我收到以下错误:
后台管理员/tests/unit/app.spec.js:1 ({"Object":function(module,exports,require,__dirname,__filename,global,jest){import { shallowMount } from '@vue/test-utils';
SyntaxError: Unexpected token {
好像babel 无法解析导入语句(特别是{)。
似乎是什么导致了这个问题?
【问题讨论】:
-
似乎您将节点样式导出 (
module.exports) 与 es6 样式导入 (import) 混淆了。即节点样式:module.exports&require; es6 风格:import&export. -
怎么样?我只使用 ES6 导入语句,这些似乎是问题
标签: vue.js jestjs babeljs vue-cli