0

enter image description here

When I write jest test case, it throw error. my project used vant UI, but when I not use vant UI test,it's run well. I looked for the answer on the Internet, but I couldn't do it all the time。 Has anyone solved similar problems?

babel.config.js:

module.exports = {
    presets: [
        "@vue/cli-plugin-babel/preset",
    ],
    plugins: [
        [
            "import",
            {
                libraryName: "vant",
                libraryDirectory: "es",
                style: (name) => `${name}/style/less`,
            },
            "vant",
        ],
    ],
    sourceMaps: true,
    env: {
        test: {
            presets: [
                ['@vue/cli-plugin-babel/preset'],
                [
                    '@babel/preset-env',
                    {
                        targets: {
                            node: 'current',
                        },
                    },
                ],
            ],
            plugins: [
                [
                    "import",
                    {
                        libraryName: "vant",
                        libraryDirectory: "es",
                        style: (name) => {
                            return `${name}/style/less`
                        },
                    },
                    "vant",
                ],
            ],
        },
    },
};

jest.config.js:

module.exports = {
    preset: "@vue/cli-plugin-unit-jest/presets/typescript-and-babel",
    transform: {
        ".*\\.(tsx)$": "ts-jest",
        "^.+\\.js": "babel-jest",
        "^.+\\.vue$": "vue-jest",
        "^.+\\.js$": "<rootDir>/node_modules/babel-jest",
    },
    moduleNameMapper: {
        "^@/(.*)$": "<rootDir>/src/$1",
    },
    moduleFileExtensions: [
        "js",
        "json",
        "ts",
        "tsx",
        "vue"
    ],
    // collectCoverage
    // moduleNameMapper: {
    //     // "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js",
    //     "\\.(css|less)$": "identity-obj-proxy"
    // }
};

what errors enter image description here

  • 1
    Does this answer your question? [SyntaxError: Cannot use import statement outside a module](https://stackoverflow.com/questions/58384179/syntaxerror-cannot-use-import-statement-outside-a-module) – Sumurai8 Aug 07 '21 at 12:29

0 Answers0