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

TypeError: Invalid type requested to IoC container. Type is not defined. #77

Open
davidmoshal opened this issue Jan 14, 2021 · 1 comment

Comments

@davidmoshal
Copy link

davidmoshal commented Jan 14, 2021

Nice library, works great with VueJS components, but fails when the components is wrapped and mounted for testing!

ie: Given a service, a main.ts and an App.vue component, everything works fine:

  • service.ts
abstract class IService{}
class Service extends IService{}
  • main.ts
import "reflect-metadata";
import Vue from "vue";
import App from "./App.vue";
import { Container } from "typescript-ioc";
import { IService, Service } from "./service";

Container.bind(IService).to(Service)
new Vue({
  router,
  render: (h) => h(App),
}).$mount('#app');
  • App.vue
<template>
  <div>App</div>
</template>
<script lang="ts">
import { Component, Vue } from "vue-property-decorator";
import { Inject } from "typescript-ioc";
import { IService } from "./services";

@Component()
export default class App extends Vue {
  @Inject service1:IService // this works in browser, but fails in tests
  service2:IService = Container.get(IService) // this works in browser and also in tests
}
</script>

However a test which wraps the App component fails:

import "reflect-metadata";
import { Container } from "typescript-ioc";
import { IService, Service } from "../../src/service";
import { mount } from "@vue/test-utils";
import App from "../../src/App.vue";

describe("TS_IOC specs", () => {

  it("should return correct instances", () => {
     Container.bind(IService).to(Service);

	// this succeeds, ie: the instance exists in the container:
    const a = Container.get(IService)
	
	// but this fails:
    const wrapper = mount(App)
  });
});

Error message is: TypeError: Invalid type requested to IoC container. Type is not defined.

So, I'm curious, the instance and type exits in the container, but the wrapper doesn't find it.
Yet the App runs fine outside the test.

Why would this fail: @Inject service:IService
but this passes: service:IService = Container.get(IService)

@InsOpDe
Copy link

InsOpDe commented Aug 21, 2021

Im experiencing the same issues.

I just switched from ts-jest transformer to esbuild-runner/jest or @swc-node/jest.

Before I switched everything worked fine, but after the switch these errors popped up for every typescript-ioc related call.

I noticed that other decorators were not working as well , for example I needed to adapt following decorator helper function:

from

export const isFunctionAsync = (fn: any): boolean => {
	const string = fn.toString().trim()
	return Boolean(string.match(/__awaiter/))
}

to

export const isFunctionAsync = (fn: any): boolean => {
	const string = fn.toString().trim()
	return Boolean(string.match(/__awaiter|async/))
}

This implies that the stringified function doesnt look the way it used to look. Maybe this is the same problem here as well

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

2 participants