【问题标题】:SyntaxError: Cannot use import statement outside a module in JEST LWCSyntaxError:无法在 JEST LWC 中的模块外部使用导入语句
【发布时间】:2020-03-15 11:14:22
【问题描述】:

我正在尝试使用 Visual Studio 代码作为我的 IDE 来测试我的第一个闪电网络组件。按照指示,我安装了 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 在 tests 文件夹中

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

感谢任何意见

【问题讨论】:

    标签: salesforce lwc


    【解决方案1】:

    tsconfig.json

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

    笑话配置

    添加

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

    删除

    "testPathIgnorePatterns": [
      "node_modules"
    ],
    

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

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-08
      • 2021-10-25
      • 2022-09-28
      • 2021-10-02
      • 2021-03-16
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多