当使用TypeScript和lit-html编写测试时,出现"SyntaxError: Cannot use import statement outside a module"的错误。

8
我使用 TypeScript 编写了一个简单的示例,其中涉及到了 lit-html:

lit-html

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

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

使用Jest编写一些简单的测试:

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" 如下:

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

然而,当我使用jest运行测试时,出现了以下错误:

FAIL  src/sayHello.test.tsTest 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 模块,在 Jest 中不能很好地处理。

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

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

1个回答

12

默认情况下,Jest不会转换/node_modules/文件夹(文档)。为了让它正常工作,你可以像这样更改你的配置

Lit 2.0

还有@lit命名空间的软件包也需要进行转换:

  • @lit/reactive-element

所以在正则表达式中添加可选的@就可以解决问题了

jest.config.js

module.exports = {
  // ...
  transformIgnorePatterns: ["node_modules/(?!\@?lit)"],
}

有效的GitHub设置


ts-jest >27.x

配置:删除tsConfig选项的支持(#2127) (3cc9b80)

应该使用小写的tsconfig,而不是tsConfig

jest.config.js

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,
      },
    },
  },
};

使用 "ts-jest": "27.0.3" 版本的 GitHub 设置


ts-jest <27.x

jest.config.js

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,
      },
    },
  },
};

解决方案分支的工作GitHub设置


1
谢谢你!不幸的是,它看起来像是 TypeScript 4.3.3+ 的错误又出现了。 - jhorback
@jhorback用最新版本的解决方案更新了我的答案。 - Teneff
1
无法在lit上运行。 - leonheess
嗨@Teneff,在您的存储库中,您正在使用已弃用的lit-element。 您可以遵循https://lit.dev/docs/releases/upgrade/进行升级。 - leonheess
@leonheess 看一下the lit branch - Teneff
只有在我执行了以下操作后才能正常工作:transformIgnorePatterns: [\/node_modules/(?!${['lit-element', 'lit-html', 'lit', '@lit'].join('|')})`]`,但还是感谢你的努力。 - leonheess

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接