Repository navigation
TypeScript example compilation errors #52
Description
Activity
This can be resolved by importing:
import intern from 'intern';as the first line, however I'm not sure whether or not that is an acceptable solution? If so, I can create a PR.
I assume in your
tsconfig.json(or when you are compiling with TSC) you have"declaration": true. If this is the case, TypeScript is generating a.d.tsfor each of your TypeScript files, including the tests. Typically you only do this when you are going to redistribute your code as JavaScript and need the declaration files to go along with it.You are then exporting something from
test/functional/example.tswhich then when TypeScript is generating the file, it cannot create a valid.d.tswithout you being explicit where certain type definitions are coming from. This is why doing the import resolves the issue, because TypeScript can name those types to generate a valid definition file.There are three solutions to this:
- Don't export anything from
example.ts - Don't generate declaration files when compiling tests
- Import the intern types as you have done above, so the compiler can name them.
- Don't export anything from
Thanks for the quick reply...
Yes, this project in question is exporting modules for import into other projects so you're quite correct that we have
"declaration": truein ourtsconfig.jsonfile... this is in the maintsconfig.jsonfile at the root of the project rather than a nestedtsconfig.jsonat the root of the tests folder.I'm happy with my resolution to the problem but wanted to flag up the issue in case other developers hit the same problem.
Note that the example uses a separate tsconfig.json for the tests. That config includes the following
typesdirective:"types": ["intern"]
This has the same effect as
import intern from 'intern';, which will load the globalinternvariable type.Reacted by Kitson KellyI had a separate
tsconfig.jsonfile that had that"types": ["intern"]entry in it. It didn't prevent me seeing the error appearing in either the VSCode console or the output from running TSC.I'm just reporting what I experienced in case this is something that other people encounter.
Maybe I needed to run TSC from within the
testsfolder directly - however, as with a lot of TypeScript errors this isn't immediately obvious as to the cause of the problem.Could you post a sample project somewhere? The example itself builds properly whether or not declarations are enabled in "tests/tsconfig.json", and I'd like to look into your specific situation in a bit more detail.
In general, if you're creating a project that's exporting test functionality (like helper functions for Intern), it would make sense to have a single
tsconfig.jsonfile and include the"types": ["intern"]directive in that file (orimport 'intern';somewhere). The reason the example uses a separate tsconfig for tests is because for a typical project the test code isn't part of the application being tested. We don't want to create any accidental dependencies between the application and Intern, so the application's tsconfig doesn't load intern types.Unfortunately I can't post my project as-is (without disclosing private company information) but could look to create a sample project with similar configuration when I get a chance.
I agree that it makes sense to keep the tsconfig separate, but the example is extending the configuration - it might be better to define a separate config without explicitly without extending.
My point here isn't to criticize for the sake of it... I really like Intern, I just want to help reduce barriers to entry which was I raised the issue. It's not especially blocking me at the moment, I just wanted to make you aware it had happened.
Our main tsconfig looks like this though:
{ "compilerOptions": { "allowJs": false, "allowSyntheticDefaultImports": true, "declaration": true, "jsx": "react", "module": "commonjs", "noImplicitAny": false, "outDir": "dist", "removeComments": true, "inlineSourceMap": true, "strictNullChecks": true, "lib": [ "es6", "dom" ], "target": "es5" }, "include": [ "node_modules/@types/**/*.d.ts", "src/**/*.ts", "src/**/*.tsx" ], "exclude": [ "deps**/*" ] }...and the nested version looks like this:
{ "declaration": false, "extends": "../tsconfig.json", "compilerOptions": { "baseUrl": "..", "types": [ "intern" ] }, "include": [ "./**/*.ts" ] }I definitely appreciate the information. This just seems more like an issue with a specific use case than a problem with the example.
There wouldn't really be any benefit to separating the configs here. I mean, the tests should be built with almost all the same settings as the application, with only a few test-specifi differences, which is the intended use case for
extends.Note that "declaration" is a compiler option, so should be in
compilerOptionsin the nested config. While having it outsidecompilerOptionswon't cause a build error, it has no effect there. That could be causing problems, since the nested config isn't disabling thedeclarationstatement as intended.
I'm trying to write functional tests using TypeScript and have followed the example provided. However I'm getting compilation errors:
...generated from the line:
...which is taken directly from the example
The tests themselves do actually run when treated as JavaScript (however I needed to modify the
leadfoot/keysimport to be:const keys = require('@theintern/leadfoot/keys').default;)