SyntaxError: Cannot use import statement outside a module in JEST LWC

99封情书 提交于 2020-01-30 09:16:25

问题


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

Error Image

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


回答1:


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



来源:https://stackoverflow.com/questions/58942442/syntaxerror-cannot-use-import-statement-outside-a-module-in-jest-lwc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!