【问题标题】:Jest calls async jest.config.ts twiceJest 两次调用 async jest.config.ts
【发布时间】:2021-12-19 18:10:19
【问题描述】:

在我的包含 Puppeteer UI 测试的 Typescript 存储库中,我有一个最近更新为导出异步对象的 jest.config.ts 文件,因为我们必须进行 api 调用来获取需要放入测试报告中的信息,使用开玩笑的reporters 属性

注意到由于这是一个异步导出,jest 开始执行 jest.config.ts 两次。因此,api 调用的次数是必要的两倍。非异步时不会发生这种情况。

这可能是一个错误或我缺少的东西吗?我的猜测是 jest.config.ts 第一次执行全局配置,第二次执行项目配置,但这仅在其异步时发生。

这是我的 jest.config.ts 文件:

import type { Config } from '@jest/types';
import BitBucketAPI from './BitBucketAPI';

export default async (): Promise<Config.InitialOptions> => {
    return {
        verbose: true,
        bail: false,
        maxWorkers: 4,
        forceExit: true,
        preset: 'ts-jest',
        testEnvironment: 'node',
        testSequencer: './test-sequencer.ts',
        testRegex: '((\\.|/)(spec))\\.(ts)$',
        reporters: [
            'default',
            [
                'jest-html-reporters',
                {
                    publicPath: './test-reports',
                    filename: 'main.html',
                    pageTitle: 'Test Report',
                    customInfos: [
                        {
                            title: 'Environment URL',
                            value: 'Test',
                        },
                          await new BitBucketAPI().retrieveCommitInformation().then(commitInformation => {
                                return {
                                    title: 'BitBucket data',
                                    value: commitInformation,
                                };
                            }) : {}
                    ],
                }
            ]
        ],
    };
};

【问题讨论】:

    标签: typescript jestjs ts-jest jest-puppeteer


    【解决方案1】:

    我遇到了同样的问题并遇到了您的帖子。 我能够解决它,所以它不会通过直接调用函数来调用函数两次,如下所示:

    export default (async (): Promise<Config.InitialOptions> => {
      //something here
    })();
    

    这已经帮我解决了,希望对你有帮助!

    【讨论】:

    • 导出为匿名函数解决了这个问题。谢谢@Steinhard
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-09
    • 2021-10-27
    • 2020-10-12
    • 1970-01-01
    • 2021-05-15
    • 2022-12-09
    • 2016-10-17
    相关资源
    最近更新 更多