2

我正在尝试用玩笑来模拟我的axios请求。这是我的测试:

// External imports
import axios, { AxiosPromise } from 'axios'

// Personal imports
import { getProgramApplications } from '#stores'
import { programItem } from '../fixtures/zanox'

describe('Zanox sales', async function () {
    describe('Get program applications', async function () {
        it('should get program applications', async function () {
            const program = programItem()
            const mock = jest.spyOn(axios, 'get')
            mock.mockReturnValueOnce({ data: [program] } as unknown as AxiosPromise<unknown>)
            getProgramApplications(process.env.ZANOX_ADSPACE!)
        })
    })
})

这是我的package.json

{
    "name": "zanox",
    "version": "1.0.0",
    "main": "index.js",
    "license": "MIT",
    "requireRewrite": {
        "map": [
            [
                "#database",
                "dist/src/setup/database"
            ],
            [
                "#models/*",
                "dist/src/models/*"
            ],
            [
                "#sales",
                "dist/src/sales/index"
            ],
            [
                "#stores",
                "dist/src/stores/index"
            ],
            [
                "#stores/",
                "dist/src/stores/"
            ],
            [
                "#utils",
                "dist/src/utils/index"
            ],
            [
                "#utils/",
                "dist/src/utils/"
            ],
            [
                "#zanox/",
                "dist/src/zanox/"
            ]
        ]
    },
    "scripts": {
        "build": "yarn run tsc",
        "clean": "rm -rf dist",
        "doc": "./node_modules/typedoc/bin/typedoc --out docs/",
        "lint": "tslint -c tslint.json -p tsconfig.json",
        "lint-fix": "tslint -c tslint.json -p tsconfig.json --fix",
        "test": "yarn clean && yarn run build && find dist/tests -name '*test.js' | xargs mocha --exit --timeout 10000 -R spec",
        "test-no-build": "find dist/tests -name '*test.js' | xargs mocha --exit --timeout 10000 -R spec",
        "watch": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"yarn run watch-ts\" \"yarn run serve\"",
        "watch-debug": "concurrently -k -p \"[{name}]\" -n \"TypeScript,Node\" -c \"cyan.bold,green.bold\" \"yarn run watch-ts\" \"npm run serve-debug\"",
        "watch-ts": "tsc -w"
    },
    "engines": {
        "node": ">=10.0",
        "yarn": ">=1.7.0"
    },
    "devDependencies": {
        "@types/axios": "^0.14.0",
        "@types/faker": "^4.1.5",
        "@types/jest": "^24.0.18",
        "@types/luxon": "^1.4.1",
        "@types/mocha": "^5.2.7",
        "@types/nock": "^10.0.3",
        "@types/node": "^12.6.8",
        "@types/pg": "^7.4.11",
        "@types/uuid": "^3.4.4",
        "@types/winston": "^2.4.4",
        "faker": "^4.1.0",
        "jest": "^24.9.0",
        "mocha": "^6.2.0",
        "nock": "^10.0.6",
        "prettier": "^1.15.3",
        "ts-jest": "^24.0.2",
        "ttypescript": "^1.5.5",
        "typescript": "^3.5.3"
    },
    "dependencies": {
        "@types/dotenv-safe": "^5.0.3",
        "@types/source-map-support": "^0.5.0",
        "aws-sdk": "^2.401.0",
        "axios": "^0.18.0",
        "db-migrate": "^0.11.5",
        "db-migrate-pg": "^0.4.0",
        "dotenv": "^6.2.0",
        "dotenv-safe": "^6.1.0",
        "io-ts": "^1.6.1",
        "js-joda": "^1.9.2",
        "luxon": "^1.10.0",
        "pg": "^7.8.0",
        "require-rewrite": "^1.0.2",
        "source-map-support": "^0.5.10",
        "tslint": "^5.18.0",
        "tslint-eslint-rules": "^5.4.0",
        "typedoc": "^0.15.0",
        "utility-types": "^3.7.0",
        "uuid": "^3.3.2"
    }
}

最后,这是我的tsconfig.json

