extendable-immutable icon indicating copy to clipboard operation
extendable-immutable copied to clipboard

TypeError while testing extended Immutable.List.sort() with jest

Open alekseykarpenko opened this issue 8 years ago • 13 comments

Hi @philpl , thx for your great work on extendable-iummutable, i've liked it so much.

But now i'm faced with weird error while trying to test my extended class with Jest, saying:

TypeError: Class constructor Second cannot be invoked without 'new'

while using immutable.reify

Here is my code:

import { fromJS } from 'immutable'
import { List } from 'extendable-immutable'

const list = fromJS([
  {title: "A"},
  {title: "E"},
  {title: "B"},
  {title: "D"},
  {title: "C"},
])

const result = fromJS([
  {title: "A"},
  {title: "B"},
  {title: "C"},
  {title: "D"},
  {title: "E"},
])

class Second extends List {
  lol(){
    return 'LOL:)'
  }
}

const originalList = new List(list)
const secondList = new Second(list)

describe('original List', () => {
  it('should return sorted list', () => {
    expect(originalList.sort().toJS(), result)
  })
})

describe('extended List', () => {
  it('should return sorted list', () => {
    expect(secondList.sort().toJS(), result)
  })
  it('should say LOL!', () => {
    expect(secondList.lol(), "LOL:)")
  })
})

Could you please help me to deal with it?

alekseykarpenko avatar Sep 12 '17 16:09 alekseykarpenko

Can't manage to reproduce this: https://codesandbox.io/s/n05pjv292m

Can you provide a working reproduction please?

kitten avatar Sep 12 '17 21:09 kitten

@philpl i can't figure out how to setup Jest tests in codesandbox. Is it possible there?

If not, u could simply copy my code and run it with jest locally (my version included in react-scripts is 20.0.4 now)

alekseykarpenko avatar Sep 13 '17 07:09 alekseykarpenko

@alekseykarpenko yea, I don't think it's possible. But take a look at my codesandbox there and see if you can reproduce it with that one

kitten avatar Sep 13 '17 11:09 kitten

@philpl it's reproducable only with jest! it's pointless to try it on codesandbox if no jest there 🤔

alekseykarpenko avatar Sep 13 '17 12:09 alekseykarpenko

@alekseykarpenko Alright, can you try to create a minimal code snippet that reproduces the bug? Then I can try to narrow it down

kitten avatar Sep 13 '17 12:09 kitten

@philpl code snippet is mentioned above. U can setup a blank app with create-react-app, insert that code into any test file like example.test.js and run yarn test

alekseykarpenko avatar Sep 13 '17 12:09 alekseykarpenko

@alekseykarpenko It is not a minimal reproduction though. You can save me a lot of time by pushing your existing reproduction repo and/or minimising the reproduction above :wink:

Edit: To be a bit clearer on this, sorry, I'm not here to torment you 😆 but at this point you know the bug better than I do, so when you minimise and pin it down, it won't take you as long as it would take me

kitten avatar Sep 13 '17 12:09 kitten

@philpl np, will try to push example repo asap. im still a little bit newbie to github best practices)

alekseykarpenko avatar Sep 13 '17 12:09 alekseykarpenko

@philpl here you go. clone that repo, run yarn install and then yarn test

https://github.com/alekseykarpenko/extendable-immutable-jest-typeerror

alekseykarpenko avatar Sep 13 '17 13:09 alekseykarpenko

@philpl were you able to reproduce that error?

alekseykarpenko avatar Sep 15 '17 11:09 alekseykarpenko

+1 got this reproduced in similar situation, or we may be doing something wrong

webdevbyjoss avatar Sep 19 '17 15:09 webdevbyjoss

hey guys, still experiencing this exact issue. have you managed to find a solution for this? Otherwise extendable-immutable can't be used in anything that is planned to be covered with unit tests on Jest.

webdevbyjoss avatar Nov 03 '17 12:11 webdevbyjoss

I've figured this out.

Here's a small reproducible case:

const { OrderedMap } = require('extendable-immutable')

class Collection extends OrderedMap {}
const magic = new Collection()
magic.filter(x => true)

Run that with Node.js and you'll get the error TypeError: Class constructor Collection cannot be invoked without 'new'.

Compile it with Babel down to ES5 first and there will be no error. This library is not compatible with ES6 and above.

Empowerful avatar Jun 11 '19 22:06 Empowerful