7

I'm starting on a jasmine unit testing path and I'm having issues trying to execute jasmine and finding my ts written specs.

I have jasmine-ts and I just run the usual

jasmine-ts --config=spec/support/jasmine.json

the above line is executed after I run jasmine init on the root folder

EDIT:

Sorry I forgot to put in the error

internal/modules/cjs/loader.js:438
      throw e;
      ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './dist' is not defined by "exports" i
gdubs
  • 2,724
  • 9
  • 55
  • 102

3 Answers3

8

Fixed it. Found that you need to downgrade your ts-node in the meantime. The issue is being tracked here.

bicarlsen
  • 1,241
  • 10
  • 27
gdubs
  • 2,724
  • 9
  • 55
  • 102
  • 5
    The recommendation is to downgrade to ts-node@8.10.2. Running `npm install ts-node@8.10.2 --save-dev` fixed this for me. – AlmaniaM Jan 15 '21 at 18:08
  • 1
    Check [this GitHub Issue](https://github.com/svi3c/jasmine-ts/issues/177#issuecomment-1008292856) as well, and the example setups by @ert78gb for [commonJS](https://github.com/ert78gb/jasmine4-typescript-es6-module-example) and [ES6 modules](https://github.com/ert78gb/jasmine4-typescript-es6-module-example). – bicarlsen Feb 04 '22 at 22:49
2

Had the same issue with jasmine-ts. Newer version of Jasmine restricts which parts are visible to outside ("exports" field in package.json).

Jasmine-ts tries to access stuff that is not allowed by Jasmine's package.json. The solution was to remove this restriction (removed "exports" field from package.json).

Added this snippet to my projects package.json:

"scripts": {
  "postinstall" : "npx json -I -f ./node_modules/jasmine/package.json -e \"delete this.exports\""
}
mkalmo
  • 185
  • 1
  • 5
2

For the official answer see https://github.com/svi3c/jasmine-ts/issues/177#issuecomment-1008292856 Basically jasmine-ts is dead and will not be updated for jasmine 4.0. You don't need to do any patching of jasmine. I strongly advise against it as you are only creating extra work for your self with every upgrade. In addition even minor updates may change or remove the internal classes/files your patches expose.

For commonjs output as is default for node projects you only need to add the fallowing helper:

const { register } = require('ts-node');

register({
    project: 'tsconfig.json'
});

This will enable jasmine to run typescript tests though other helper. For ES6 output see https://github.com/ert78gb/jasmine4-typescript-es6-module-example.. Nodejs support for E6 modules is still new and will come with some changes beyond the scope of this answer.

Failing this I have found another solution using ts-node:

#!/bin/sh
ts-node -r ts-node/register ./node_modules/jasmine/bin/jasmine.js
#ts-node returns non-zero positive even on success for the above. Force return to 0 so npm doesn't complain.
return 0

If you have more than one tsconifg.json file you may need an addition npm module to assist with this. The above script has the advantage of making even the config files able to be read as typescript.