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

How to disable ts-node? #3329

Comments

@trusktr
Copy link

trusktr commented Jun 19, 2019

Expected behaviour

  • Write JavaScript code (.js files)
  • Install Karma
  • Write JavaScript test files (.js)
  • everything works great!
  • install ts-node and don't expect anything to break.

Actual behaviour

installing ts-node causes previously-working tests to break, because Karma automatically uses ts-node, assuming that it is what I want.

Environment Details

  • Karma version (output of karma --version): 4.0.1
  • Relevant part of your karma.config.js file: N/A, there's nothing about ts-node in there.

Steps to reproduce the behaviour

Described above.

As soon as I rm -rf node_modules/ts-node, errors go away.

Errors, for example:

19 06 2019 11:13:35.064:ERROR [config]: Invalid config file!
  TSError: ⨯ Unable to compile TypeScript:
../trusktr+builder-js-package/config/karma.config.js:3:22 - error TS7016: Could not find a declaration file for module 'globby'. '/Users/trusktr/src/trusktr+builder-js-package/node_modules/globby/index.js' implicitly has an 'any' type.
  Try `npm install @types/globby` if it exists or add a new declaration (.d.ts) file containing `declare module 'globby';`

3 const glob = require('globby')
                       ~~~~~~~~
../trusktr+builder-js-package/config/karma.config.js:6:24 - error TS7016: Could not find a declaration file for module 'mkdirp'. '/Users/trusktr/src/trusktr+builder-js-package/node_modules/mkdirp/index.js' implicitly has an 'any' type.
  Try `npm install @types/mkdirp` if it exists or add a new declaration (.d.ts) file containing `declare module 'mkdirp';`

6 const mkdirp = require('mkdirp') // mkdir -p
                         ~~~~~~~~
../trusktr+builder-js-package/config/karma.config.js:7:22 - error TS7016: Could not find a declaration file for module 'rimraf'. '/Users/trusktr/src/trusktr+builder-js-package/node_modules/rimraf/rimraf.js' implicitly has an 'any' type.
  Try `npm install @types/rimraf` if it exists or add a new declaration (.d.ts) file containing `declare module 'rimraf';`

7 const rmrf = require('rimraf') // rm -rf
                       ~~~~~~~~
../trusktr+builder-js-package/config/karma.config.js:8:19 - error TS7016: Could not find a declaration file for module 'regexr'. '/Users/trusktr/src/trusktr+builder-js-package/node_modules/regexr/index.js' implicitly has an 'any' type.
  Try `npm install @types/regexr` if it exists or add a new declaration (.d.ts) file containing `declare module 'regexr';`

8 const r = require('regexr').default
                    ~~~~~~~~
../trusktr+builder-js-package/config/karma.config.js:23:19 - error TS7006: Parameter 'file' implicitly has an 'any' type.

