我正在使用Jest對我的一個組件運行單元測驗,但我遇到了一些錯誤。
我正在嘗試測驗的組件使用tinymce,因此,我從 tinymce 匯入了一些檔案。我在官方 Jest 檔案中看到我插入了以下內容,這些內容在我的setupTests.js檔案中:
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addListener: jest.fn(), // Deprecated
removeListener: jest.fn(), // Deprecated
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn(),
})),
});
但是,我已經這樣做了,但是我遇到了另一個問題:
Jest 遇到了意外的令牌 SyntaxError: Unexpected token '.'
匯入“tinymce/skins/ui/oxide.skin.min.css”
我嘗試遵循使用“moduleNameMapper”模擬來自 Tinymce 的所有內容以及模擬非 JS 模塊的建議。例如,我的_jest.config.js檔案包括:
module.exports = {
"moduleNameMapper": {
"^image![a-zA-Z0-9$_-] $": "GlobalImageStub",
"^[./a-zA-Z0-9$_-] \\.png$": "<rootDir>/RelativeImageStub.js",
"module_name_(.*)": "<rootDir>/substituted_module_$1.js",
"assets/(.*)": [
"<rootDir>/images/$1",
"<rootDir>/photos/$1",
"<rootDir>/recipes/$1"
]
}
"transformIgnorePatterns": [
"<rootDir>/node_modules/"
]
}
以上不起作用,我仍然得到同樣的錯誤。
編輯:
根據其中一個建議,我創建了一個styleMock.js檔案,其中包含module.exports = {};并包含在我的src/tests/jest/__mocks__路徑中。然后我輸入:
"moduleNameMapper": {
'\\.(css|less)$': '<rootDir>/src/tests/jest/__mocks__/styleMock.js'
}
但我仍然遇到與上述相同的錯誤。
uj5u.com熱心網友回復:
遵循此檔案有效。
換句話說,添加package.json以下內容:
{
"jest": {
"moduleNameMapper": {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
"\\.(css|less)$": "identity-obj-proxy"
}
}
}
跑步的同時 yarn add --dev identity-obj-proxy
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/416277.html
標籤:
