Ts-jest: Cannot run Jest tests with Inversify

Created on 22 Sep 2018  Â·  9Comments  Â·  Source: kulshekhar/ts-jest

Expected Behavior

Being able to tests with Jest using typescript and inversify

Current Behavior

Not able to run test, getting the message:
TypeError: Reflect.hasOwnMetadata is not a function

Possible Solution


Steps to Reproduce (for bugs)

git clone this sample project and run tests using Jest with the Jest config file.
https://github.com/stherrienaspnet/nodejs-inversify-typescript-jest

Many thanks!

Most helpful comment

@stherrienaspnet back from my computer. This is not a bug. You have to configure Jest correctly. Here is the diff that makes it work:

diff --git a/jest.config.js b/jest.config.js
new file mode 100644
index 0000000..10446b7
--- /dev/null
+++ b/jest.config.js
@@ -0,0 +1,5 @@
+module.exports = {
+  preset: 'ts-jest',
+  testEnvironment: 'node',
+  setupFiles: ['./src/config/ioc.config.ts']
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..e88fe4c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,13 @@
+{
+  "devDependencies": {
+    "@types/jest": "^23.3.2",
+    "@types/reflect-metadata": "^0.1.0",
+    "jest": "^23.6.0",
+    "ts-jest": "^23.10.1",
+    "typescript": "^3.0.3"
+  },
+  "dependencies": {
+    "inversify": "^4.13.0",
+    "typemoq": "^2.1.0"
+  }
+}
diff --git a/tsconfig.json b/tsconfig.json
index 9b733fe..c7a6fca 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -5,7 +5,8 @@
     "sourceMap": true,
     "types": ["reflect-metadata", "jest"],
     "outDir": "dist",
-    "experimentalDecorators": true
+    "experimentalDecorators": true,
+    "emitDecoratorMetadata": true
   },
   "exclude": [
     "node_modules"

All 9 comments

on my phone, sorry for quick message. in tsconfig you have to enable emitDecoratorMetadat and experimentalDecorator

@stherrienaspnet back from my computer. This is not a bug. You have to configure Jest correctly. Here is the diff that makes it work:

diff --git a/jest.config.js b/jest.config.js
new file mode 100644
index 0000000..10446b7
--- /dev/null
+++ b/jest.config.js
@@ -0,0 +1,5 @@
+module.exports = {
+  preset: 'ts-jest',
+  testEnvironment: 'node',
+  setupFiles: ['./src/config/ioc.config.ts']
+};
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..e88fe4c
--- /dev/null
+++ b/package.json
@@ -0,0 +1,13 @@
+{
+  "devDependencies": {
+    "@types/jest": "^23.3.2",
+    "@types/reflect-metadata": "^0.1.0",
+    "jest": "^23.6.0",
+    "ts-jest": "^23.10.1",
+    "typescript": "^3.0.3"
+  },
+  "dependencies": {
+    "inversify": "^4.13.0",
+    "typemoq": "^2.1.0"
+  }
+}
diff --git a/tsconfig.json b/tsconfig.json
index 9b733fe..c7a6fca 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -5,7 +5,8 @@
     "sourceMap": true,
     "types": ["reflect-metadata", "jest"],
     "outDir": "dist",
-    "experimentalDecorators": true
+    "experimentalDecorators": true,
+    "emitDecoratorMetadata": true
   },
   "exclude": [
     "node_modules"

Thanks I will try it when i will be home

Le sam. 22 sept. 2018 15:21, Huafu Gandon notifications@github.com a
écrit :

on my phone, sorry for quick message. in tsconfig you have to enable
emitDecoratorMetadat and experimentalDecorator

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/kulshekhar/ts-jest/issues/752#issuecomment-423767149,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AF19cTC_zGRbtbgMEXvy1vmqivgFk5pYks5udo2rgaJpZM4W1ZzT
.

Many thanks :)

Le sam. 22 sept. 2018 16:17, Huafu Gandon notifications@github.com a
écrit :

Closed #752 https://github.com/kulshekhar/ts-jest/issues/752.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/kulshekhar/ts-jest/issues/752#event-1861739441, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AF19cYKuHkF1i-odTiBPy-CoWXJZenjLks5udprtgaJpZM4W1ZzT
.

Hello huafu, I have updated my git repository with your changes and this is running :). The only thing remaining is to filter files, because actually when i run tests, it is running tests for ts files and js files, so every tests are duplicated. I do not know what to add as configuration changes just to get ts files tests only.
Many thanks

Your build script should not build test files

Also for more info: https://kulshekhar.github.io/ts-jest/user/install#customizing

Is this something I have to configure into tsconfig.json since files are generated by typescript? Sorry for my bad understanding

yes, look exclude key, search on google for tsconfig doc

thanks you :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

qm3ster picture qm3ster  Â·  3Comments

ahnpnl picture ahnpnl  Â·  3Comments

mikeyakymenko picture mikeyakymenko  Â·  3Comments

TKJohn picture TKJohn  Â·  4Comments

jbreckmckye picture jbreckmckye  Â·  3Comments