本文介绍了"SyntaxError:无法在模块外部使用import语句"使用带有lit-html的打字稿编写测试时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用打字稿用 lit-html 编写一个简单演示:

I use typescript to write a simple demo with lit-html:

import {html, TemplateResult} from 'lit-html';

export default function sayHello(name: string): TemplateResult {
  return html`<h1>Hello ${name}</h1>`;
}

并使用笑话编写一些简单的测试:

and use jest to write some simple test:

import sayHello from "./sayHello";
import {render} from "lit-html";

beforeEach(() => {
  render('', document.body);
})

describe('sayHello', () => {
  it('says hello', () => {
    render(sayHello('world'), document.body);
    const component = document.querySelector('h1');
    expect(component).toBeDefined();
  })
})

我的"jest.config.js"是:

My "jest.config.js" is:

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'jsdom',
}

但是当我使用jest运行测试时,出现了这样的错误:

But when I run the tests with jest, I got such error:

FAIL  src/sayHello.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.

    By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".

    Here's what you can do:
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/en/configuration.html

    Details:

    /workspace/typescript-webpack-lit-html-jest-test-demo/node_modules/lit-html/lit-html.js:31
    import { defaultTemplateProcessor } from './lib/default-template-processor.js';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

    > 1 | import {html, TemplateResult} from 'lit-html';
        | ^
      2 |
      3 | export default function sayHello(name: string): TemplateResult {
      4 |   return html`<h1>Hello ${name}</h1>`;

      at Runtime._execModule (node_modules/jest-runtime/build/index.js:1166:56)
      at Object.<anonymous> (src/sayHello.ts:1:1)
      at Object.<anonymous> (src/sayHello.test.ts:1:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.339 s
Ran all test suites.
error Command failed with exit code 1.

原因应该是"node_modules"中的文件.node_modules/lit-html/lit-html.js使用es模块,并且开玩笑不能很好地处理它.

The reason should be the file in "node_modules" node_modules/lit-html/lit-html.js uses es module, and jest can't handle it well.

我尝试了各种配置,但仍然无法解决,需要您的帮助,谢谢.

I tried all kinds of configurations, but still can't fix it, need your help, thanks.

针对此问题的小型完整演示项目: https://github.com/freewind-demos/typescript-webpack-lit-html-jest-test-demo

A small and complete demo project for this issue: https://github.com/freewind-demos/typescript-webpack-lit-html-jest-test-demo

推荐答案

默认情况下,Jest不会转换/node_modules/(文档).为了使其正常工作,您可以像这样更改配置

By default Jest doesn't transforms the /node_modules/ (docs). In order to make it work you can change your configuration like this

module.exports = {
  preset: "ts-jest",
  testEnvironment: "jsdom",
  transform: {
    // transform files with ts-jest
    "^.+\\.(js|ts)$": "ts-jest",
  },
  transformIgnorePatterns: [
    // allow lit-html transformation
    "node_modules/(?!lit-html)",
  ],
  globals: {
    "ts-jest": {
      tsConfig: {
        // allow js in typescript
        allowJs: true,
      },
    },
  },
};

这篇关于"SyntaxError:无法在模块外部使用import语句"使用带有lit-html的打字稿编写测试时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 11:16