23 testFiles.forEach(file => {
                     ~~~~
../trusktr+builder-js-package/config/karma.config.js:45:48 - error TS7006: Parameter 'moduleName' implicitly has an 'any' type.

45                     ${nodeModulesToCompile.map(moduleName => {
                                                  ~~~~~~~~~~
../trusktr+builder-js-package/config/karma.config.js:96:27 - error TS7006: Parameter 'config' implicitly has an 'any' type.

96 module.exports = function(config) {
                             ~~~~~~
../trusktr+builder-js-package/config/karma.config.js:150:18 - error TS7006: Parameter 'fullname' implicitly has an 'any' type.

150 function dirname(fullname) {
                     ~~~~~~~~
../trusktr+builder-js-package/config/karma.config.js:157:27 - error TS7006: Parameter 'fileName' implicitly has an 'any' type.

157 function withoutExtention(fileName) {
                              ~~~~~~~~

    at createTSError (/Users/trusktr/src/trusktr+infamous/node_modules/ts-node/src/index.ts:245:12)
    at reportTSError (/Users/trusktr/src/trusktr+infamous/node_modules/ts-node/src/index.ts:249:19)
    at getOutput (/Users/trusktr/src/trusktr+infamous/node_modules/ts-node/src/index.ts:362:34)
    at Object.compile (/Users/trusktr/src/trusktr+infamous/node_modules/ts-node/src/index.ts:395:32)
    at Module.m._compile (/Users/trusktr/src/trusktr+infamous/node_modules/ts-node/src/index.ts:473:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/trusktr/src/trusktr+infamous/node_modules/ts-node/src/index.ts:476:12)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.parseConfig (/Users/trusktr/src/trusktr+infamous/node_modules/karma/lib/config.js:351:22)
    at new Server (/Users/trusktr/src/trusktr+infamous/node_modules/karma/lib/server.js:66:24)
    at Object.exports.run (/Users/trusktr/src/trusktr+infamous/node_modules/karma/lib/cli.js:291:7)
    at Object.<anonymous> (/Users/trusktr/src/trusktr+infamous/node_modules/karma/bin/karma:3:23)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
Failed with exit code: 1

As you can see, tests break simply by installing ts-node in the project and not yet converting any tests to TypeScript.

I have a tsconfig.json file in the project, but used for other things at the moment.

It'd be nice to be able to tell Karma which tsconfig to use for test files, and to be able enable TypeScript support manually in the first place, so that there aren't any surprises.

@trusktr
Copy link
Author

trusktr commented Jun 19, 2019

The documentation on Customized TypeScript Configuration shows how to override ts-node settings, but it doesn't work:

  • First, doesn't the require('./karma.conf.ts') need to be placed on module.exports? Otherwise how does the config get exported to Karma?
  • Second, after placing my configuration, as follows, nothing changes, and I get the same type errors although I've clearly told ts-node not to type check:
    // disable ts-node's type checking, allows plain JS to work without ts-node
    // getting in the way.
    require('ts-node').register({
    	typeCheck: false,
    	transpileOnly: true,
    	files: true,
    	compilerOptions: {},
    })
    
    module.exports = require('./karma-config.js')
    with similar errors, but relative to the new karma-config.js file. For example:
    ../trusktr+builder-js-package/config/karma-config.js:95:27 - error TS7006: Parameter 'config' implicitly has an 'any' type.
    
    95 module.exports = function(config) {
    

ts-node is still type checking, despite my configuration telling not to type check. So configuring ts-node in this manner does not work. EDIT: see #3274

@trusktr
Copy link
Author

trusktr commented Jun 19, 2019

How do we disable Karma's automatic ts-node usage?

@trusktr
Copy link
Author

trusktr commented Jun 19, 2019

Related: #3274

@trusktr
Copy link
Author

trusktr commented Jun 19, 2019

I have a tsconfig.json in my project, used for other things. I didn't intend for it to be used by Karma. This tsconfig.json file has allowJs: true, which is causing Karma to run TypeScript on JavaScript config file.

@trusktr
Copy link
Author

trusktr commented Jun 19, 2019

Cool, I forked Karma, removed the register() call as in #3274, and can now get back to work!

@LarsDenBakker
Copy link

The tests in my monorepo magically broke, after a lot of debugging I found out that suddenly karma tests loading my config with ts-node because a dependency of another packages installed ts-node. Is there really no way to turn off this behavior?

@LarsDenBakker
Copy link

I ended up using this:

const mock = require('mock-require');

mock('ts-node', () => {
  throw new Error("I don't want to run tests with ts-node");
});

require('karma/lib/cli').run();

@jimbojw
Copy link

jimbojw commented Jun 1, 2020

Just ran into this today. 😓

@trusktr
Copy link
Author

trusktr commented Jun 27, 2020

Ping. Karma team, any plan to fix this?

@johnjbarton
Copy link
Contributor

Looks like PR #3274 stalled.

@maksimr
Copy link
Contributor

maksimr commented Feb 2, 2021

This is absolutely ugly/unacceptable behavior. 😡
Imagine a situation when transitive dependency install ts-node in your JavaScript project where you use TypeScript over JavaScript only for type checking and your tests stop working at some time because TypeScript start checking(wrong files) when you run tests. Happy to debug it. Rewrite default require is a bad practice in most cases and even worse when this does not your code!

I'll provide pull request in the future. As a workaround you can use this code in karam.conf.js

// @ts-nocheck
process[Symbol.for('ts-node.register.instance')]?.enabled(false);

@devoto13
Copy link
Collaborator

devoto13 commented Feb 3, 2021

@maksimr I do agree that this behaviour is causing issues and we should not make such assumptions by default. At the same time we want to allow users to write their configs using their preferred language with the relative convenience. Please see #3521 (comment) for some thinking on the design. But I'm open to discuss alternative/better approach especially if this approach is used by some other popular tool dealing with the same issue.

And the PR would be very welcome once the design is discussed and decided!

xel23 added a commit to xel23/karma that referenced this issue Feb 8, 2021
Call require('ts-node').register() after checking configFilePath has `.ts` extension

Fixes karma-runner#3329
xel23 added a commit to xel23/karma that referenced this issue Feb 8, 2021
Call require('ts-node').register() after checking configFilePath has `.ts` extension

Fixes karma-runner#3329
johnjbarton pushed a commit that referenced this issue Feb 12, 2021
Call require('ts-node').register() after checking configFilePath has `.ts` extension

Fixes #3329
karmarunnerbot pushed a commit that referenced this issue Feb 12, 2021
## [6.1.1](v6.1.0...v6.1.1) (2021-02-12)

### Bug Fixes

* **config:** check extension before ts-node register ([#3651](#3651)) ([474f4e1](474f4e1)), closes [#3329](#3329)
* report launcher process error when exit event is not emitted ([#3647](#3647)) ([7ab86be](7ab86be))
@karmarunnerbot
Copy link
Member

🎉 This issue has been resolved in version 6.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@devoto13
Copy link
Collaborator

Re-opening as I still want to implement a sustainable solution for this use case.

This was referenced Mar 15, 2021
anthony-redFox pushed a commit to anthony-redFox/karma that referenced this issue May 16, 2023
Call require('ts-node').register() after checking configFilePath has `.ts` extension

Fixes karma-runner#3329
anthony-redFox pushed a commit to anthony-redFox/karma that referenced this issue May 16, 2023
## [6.1.1](karma-runner/karma@v6.1.0...v6.1.1) (2021-02-12)

### Bug Fixes

* **config:** check extension before ts-node register ([karma-runner#3651](karma-runner#3651)) ([474f4e1](karma-runner@474f4e1)), closes [karma-runner#3329](karma-runner#3329)
* report launcher process error when exit event is not emitted ([karma-runner#3647](karma-runner#3647)) ([7ab86be](karma-runner@7ab86be))
@PDSSnyk PDSSnyk linked a pull request May 26, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment