我试图 Package typescript应用程序(灯塔+ puppet )在jest.我创建了jest环境,安装和拆卸就像是在documentation .从安装的所有步骤工作正常,我看到的结果在控制台,但当我试图导入__tests__/test1.test.ts
内startFlow
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";
| ^
型
我的test1.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导入startFlow之前,它在导入模块方面没有问题。
我试图添加额外的babel转换到jest.config,但它没有帮助。
1条答案
按热度按时间bweufnob1#
出现问题是因为我没有在
--experimental-vm-modules
模式下运行它。当我以这种方式运行测试时,问题不会发生:node --experimental-vm-modules --experimental-loader ts-node/esm node_modules/jest/bin/jest.js