SyntaxError:无法在JEST LWC中的模块外部使用import语句

时间:2019-11-19 20:38:31

标签: salesforce lwc

我正在尝试使用Visual Studio代码作为我的IDE测试我的第一个闪电Web组件。按照指示,我安装了Node.js,npm和jest依赖项。但是我遇到了这个错误

Error Image

尝试运行以下代码时

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

// 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!');
    }); 
});

任何输入都值得赞赏

1 个答案:

答案 0 :(得分:1)

tsconfig.json

"compilerOptions": {
   ...
  "allowJs": true
}

最好的配置

添加

"transformIgnorePatterns": [
  "node_modules/(?!lwc)"
]

删除

"testPathIgnorePatterns": [
  "node_modules"
],

如果那还不够=> 开玩笑--clearCache

希望这会有所帮助

相关问题