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

Jasmine-TS support #19

Open
jelling opened this issue Sep 14, 2018 · 17 comments
Open

Jasmine-TS support #19

jelling opened this issue Sep 14, 2018 · 17 comments

Comments

@jelling
Copy link

jelling commented Sep 14, 2018

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.

@hbenl
Copy link
Owner

hbenl commented Sep 15, 2018

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.

@IvanRave
Copy link

Is it possible to pass node arguments in adapter config?

as options of this child process

const childProcess = fork(

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

@johnhamm
Copy link

johnhamm commented Nov 14, 2018

@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
Copy link

johnhamm commented Nov 17, 2018

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.

@IGx89
Copy link

IGx89 commented Nov 17, 2018

@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.

@hbenl
Copy link
Owner

hbenl commented Nov 18, 2018

@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?

@johnhamm
Copy link

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'

@jelling
Copy link
Author

jelling commented Nov 18, 2018

@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

@johnhamm
Copy link

@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.

@supersinex
Copy link

@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!

@hbenl
Copy link
Owner

hbenl commented Jan 21, 2019

@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

@supersinex
Copy link

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

@apis3445
Copy link

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?

@hbenl
Copy link
Owner

hbenl commented Nov 15, 2020

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

@apis3445
Copy link

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

@hbenl
Copy link
Owner

hbenl commented Nov 16, 2020

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.

@apis3445
Copy link

yes it is protractor test cases

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

7 participants