vscode-jasmine-test-adapter icon indicating copy to clipboard operation
vscode-jasmine-test-adapter copied to clipboard

Jasmine-TS support

Open jelling opened this issue 6 years ago • 17 comments

We use Jasmine-TS to run our tests as it saves a compilation step and solves a few other problems for us. Naturally, jasmine-test-adapter extension just uses jasmine directly.

Could support be added for Jasmine-TS? I looked at the source for the extension and it's using Jasmine programmatically, as opposed to calling the CLI version of Jasmine. Jasmine-TS doesn't seem to have that feature but I don't know how much that matters.

jelling avatar Sep 14 '18 14:09 jelling

The adapter needs a communication channel through which Jasmine can report the results of loading or running the tests. The current communication channel would most likely not work with the CLI version of Jasmine or Jasmine-TS, so currently I can only recommend to use Jasmine and node-ts directly. I'm looking into creating a different communication channel that can also be used with other test runners that use Jasmine behind the scenes (like Jasmine-TS, Karma, Protractor, Chutzpah,...). But I can't say if this will work and when it would be ready.

hbenl avatar Sep 15 '18 13:09 hbenl

Is it possible to pass node arguments in adapter config?

as options of this child process https://github.com/hbenl/vscode-jasmine-test-adapter/blob/7191fffefbbf7df1b70c37836213e6d398c9ac97/src/adapter.ts#L118

So, we'll be able to use node --require ts-node/register

IvanRave avatar Nov 14 '18 17:11 IvanRave

@IvanRave thanks for the idea - I went ahead and implemented your idea and it worked. I've submitted a pull request! @hbenl

#21

2018-11-14 16_20_27- extension development host - tests_jsonspec ts - jasmine-lab - visual studio c

johnhamm avatar Nov 14 '18 23:11 johnhamm

Well I spoke too soon. It worked on a small test project with a tsconfig.json file set to:

"compilerOptions": {
  "module": "commonjs"
}

However, my Angular CLI project uses:

"compilerOptions": {
  "module": "es2015"
}

When I switch to es2015, I get the following error:

(function (exports, require, module, __filename, __dirname) { import { async, TestBed } from '@angular/core/testing';
                                                              ^^^^^^

SyntaxError: Unexpected token import

Although no module resolution error is visible, I believe it is because it cannot resolve the location of the required ts-node module that is sent to Node as an arg:

node --require "ts-node/register"

It finds ts-node when installed locally in the small test project. It also finds it in the Angular CLI project when I change "module" to "commonjs" in the tsconfig.json file.

I am not sure how to correct this.

johnhamm avatar Nov 17 '18 19:11 johnhamm

@johnhamm I spent some time yesterday trying out your PR and ran into that myself. See https://stackoverflow.com/questions/40635956/overriding-tsconfig-json-for-ts-node-in-mocha for some options for solving it. I solved it by creating a second tsconfig.json with module: commonjs, but the TS_NODE_COMPILER_OPTIONS environment variable solution looks potentially even better.

IGx89 avatar Nov 17 '18 22:11 IGx89

@johnhamm @IGx89 Interestingly, I can't reproduce this problem. In my small test project, it works even with

"compilerOptions": {
  "module": "es2015"
}

Does adding

"jasmineExplorer.env": {
  "TS_NODE_COMPILER_OPTIONS": "{\"module\":\"commonjs\"}"
}

to your project settings fix it for you?

hbenl avatar Nov 18 '18 19:11 hbenl

Unfortunately adding that jasminExplorer.env setting did not change the error (ts-node doesn't seem to be using that env variable for some reason), but directly changing "module" to "commonjs" in the project's ./tsconfig.json file does, but then I get errors where it can no longer find my modules that use a relative path (I use baseUrl in my tsconfig.json file so I don't have to do '../../app/core/models/id' etc:

Error: Cannot find module 'core/models/id'

johnhamm avatar Nov 18 '18 19:11 johnhamm

@johnhamm perhaps the tsconfig-paths package can help?

We use it like this on the CLI:

ts-node -r tsconfig-paths/register -r dotenv/config node_modules/jasmine/bin/jasmine.js "services/our.service.spec.ts

jelling avatar Nov 18 '18 21:11 jelling

@jelling - that did the trick! Thank you so much.

So to recap for anyone experiencing this in the future:

  • To add typescript support to the VSCode Jasmine Test Adapter, add the following two settings to your user settings:
"jasmineExplorer.nodeArgv": [
        "--require",
        "ts-node/register",
        "--require",
        "tsconfig-paths/register"
    ],
"jasmineExplorer.env": {
        "TS_NODE_COMPILER_OPTIONS": "{\"module\":\"commonjs\"}"
    },

You will need the ts-node and tsconfig-paths NPM packages installed in your project.

johnhamm avatar Nov 19 '18 21:11 johnhamm

@johnhamm I'm glad someone has this working! Perhaps a dumb question, but is there anything else needed? I installed those packages and of course have the plugin installed, but it is unable to find any of my tests. We don't have them all in one directory, but rather they're bundled in separate directories with each angular component we make.

We're on Angular 6+ using jasmine and karma. Any other tips you could provide would be immensely helpful, thank you!

supersinex avatar Jan 16 '19 21:01 supersinex

@supersinex This adapter can't load and run Karma tests, but a Karma adapter is being worked on, see hbenl/vscode-example-test-adapter#1

hbenl avatar Jan 21 '19 17:01 hbenl

Thanks for linking that, @hbenl! That looks super promising 👍

supersinex avatar Feb 08 '19 13:02 supersinex

Sorry but I can't get working I have spec in src/specs/module/test.e2e-spec.ts I added the ts-node and tsconfig-paths and I added in the user and workspace the settings "jasmineExplorer.nodeArgv": [ "--require", "ts-node/register", "--require", "tsconfig-paths/register" ], "jasmineExplorer.env": { "TS_NODE_COMPILER_OPTIONS": "{"module":"commonjs"}" }, but doesn't show the test How I can log the error?

apis3445 avatar Oct 30 '20 23:10 apis3445

@apis3445 Can you point me to a sample project with which I can reproduce this?

hbenl avatar Nov 15 '20 17:11 hbenl

it is a normal angular + protractor, maybe is due to protractor with the latest version of annual

apis3445 avatar Nov 16 '20 17:11 apis3445

So are these tests karma or protractor tests? In that case, it won't work with this adapter, but for Karma tests there is the Angular/Karma Test adapter.

hbenl avatar Nov 16 '20 17:11 hbenl

yes it is protractor test cases

apis3445 avatar Nov 18 '20 03:11 apis3445