{
    "compilerOptions": {
        "baseUrl": ".",
        "diagnostics": false,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "module": "CommonJS",
        "moduleResolution": "node",
        "noErrorTruncation": true,
        "noFallthroughCasesInSwitch": true,
        "noImplicitReturns": false,
        "noUnusedLocals": true,
        "noUnusedParameters": false,
        "outDir": "./dist/",
        "resolveJsonModule": true,
        "sourceMap": true,
        "strict": true,
        "target": "es6",
        "lib": [
            "es5",
            "es6",
            "dom",
            "es2017"
        ],
        "paths": {
            "#database": [
                "src/setup/database"
            ],
            "#models/*": [
                "src/models/*"
            ],
            "#sales": [
                "src/sales/index"
            ],
            "#stores": [
                "src/stores/index"
            ],
            "#stores/*": [
                "src/stores/*"
            ],
            "#utils": [
                "src/utils/index"
            ],
            "#utils/*": [
                "src/utils/*"
            ],
            "#zanox/*": [
                "src/zanox/*"
            ],
            "*": [
                "node_modules/*",
                "src/_vendor/*"
            ]
        }
    },
    "include": [
        "src/**/*",
        "tests/**/*"
    ]
}

问题是:当我尝试编译我的项目时,终端说:

$ /home/olegario/hotsites/monorepo-hotsites/zanox/node_modules/.bin/tsc
node_modules/@types/mocha/index.d.ts:2680:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'beforeEach' must be of type 'Lifecycle', but here has type 'HookFunction'.

2680 declare var beforeEach: Mocha.HookFunction;
                 ~~~~~~~~~~

  node_modules/@types/jest/index.d.ts:32:13
    32 declare var beforeEach: jest.Lifecycle;
                   ~~~~~~~~~~
    'beforeEach' was also declared here.

node_modules/@types/mocha/index.d.ts:2698:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'afterEach' must be of type 'Lifecycle', but here has type 'HookFunction'.

2698 declare var afterEach: Mocha.HookFunction;
                 ~~~~~~~~~

  node_modules/@types/jest/index.d.ts:34:13
    34 declare var afterEach: jest.Lifecycle;
                   ~~~~~~~~~
    'afterEach' was also declared here.

node_modules/@types/mocha/index.d.ts:2714:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'describe' must be of type 'Describe', but here has type 'SuiteFunction'.

2714 declare var describe: Mocha.SuiteFunction;
                 ~~~~~~~~

  node_modules/@types/jest/index.d.ts:35:13
    35 declare var describe: jest.Describe;
                   ~~~~~~~~
    'describe' was also declared here.

node_modules/@types/mocha/index.d.ts:2735:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xdescribe' must be of type 'Describe', but here has type 'PendingSuiteFunction'.

2735 declare var xdescribe: Mocha.PendingSuiteFunction;
                 ~~~~~~~~~

  node_modules/@types/jest/index.d.ts:37:13
    37 declare var xdescribe: jest.Describe;
                   ~~~~~~~~~
    'xdescribe' was also declared here.

node_modules/@types/mocha/index.d.ts:2749:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'it' must be of type 'It', but here has type 'TestFunction'.

2749 declare var it: Mocha.TestFunction;
                 ~~

  node_modules/@types/jest/index.d.ts:38:13
    38 declare var it: jest.It;
                   ~~
    'it' was also declared here.

node_modules/@types/mocha/index.d.ts:2763:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'test' must be of type 'It', but here has type 'TestFunction'.

2763 declare var test: Mocha.TestFunction;
                 ~~~~

  node_modules/@types/jest/index.d.ts:41:13
    41 declare var test: jest.It;
                   ~~~~
    'test' was also declared here.

node_modules/@types/mocha/index.d.ts:2770:13 - error TS2403: Subsequent variable declarations must have the same type.  Variable 'xit' must be of type 'It', but here has type 'PendingTestFunction'.

2770 declare var xit: Mocha.PendingTestFunction;
                 ~~~

  node_modules/@types/jest/index.d.ts:40:13
    40 declare var xit: jest.It;
                   ~~~
    'xit' was also declared here.


Found 7 errors.

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

我发现的大部分讨论都说升级typescript版本并jasmine从项目中删除。如您所见,jasmine我的项目中没有,而且我的typescript版本是最新版本。为什么会这样?

4

1 回答 1

7

一种解决方案是跳过检查来自外部项目的类型。在你的tsconfig.json

{
    "compilerOptions": {
        ....
        "skipLibCheck": true
    }
}

在此处查看所有编译器选项:https ://www.typescriptlang.org/docs/handbook/compiler-options.html

于 2019-08-21T23:14:40.030 回答