P粉2564870772023-08-27 00:35:45
vue-jest
The src
URL for the top-level block tag in SFC cannot be resolved, so you need to do it in src/components/core/App/index.vue
Use unaliased relative paths in:
<script src="./script.js" /> <style module src="./style.css" /> <template src="./template.html" />
vue-jest
Use @vue/component-compiler-utils
to compile the template, but URL parsing requires transformAssetUrls
option. vue-jest
3.x does not support passing options to @vue/component-compiler-utils
, but it does # in #4.0.0-rc.1 ##templateCompiler.transformAssetUrlsConfigurationImplementation.
Even with URL parsing enabled, the Vue CLI configuration
to return an empty string for
require's media, including images . If your tests need to work in production with normally parsed URLs, you will need a Jest converter that mimics
url-loader. The Vue CLI configuration loader returns the parsed filename with
if larger than 4KB; otherwise returns the base64 data URL .
4:
npm i -D vue-jest@4
that will be used later below:
// <rootDir>/tests/my-jest-url-loader.js
const urlLoader = require('url-loader')
module.exports = {
process(src, filename) {
const urlLoaderOptions = {
esModule: false,
limit: 4096,
fallback: {
loader: 'file-loader',
options: {
esModule: false,
emitFile: false,
name: filename,
},
},
}
const results = urlLoader.call({
query: urlLoaderOptions,
resourcePath: filename,
}, src)
// strip leading Webpack prefix from file path if it exists
return results.replace(/^module.exports = __webpack_public_path__ \+ /, 'module.exports = ')
}
}
lodash.merge) in
jest.config.jsInsert custom configuration.
configuration in Jest global and set templateCompiler.transformAssetUrls
.
property of the merge preset to use our my-jest-url-loader
converter to process the image. This requires removing other image converters from the default Jest preset to avoid conflicts.
// jest.config.js
const vueJestPreset = require('@vue/cli-plugin-unit-jest/presets/default/jest-preset')
const merge = require('lodash.merge') 3️⃣
const newJestPreset = merge(vueJestPreset, {
globals: { 4️⃣
'vue-jest': {
templateCompiler: {
transformAssetUrls: {
video: ['src', 'poster'],
source: 'src',
img: 'src',
image: ['xlink:href', 'href'],
use: ['xlink:href', 'href']
}
}
}
},
moduleNameMapper: {
'^foo/(.*)$': '<rootDir>/',
},
})
function useUrlLoaderForImages(preset) { 5️⃣
const imageTypes = ['jpg', 'jpeg', 'png', 'svg', 'gif', 'webp']
const imageTypesRegex = new RegExp(`(${imageTypes.join('|')})\|?`, 'ig')
// remove the image types from the transforms
Object.entries(preset.transform).filter(([key]) => {
const regex = new RegExp(key)
return imageTypes.some(ext => regex.test(`filename.${ext}`))
}).forEach(([key, value]) => {
delete preset.transform[key]
const newKey = key.replace(imageTypesRegex, '')
preset.transform[newKey] = value
})
preset.transform = {
...preset.transform,
[`.+\.(${imageTypes.join('|')})$`]: '<rootDir>/tests/my-jest-url-loader',
}
}
useUrlLoaderForImages(newJestPreset)
module.exports = newJestPreset