0
votes

I am using :

  • vscode v1.49.2
  • jest runner v0.4.24 extension
  • node : 14.10.1
  • win10 : 64 bits
"devDependencies": {
        "@types/jest": "24.0.23",
        "@types/jest-when": "2.7.0",
        "jest": "24.9.0",
        "jest-cli": "24.9.0",
        "jest-coverage-badges": "1.1.2",
        "jest-create-mock-instance": "1.1.0",
        "jest-html-reporter": "^3.1.2",
        "jest-jenkins-reporter": "1.0.2",
        "jest-junit-reporter": "1.1.0",
        "jest-when": "2.7.0",
        "typescript": "^3.8.3"
    },

The issue : Jest can't resolve relative path

Cannot find module '../../common/utility/json.utility' from 'command.test.ts'

1 | import { JsonUtility } from '../../common/utility/json.utility'; | ^

at Resolver.resolveModule (node_modules/jest-resolve/build/index.js:259:17) at Object. (test/command/poll/command.test.ts:1:1)

My configs :

**jest.config.js : ** The details

module.exports = {
    verbose: true,
    transform: {
        '^.+\\.tsx?$': 'ts-jest'
    },
    testEnvironment: 'node',
    testRegex: '(/test/.*|(\\.|/)(test|spec))(\\.it)?\\.(jsx?|tsx?)$',
    testPathIgnorePatterns: ['/coverage', '/lib', '/test/fixture'],
    testResultsProcessor: './node_modules/jest-junit-reporter',
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    coverageThreshold: {
        global: {
            branches: 15, // 80
            functions: 15, // 80
            lines: 15, // 80
            statements: 0 // 20
        }
    },
    collectCoverageFrom: ['src/**/*.ts', '!src/index.ts'],
    coverageReporters: ['json-summary', 'text', 'lcov']
};

tsconfig.json : The details of the tsconfig

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "lib": [
            "es6",
            "esnext"
        ],
        "declaration": true,
        "sourceMap": true,
        "outDir": "lib",
        "rootDirs": [
            "src",
            "test"
        ],
        "removeComments": true,
        "strict": true,
        "noImplicitAny": true,
        "moduleResolution": "node",
        "strictNullChecks": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    },
    "exclude": [
        "typedoc",
        "**/*.d.ts",
        "node_modules",
        "**/node_modules/*",
        "lib"
    ]
}

Best Regards

1
After some investigations : Path created by 'quick fix" import { BootstrapService } from '../../core/service/bootstrap.service'; Path expected by 'jest' import { BootstrapService } from '../../../src/core/service/bootstrap.service'; Regarding all the files in the 'test' folder, no issue for that. BR - Y. Boujraf

1 Answers

0
votes

You may want to configure your module paths in jest: https://jestjs.io/docs/en/configuration#modulepaths-arraystring

Something like:

module.exports = {
    verbose: true,
    transform: {
        '^.+\\.tsx?$': 'ts-jest'
    },
    testEnvironment: 'node',
    testRegex: '(/test/.*|(\\.|/)(test|spec))(\\.it)?\\.(jsx?|tsx?)$',
    testPathIgnorePatterns: ['/coverage', '/lib', '/test/fixture'],
    testResultsProcessor: './node_modules/jest-junit-reporter',
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
    coverageThreshold: {
        global: {
            branches: 15, // 80
            functions: 15, // 80
            lines: 15, // 80
            statements: 0 // 20
        }
    },
    collectCoverageFrom: ['src/**/*.ts', '!src/index.ts'],
    coverageReporters: ['json-summary', 'text', 'lcov'],
    modulePaths: [ // Add your paths here
        "<rootDir>/src/"
    ],
};