javascript - 测试导入图像的组件时出现“SyntaxError: Invalid or unexpected token”
问题描述
我正在为一个组件构建一个测试,该组件从常量文件中调用图像的 url。该组件工作正常,但是当我运行测试时出现此错误:
SyntaxError: Invalid or unexpected token
61 | image: require('~/assets/right_facing_bird_in_tree.png'),
| ^
我不知道为什么常量文件上的 require 语句对开玩笑无效,但我需要它来正确渲染图像。
组件.vue
<template>
<div>
<div v-if="content.image" class="staff-photo">
<img
:id="content.identifier"
:src="content.image"
:style="{ height: content.imageHeight }"
/>
</div>
</div>
</template>
<script>
export default {
props: ['content']
};
</script>
常量.js
export const HOMEPAGE_CONTENT = {
huitzi: {
image: require('~/assets/right_facing_bird_in_tree.png'),
imageHeight: '340px',
title: 'We are ',
typedWords: [
'UX Designers.',
'Salesforce Developers.',
'Software Engineers.',
'Data Scientists.'
],
text: 'But we are SO much more than that.',
identifier: 'huitzi',
hasBorderBottom: false,
contentType: MAIN_PAGE_CONTENT_TYPE
}
}
jest.config.js
module.exports = {
moduleNameMapper: {
'^@/(.*)$': '<rootDir>/$1',
'^~/(.*)$': '<rootDir>/$1',
'^vue$': 'vue/dist/vue.common.js'
},
moduleFileExtensions: [
'js',
'vue',
'json'
],
transform: {
'^.+\\.js$': 'babel-jest',
'.*\\.(vue)$': 'vue-jest'
},
collectCoverage: true,
collectCoverageFrom: [
'<rootDir>/components/**/*.vue',
'<rootDir>/pages/**/*.vue'
]
}
解决方案
推荐阅读
- typescript - 多个异步函数后同步执行函数
- icons - 如何在 ionic-4 中使用 png 图像作为选项卡图标
- node.js - node.js,MongoDB - 异步和等待功能在 findOne() 函数中没有按预期工作
- node.js - 在 electronjs 应用程序中崩溃时重新启动 nodejs 服务器
- java - ElasticSearch 使用 java API“ScrolledPage.class”,我想要 getId
- c++ - 使用了未初始化的局部变量“快速”
- java - 无法理解此 java 代码的输出
- rx-java - 即使与 defer 一起使用,反应式 zipWith 也不懒惰
- mysql - 当条件对第二个表没有结果时,如何在 LEFT JOIN 上返回 NULL
- go - 开始时遇到困难。`package main`抛出运行时错误-索引超出范围?