vscode-jasmine-test-adapter
vscode-jasmine-test-adapter copied to clipboard
Jasmine-TS support
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.
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.
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 thanks for the idea - I went ahead and implemented your idea and it worked. I've submitted a pull request! @hbenl
#21
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 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.
@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?
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 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 - 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 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 This adapter can't load and run Karma tests, but a Karma adapter is being worked on, see hbenl/vscode-example-test-adapter#1
Thanks for linking that, @hbenl! That looks super promising 👍
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 Can you point me to a sample project with which I can reproduce this?
it is a normal angular + protractor, maybe is due to protractor with the latest version of annual
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.
yes it is protractor test cases