salesforce - SyntaxError: Cannot use import statement outside a module in JEST LWC
问题描述
I am trying to test my first lightning web component using visual studio code as my IDE. As instructed I installed Node.js, npm and jest dependency. But I am getting this error
when trying to run the below code
driver_Registration.html
<template>
<div class="slds-m-around_medium">
<p>Hello, {person}!</p>
</div>
</template>
driver_Registration.js
import { LightningElement, api } from 'lwc';
export default class Driver_Registration extends LightningElement {
@api person = 'World';
}
hello.test.js in tests folder
// hello.test.js
import { createElement } from 'lwc';
import Hello from 'c/driver_Registration';
describe('c-hello', () => {
afterEach(() => {
// The jsdom instance is shared across test cases in a single file so reset the DOM
while (document.body.firstChild) {
document.body.removeChild(document.body.firstChild);
}
});
it('displays greeting', () => {
// Create element
const element = createElement('c-hello', {
is: Hello
});
document.body.appendChild(element);
// Verify displayed greeting
const pTag = element.shadowRoot.querySelector('p');
expect(pTag.textContent).toEqual('Hello, World!');
});
});
Any input is appreciated
解决方案
tsconfig.json
"compilerOptions": {
...
"allowJs": true
}
笑话配置
添加
"transformIgnorePatterns": [
"node_modules/(?!lwc)"
]
消除
"testPathIgnorePatterns": [
"node_modules"
],
如果这还不够 => 开玩笑 --clearCache
希望这可以帮助
推荐阅读
- firebase - 使用 Firebase signInWithRedirect 我可以将其配置为将我重定向回特定页面
- c - 在 system() 调用中转义 " 以在 echo 命令中使用
- python - PyTest:自动删除使用 tmpdir_factory 创建的临时目录
- web - 在 python 中用漂亮的汤刮网页
- sql - SQL - 计算产品的累积平均成本价
- java - java.lang.NullPointerException at impl.org.controlsfx.skin.PopOverSkin.lambda$new$68(PopOverSkin.java:255)
- google-cloud-platform - Python Bigtable 客户端在 deepcopy 上花费了很多时间
- javascript - Mongodb shell 4.0.0 未在 Linux mint 18 上安装
- android - onPageFinished() 有时不会被调用
- vue.js - v-for 渲染具有多个 tr 的 html 表