mercredi 18 mars 2020

Jest TypeError "class import" is not a constructor

A dependency class: dependency.js

export default class Dependency {
  constructor() {}

  getSomething() {
    return "something"
  }
}

A client module: module.js

import Dependency from 'denpendency.js'

const dependencyInstance = new Dependency()

export const Service1 = {
  function1() {
    return dependencyInstance.getSomething()
  },
  function2() {
    return dependencyInstance.getSomething()
  }
}

A test for client module: module.spec.js

import { Service1 } from 'module.js'

describe('module test', () => {
  it('should return something', () => {
    const value = Service1.function1()
    expect(value).toEqual('something')
  })
})

This test fails to run with:

TypeError: _dependency.default is not a constructor

Tests started to fail when I refactored dependency to be a class, before was a plain module with functions.

Also tests that do not import module.js directly neither dependency.js but have dependency.js in the module resolution chain fails.

App works perfectly but test are failing. I do not know why. I have tried to mock that dependency with no success. But I think the problem arises in the module resolution. When importing any file that ends up importing module.js

Aucun commentaire:

Enregistrer un commentaire