3
votes

I'm writing tests in typescript, and i've got private dependencies in node_modules/@my-modules folder. I'm using ts-jest compiler and jest still complains about esnext modules in node_modules folder.

package versions:

"@types/jest": "^23.3.12",
"jest": "^23.6.0",
"ts-jest": "^23.10.5",

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "outDir": "build/dist",
    "module": "commonjs",
    "target": "es5",
    "lib": ["es5", "es6", "dom", "esnext"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": false,
    "strict": false,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

jest.config.js:

module.exports = {
  globals: {
    "ts-jest": {
      diagnostics: true,
      tsConfig: "<rootDir>/tsconfig.json",
    },
  },
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  preset: "ts-jest",
  roots: ["<rootDir>/src/"],
  setupTestFrameworkScriptFile: "<rootDir>/src/setupEnzyme.ts",
  snapshotSerializers: ["enzyme-to-json/serializer"],
  testEnvironment: "jest-environment-jsdom",
  transformIgnorePatterns: [
    "node_modules/(?!(@my-modules)/)",
  ]
};

exact error when running jest:

 Details:

    /sites/frontend/node_modules/@my-modules/ui-components/.dist/src/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default as Calendar } from './components/calendar';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

What is the proper way to transpile files in @my-modules for jest?

2
alright I solved it finally. will share the answer little bit laterVictor L.
How did you manage to solve it?Salman Hasrat Khan

2 Answers

1
votes

The config that works for me right now

package versions:

"@types/jest": "^24.0.17",
"jest": "^24.8.0",
"ts-jest": "^23.10.5",

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "build/dist",
    "lib": ["es5", "es6", "dom", "esnext"],
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es5",
    "sourceMap": true,
    "allowJs": true,
    "jsx": "react",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  },
  "exclude": [
    "node_modules",
    "build",
    "dist"
  ]
}

jest.config.js:

module.exports = {
  globals: {
    "ts-jest": {
      diagnostics: true,
      tsConfig: "<rootDir>/tsconfig.json",
    },
  },
  moduleDirectories: [
    "node_modules",
    "utils",
    __dirname,
  ],
  moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json"],
  moduleNameMapper: {
    "\\.(css|less)$": "identity-obj-proxy",
    "\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
      "<rootDir>/config/jest/fileMock.js",
  },
  preset: "ts-jest",
  roots: ["<rootDir>/src/"],
  setupFilesAfterEnv: ["<rootDir>/config/jest/setupJest.ts"],
  snapshotSerializers: ["enzyme-to-json/serializer"],
  testEnvironment: "jest-environment-jsdom",
  transform: {
    "^.+\\.jsx?$": "babel-jest",
    "^.+\\.tsx?$": "ts-jest",
  },
  transformIgnorePatterns: [
    "node_modules/(?!(@my-modules)/)",
  ],
};

config/jest/fileMock.js:

module.exports = 'test-file-stub';
0
votes

I followed the tuto here and worked like a charm

  • npm install --save-dev jest typescript ts-jest @types/jest
  • npx ts-jest config:init

Another solution for me was to update the jest.config.ts so it contains the following setting

transform: {
    //"^.+\\.(ts|tsx)$": "ts-jest",
  },