0

请帮我解决这个问题,非常感谢。

我正在尝试向我的项目添加测试:

  • 打字稿文件=>成功
  • vue 文件(没有打字稿)=> 成功
  • vue 文件(带有打字稿)=> 失败

测试.test.ts

import { shallowMount } from '@vue/test-utils';
import sum from '../TestTs';
import VueWithJs from '../VueWithJs.vue';
import VueWithTs from '../VueWithTs.vue';

describe('Test Jest with Vue and TypeScript', () => {

    test('test ts file', () => {

        const result = sum( 1, 1 );

        expect(
            result
        )
        .toBe(2);
    });

    test('test vue with js', () => {
        const title: string = 'test';

        const wrapper = shallowMount(VueWithJs);

        expect(
            wrapper.text()
        )
        .toMatch(title);
    });
    
    test('test vue with ts', () => {
        const title: string = 'test';

        const wrapper = shallowMount(VueWithTs);

        expect(
            wrapper.text()
        )
        .toMatch(title);
    });

});

Test.ts // 成功

function sum( a: number, b:number ): number {
    return a + b;
}

export default sum;

VueWithJs.vue // 成功

<template>
    <div>
        test
    </div>
</template>

<script>
export default {
    data() {
        const data = {
            test: 'test'
        };
        return data;
    }
};
</script>

VueWithTs.vue // 失败

<template>
    <div>
        test
    </div>
</template>

<script lang="ts">
export default {
    data() {
        const data: {
            test: string
        } = {
            test: 'test'
        };
        return data;
    }
};
</script>

错误:

resources/js/vue/Jest/__tests__/Test.test.ts
  ● Test suite failed to run

    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.
     • 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:

    SyntaxError: Unexpected token } in JSON at position 2202
        at JSON.parse (<anonymous>)

      at parse (node_modules/tsconfig/src/tsconfig.ts:195:15)
      at readFileSync (node_modules/tsconfig/src/tsconfig.ts:181:10)
      at Object.loadSync (node_modules/tsconfig/src/tsconfig.ts:151:18)
      at find (node_modules/vue-jest/lib/load-typescript-config.js:33:39)
      at loadTypescriptConfig (node_modules/vue-jest/lib/load-typescript-config.js:73:26)
      at compileTypescript (node_modules/vue-jest/lib/compilers/typescript-compiler.js:9:20)
      at processScript (node_modules/vue-jest/lib/process.js:23:12)
      at Object.module.exports [as process] (node_modules/vue-jest/lib/process.js:42:18)
      at ScriptTransformer.transformSource (node_modules/@jest/transform/build/ScriptTransformer.js:612:31)

jest.config.js

module.exports = {
    moduleFileExtensions: [
        "js",
        "ts",
        "json",
        "vue"
    ],
    testEnvironment: "jsdom",
    transform: {
        ".*\\.(vue)$": "vue-jest",
        "^.+\\.tsx?$": "ts-jest",
        "^.+\\.js$": "babel-jest"
    },
    testURL: "http://localhost/",
    snapshotSerializers: [
        "jest-serializer-vue"
    ],
    moduleNameMapper: {
      '^@/(.*)$': '<rootDir>/resources/js/$1'
    },
}

包.json

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch:front": "cross-env compile=frontend mix watch",
        "watch:back": "cross-env compile=backend mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production",
        "test:clear": "jest --clearCache",
        "test": "jest --clearCache && jest --config jest.conf.js"
    },
    "dependencies": {
        "reflect-metadata": "^0.1.13",
        "vue": "^2.6.11",
        "vue-property-decorator": "^9.1.2",
        "vue-router": "^3.1.3",
        "vuex": "^3.1.1",
        "vuex-class": "^0.3.2",
        "vuex-module-decorators": "^1.0.1"
    },
    "devDependencies": {
        "@babel/preset-env": "^7.14.7",
        "@types/jest": "^26.0.24",
        "@vue/test-utils": "^1.2.1",
        "babel-core": "^7.0.0-bridge.0",
        "babel-jest": "^27.0.6",
        "jest": "^27.0.6",
        "jest-serializer-vue": "^2.0.2",
        "laravel-mix": "^6.0.6",
        "sass": "~1.32.13",
        "sass-loader": "^11.1.0",
        "style-loader": "^1.2.1",
        "ts-jest": "^27.0.3",
        "ts-loader": "^9.1.2",
        "typescript": "^4.2.4",
        "vue-jest": "^3.0.7",
        "vue-loader": "^15.9.7",
    }
}

babel.config.js

module.exports = {
    presets: [
        [
            "@babel/preset-env",
            {
                targets: {
                    node: 'current',
                },
            }
        ]
    ],
}
4

0 回答 0