尝试从 lighthouse 库导入 startFlow 函数时出现“Jest 无法解析文件”

问题描述 投票:0回答:1

我尝试用玩笑包装打字稿应用程序(灯塔+木偶师)。我创建了 jest 环境、设置和拆卸,就像在文档中一样。设置中的所有步骤都工作正常,我在控制台中看到结果,但是当我尝试在

startFlow
 内导入 
__tests__/test1.test.ts

import { UserFlow, startFlow } from "lighthouse";

我收到错误

Jest encountered an unexpected token                                                                                                                  
                                                                                                                                                          
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.                                                                                                                                    
                                                                                                                                                          
Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

By default "node_modules" folder is ignored by transformers.

Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • 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/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

Details:

    C:\Users\malgorzata.kowal\Accenture Project\PMI\dce20-pmi-automation-performance-tool\node_modules\lighthouse\core\index.js:7
    import Trace from './gather/gatherers/trace.js';
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { afterAll, beforeAll, describe, test } from "@jest/globals";
      2 | import * as fs from "fs";
    > 3 | import { UserFlow, startFlow } from "lighthouse";
        | ^

我的测试1.test.ts

import { afterAll, beforeAll, describe, test } from "@jest/globals";
import * as fs from "fs";
import { UserFlow, startFlow } from "lighthouse";
import { Page } from "puppeteer";
import appConfig from "../config.ts";

describe("e2e lighthouse tests", () => {
  const customTimeout = 200_000;
  let page: Page;
  let flow: UserFlow;

  beforeAll(async () => {
    page = await globalThis.__BROWSER_GLOBAL__.newPage();
    flow = await startFlow(page, { name: "Test1" });
  });

  afterAll(async () => {
    await page.close();
    fs.writeFileSync(
      `${appConfig.directories.reports}/test1.html`,
      await flow.generateReport(),
    );
    fs.writeFileSync(
      `${appConfig.directories.reports}/test1.json`,
      JSON.stringify(await flow.createFlowResult(), null, 2),
    );
  });

  test(
    "test onet: test2 ",
    async () => {
      await page.goto("https://www.onet.pl/");
    },
    customTimeout,
  );
});

puppeteer-environment.ts

import { TestEnvironment as NodeEnvironment } from "jest-environment-node";

import { EnvironmentContext, JestEnvironmentConfig } from "@jest/environment";

import { Browser } from "puppeteer";
import PuppeteerDriver from "./puppeter.ts";

class PuppeteerEnvironment extends NodeEnvironment {
  puppeteerDriver = PuppeteerDriver;

  constructor(config: JestEnvironmentConfig, context: EnvironmentContext) {
    super(config, context);
  }

  async setup() {
    await super.setup();
    const browser = await this.puppeteerDriver.setup();
    this.global.__BROWSER_GLOBAL__ = browser;
  }

  async teardown() {
    (this.global.__BROWSER_GLOBAL__ as Browser).close();
    await super.teardown();
  }

  getVmContext() {
    return super.getVmContext();
  }
}

export default PuppeteerEnvironment;

jest.config.ts

import type { Config } from "jest";
const config: Config = {
  preset: "ts-jest",
  testEnvironment: "./src/puppeteer-environment.ts",
  extensionsToTreatAsEsm: [".ts", ".mts"],
  transform: {
    "^.+\\.(ts|mts)?$": [
      "ts-jest",
      {
        useESM: true,
      },
    ],
  },
  globalSetup: "./src/setup.js",
  globalTeardown: "./src/teardown.js",
  transformIgnorePatterns: ["node_modules/*"],
  moduleFileExtensions: ["ts", "js", "json", "node"],
};

export default config;

我运行玩笑测试

node --loader ts-node/esm node_modules/jest/bin/jest.js

并且在从 lighthouse 启动 Flow 之前导入模块不会出现问题。

我尝试向 jest.config 添加额外的 babel 转换,但没有帮助。

typescript jestjs puppeteer es6-modules lighthouse
1个回答
0
投票

出现这个问题是因为我没有在

--experimental-vm-modules
模式下运行它。当我这样运行测试时,不会出现问题:
node --experimental-vm-modules --experimental-loader ts-node/esm node_modules/jest/bin/jest.js

© www.soinside.com 2019 - 2024. All rights reserved.