Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'name' of undefined testing serialize #100

Closed
AMagistroni opened this issue Sep 22, 2020 · 8 comments
Closed

Cannot read property 'name' of undefined testing serialize #100

AMagistroni opened this issue Sep 22, 2020 · 8 comments

Comments

@AMagistroni
Copy link

AMagistroni commented Sep 22, 2020

When I try to test a component that use serialize, I found this error

Cannot read property 'name' of undefined

Using the application it's work.
I use Jest, here the test(simplified, without component)

@Serializable()
export class ClassToTest {
  @JsonProperty() num: number;

  constructor() {}
}

describe('test', () => {

  it('test serialization', () => {
    debugger;
    const obj = new ClassToTest ();
    obj.num= 100;
    const serialized = serialize(obj); // here the error
    const deserialized = deserialize<ClassToTest >(serialized, ClassToTest );
    expect(deserialized .num).toBe(100);
  });

the tsconfig.json


{
  "compilerOptions": {
    "outDir": "../out-tsc/spec",
    "types": [
      "jest",
      "node"
    ],
    "module": "commonjs",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "allowJs": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "target": "es5"
  },
  "files": [
    "polyfills.ts"
  ],
  "include": [
    "**/*.spec.ts",
    "**/*.d.ts"
  ]
}
@GillianPerard
Copy link
Owner

Hi, let me check.

@GillianPerard GillianPerard added the inspecting Time to investigate the issue label Sep 23, 2020
@GillianPerard
Copy link
Owner

GillianPerard commented Sep 25, 2020

I tested with a little sample and your tsconfig.json and your code and with this package.json and jest.config.js it works properly.

Could you test with the following packages/file please 😊

package.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "test": "jest"
  },
  "dependencies": {
    "@types/jest": "^26.0.14",
    "@types/node": "^14.11.2",
    "jest": "^26.4.2",
    "jest-junit": "^11.1.0",
    "ts-jest": "^26.4.0",
    "ts-node": "^9.0.0",
    "typescript": "^4.0.3",
    "typescript-json-serializer": "^2.4.0"
  }
}

jest.config.js

module.exports = {
  coverageReporters: ["text", "lcov"],
  reporters: ["default", "jest-junit"],
  roots: ["spec"],
  transform: {
    "^.+\\.tsx?$": "ts-jest",
  },
};

PS: my test file is in a spec folder and call index.spec.ts.

@AMagistroni
Copy link
Author

SerializeTest.zip
I've tried but with the same result, so I've created another sample project, and the error is the same.
The code is uploaded. Have you the same result with the code uploaded?

@GillianPerard
Copy link
Owner

Ok, I'll test soon.

@GillianPerard
Copy link
Owner

Hi, sorry to be late. I tested your code and it seems that ReflectMetadata package I used in my lib is not able to find the type/prototype of any value 😕

I think is due to the project technology, I can't do anything for you...

@GillianPerard GillianPerard removed the inspecting Time to investigate the issue label Oct 7, 2020
@bakatrouble
Copy link

bakatrouble commented Oct 8, 2020

Got the same issue with create-react-app project

Solved it using following .babelrc (overrided default settings using @jackwilsdon/craco-use-babelrc):

{
  "presets": ["react-app"],
  "plugins": [
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { "legacy": true }],
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ]
}

Setting in tsconfig.json:

"emitDecoratorMetadata": true,
    "experimentalDecorators": true,

And adding import "reflect-metadata"; line at the top of index.tsx

rbuckton/reflect-metadata#104

tl;dr: altering babel settings using 3rdparty plugin is required, by default it does not persist metadata.

@AMagistroni
Copy link
Author

I've solved the problem inserting in the package.json this

"jest": {
    "setupFilesAfterEnv": [
      "<rootDir>/src/setup-jest.ts"
    ],    
  }

I think that also setupFiles work

and
import 'reflect-metadata';

at the top of setup-jest.ts

Thank you @bakatrouble for your suggestion

@GillianPerard
Copy link
Owner

It's great that you can continue to use my lib!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants