最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Typescript - I can't compile project with jest, @typesjest and ts-jest - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use jest to mock my axios requests. This is my test:

// 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!)
        })
    })
})

This is my 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"
    }
}

And finally, this is my tsconfig.json:

{
    "pilerOptions": {
        "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/**/*"
    ]
}

The problem is: when I try to pile my project, the terminal says:

$ /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  for documentation about this mand.

Most part of the discussions I found about it said to upgrade the typescript version and to remove the jasmine from the project. As you can see, there is no jasmine in my project and also my typescript version is the most recently one. Why this is happening?

I'm trying to use jest to mock my axios requests. This is my test:

// 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!)
        })
    })
})

This is my 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"
    }
}

And finally, this is my tsconfig.json:

{
    "pilerOptions": {
        "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/**/*"
    ]
}

The problem is: when I try to pile my project, the terminal says:

$ /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./en/docs/cli/run for documentation about this mand.

Most part of the discussions I found about it said to upgrade the typescript version and to remove the jasmine from the project. As you can see, there is no jasmine in my project and also my typescript version is the most recently one. Why this is happening?

Share Improve this question asked Aug 21, 2019 at 20:34 olegarioolegario 7422 gold badges17 silver badges37 bronze badges 1
  • Does this answer your question? TypeScript error TS2403: Subsequent variable declarations must have the same type – leonheess Commented Apr 14, 2022 at 14:26
Add a ment  | 

2 Answers 2

Reset to default 9

One solution is to skip checking typings from outside projects. In your tsconfig.json:

{
    "pilerOptions": {
        ....
        "skipLibCheck": true
    }
}

See all piler options here: https://www.typescriptlang/docs/handbook/piler-options.html

One of the reasons is that you have types/jest and types/mocha installed in devDependencies. removing mocha should fix errors like 'also declared here'.

npm uninstall --save-dev @types/mocha

发布评论

评论列表(0)

  1. 暂无评论