Search code examples
reactjstypescriptjestjsenzyme

TypeError: plugin.test is not a function when using jest, enzyme and react-16, typescript


I'm in the process of updating my react app to react-16, but all my tests brake. I have upgraded enzyme, jest-enzyme, react-dom, installed the enzyme-adapter , created the setupTest.ts enzyme file, updated my jestconfig.js file but I'm still getting the error. If anyone has an idea of what might be going wrong.

No dependency warnings (pertaining react|tests) when doing npm ls

Here is my jestconfig

{
  "moduleDirectories": [
    "node_modules",
    "src"
  ],
  "moduleFileExtensions": [
    "ts",
    "tsx",
    "js"
  ],
  "moduleNameMapper": {
    "\\.(css|scss)$": "identity-obj-proxy"
  },
  "transform": {
    ".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
  },
  "testResultsProcessor": "<rootDir>/node_modules/ts-jest/coverageprocessor.js",
  "snapshotSerializers": [
    "enzyme-to-json"
  ],
  "setupTestFrameworkScriptFile":"<rootDir>/config/setupTests.ts",
  "coveragePathIgnorePatterns": ["node_modules", "__tests__"],
  "testRegex": "__tests__/.*\\.test\\.(ts|tsx|js)$",
  "collectCoverageFrom": ["src/**/*.{ts,tsx}"],

...rest of code

src/setupTests.ts

import 'raf/polyfill'; // <= this removes the requestAnimationFrame warning error
import * as Enzyme from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

Solution

  • Was writing the question and the realised the error. Was missing the following on the jestconfig.json

    "snapshotSerializers": [
        "enzyme-to-json/serializer" // <= this part
      ],
    
    ..rest of code