Skip to content

Can I use "synthetic default imports" in TypeScript with Wallaby and Jest? #1181

@chris-nissen

Description

@chris-nissen

I'm finding that my tests won't work in Wallaby if I use module imports like import React from 'react';. I have to use import * as React from 'react';. The former does work when I run the tests in Jest. Is there a way I can tweak my config to make this work?

Wallaby.js configuration file

module.exports = function (wallaby) {
   return {
      files: [
	      { pattern: 'package.json', instrument: false }, // since Jest config is in there
	      { pattern: 'ClientApp/**/*.ts?(x)' },
	      { pattern: 'ClientApp/**/*.spec.ts?(x)', ignore: true },
          { pattern: 'ClientApp/**/*.snap' }
      ],

      filesWithNoCoverageCalculated: [
	      'ClientApp/configureStore.ts',
	      'ClientApp/boot-client.tsx',
	      'ClientApp/boot-server.tsx'
      ],

      tests: [
	      { pattern: 'ClientApp/**/*.spec.ts?(x)' }
      ],

      testFramework: 'jest',
      env: {
         // Required for Jest running
         type: 'node',
         runner: 'node'
      },

      hints: {
         ignoreCoverage: / exclude from coverage /
      },

      debug: true
   };
};

tsconfig

{
  "compilerOptions": {
    "baseUrl": ".",
    "moduleResolution": "node",
    "target": "es5",
    "jsx": "react",
    "experimentalDecorators": true,
    "sourceMap": true,
    "skipDefaultLibCheck": true,
    "strictNullChecks": true,
    "noImplicitAny": true,
    "allowSyntheticDefaultImports": true,
    "noEmit": true, // So that VS doesn't write .js files when it compiles the TS files
    "lib": [ "es6", "dom" ],
    "paths": {
      "*": [
        "ClientApp/*"
      ],
      // Fix "Duplicate identifier" errors caused by multiple dependencies fetching their own copies of type definitions.
      // We tell TypeScript which type definitions module to treat as the canonical one (instead of combining all of them).
      "history": [ "./node_modules/@types/history/index" ],
      "redux": [ "./node_modules/@types/redux/index" ],
      "react": [ "./node_modules/@types/react/index" ]
    },
    "typeRoots": [
      "./node_modules/@types"
    ]
  },
  "exclude": [
    "bin",
    "node_modules"
  ]
}

Jest config in package.json

 "jest": {
      "testResultsProcessor": "./node_modules/jest-junit-reporter",
      "transform": {
         ".(ts|tsx)": "./node_modules/ts-jest/preprocessor.js"
      },
      "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js|jsx)$",
      "moduleDirectories": [
         "node_modules",
         "./ClientApp"
      ],
      "moduleFileExtensions": [
         "ts",
         "tsx",
         "js",
         "jsx"
      ],
      "globals": {
         "__TS_CONFIG__": {
            "allowSyntheticDefaultImports": true,
            "module": "commonjs",
            "jsx": "react",
            "baseUrl": ".",
            "moduleResolution": "node",
            "typeRoots": [
               "./node_modules/@types"
            ],
            "paths": {
               "*": [
                  "*",
                  "ClientApp/*"
               ]
            }
         }
      }
   }

Code editor or IDE name and version

Visual Studio 2017

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions