语法错误:无法在 JEST LWC 中的模块外使用 import 语句 [英] SyntaxError: Cannot use import statement outside a module in JEST LWC
本文介绍了语法错误:无法在 JEST LWC 中的模块外使用 import 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Visual Studio 代码作为我的 IDE 来测试我的第一个 Lightning Web 组件.按照指示,我安装了 Node.js、npm 和 jest 依赖项.但我收到此错误
尝试运行以下代码时
driver_Registration.html
<div class="slds-m-around_medium"><p>你好,{person}!</p>
模板>
driver_Registration.js
import { LightningElement, api } from 'lwc';导出默认类 Driver_Registration 扩展 LightningElement {@api 人 = '世界';}
hello.test.js 在 tests 文件夹中
//hello.test.js从'lwc'导入{createElement};从c/driver_Registration"导入你好;描述('c-hello', () => {afterEach(() => {//jsdom 实例在单个文件中的测试用例之间共享,因此重置 DOM而(document.body.firstChild){document.body.removeChild(document.body.firstChild);}});it('显示问候', () => {//创建元素const element = createElement('c-hello', {是:你好});document.body.appendChild(element);//验证显示的问候语const pTag = element.shadowRoot.querySelector('p');expect(pTag.textContent).toEqual('Hello, World!');});});
感谢任何输入
解决方案
tsconfig.json
"compilerOptions": {..."allowJs": 真}
玩笑配置
添加
"transformIgnorePatterns": [节点模块/(?!lwc)"]
删除
"testPathIgnorePatterns": [节点模块"],
如果这还不够=>开玩笑 --clearCache
希望能帮到你
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
}
jest config
add
"transformIgnorePatterns": [
"node_modules/(?!lwc)"
]
remove
"testPathIgnorePatterns": [
"node_modules"
],
If that's not enough => jest --clearCache
Hope this helps
这篇关于语法错误:无法在 JEST LWC 中的模块外使用 import 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文