How can I specify multiple source folders in my tsconfig.json? - unit-testing

I currently have the following project structure:
project/
tsconfig.json
webpack.config.js
package.json
node_modules/
...lots of dependencies
typings/
...lots of .d.ts files for the dependencies
src/
...folders for files for my projects
My tsonfig.json looks like:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"outDir": "./js",
"rootDir": "./src",
"sourceMap": true,
"jsx": "react"
},
"exclude": [
"typings/main.d.ts",
"typings/main",
"node_modules"
]
}
This all works very well and I can happily develop my application and run it in a browser.
I would now like to add some unit tests to my project and coming from a Java background my initial instinct is to place the tests in a separate folder:
project/
test/
...all of my test cases
Of course, the files in the test/ folder need to reference the code in my src/ folder. How do I set that up?
Or is it "better" to place the tests inline in the src/ folder and have a separate webpack.config.js file for them?
Really confused about how this works in practice in larger TypeScript projects.
Note: I have seen this but found the answer less than illuminating. It seems that the referenced feature discussion about filesGlob would help me, but I just wonder how people are doing this today?

Now other than rootDir, you can use rootDirs
"rootDirs": ["./scripts", "./src"],
for multiple folders.
Here is the API doc: https://www.typescriptlang.org/docs/handbook/module-resolution.html#virtual-directories-with-rootdirs

I think you are looking for path mapping. With the paths compiler option, you can specify not only a mapping to a single location but to several. This is the example from the documentation:
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [
"*",
"generated/*"
]
}
}
If the compiler does not find a module in the expected location, it repeats module resolution in the "generated" subfolder. The baseUrl setting seems redundant but it is mandatory.

Or is it "better" to place the tests inline in the src/ folder and have a separate webpack.config.js file for them?
That is what I do. Do not use the TypeScript compiler as a module bundler (Especially if you are not using modules https://github.com/TypeStrong/atom-typescript/blob/master/docs/out.md). Just let it do the compile and bundle for browser using webpack and use as it is (if using module commonjs) for backend (nodejs).

Related

How to create simple readable regex that matches either of two possible file paths (for Jest test script)

Apologies if this question is already answered, but all of the answers I've found recommend regex that's extremely difficult to parse, and I'm hopeful that something more readable should be possible.
I am running tests using Jest on a Javascript project using typescript.
Using the default build runner in VS Code, and configured in tsconfig.json (code below), my transpiled javascript goes to an out folder.
I have a few sub-folders containing little test projects. I would like to run tests just for one of those sub-folders. I had this working via the "scripts" section in package.json (code below). But then I added the out folder and now the tests can't find the transpiled javascript.
I'd like to tell Jest that there are TWO folders it needs to look in when running tests - the folder where the test file and typescript lives, and the folder where the transpiled javascript lives.
The current line - "test:char": "jest --watchAll --testPathPattern=src/character-copier" just specifies one folder and is using regex but this isn't obvious, which suits me fine because it means it's easy to read. I'd like to be able to say "Look in either src/character-copier or out/character-copier" - but I can't work out how to do this.
package.json:
"scripts": {
"test": "jest",
"test:char": "jest --watchAll --testPathPattern=src/character-copier"
},
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"sourceMap": true,
"outDir": "out"
}
}
To find files within a specific directory.
/(out|src)\/character-copier\/.*/gi
demo: https://regex101.com/r/F49pNG/1
Use this to execute your code:
"scripts": {
"test": "jest --watchAll --testPathPattern=\"/(out|src)\/character-copier\/.*\""
},

ignore file extension with Jest

I'm using webpack resolve.extensions to "selectively" bundle my js files.
eg.
App.js
import MyComp from 'comp/myComp';
in comp/ folder I have:
MyComp.web.js
MyComp.ios.js
MyComp.android.js
Now I want to write test for App.js, to test how it render as web. But the problem is Jest keep resolving all 3 .js files and that causes dependency error because I'm not running the command in a mobile environment and most mobile modules will not work.
how can I tell jest to only resolve .js and .web.js files just like webpack?
I added this to my package.json and it keep resolving .ios.js and .android.js
"moduleFileExtensions": ["web.js", "js", "jsx"],
"moduleDirectories": [
"node_modules",
"main"]
I tried as suggested with:
"testPathIgnorePatterns": ["<rootDir>/node_modules/", "^.+\\.(android|ios)\\.js$"],
looks no effects :(
You can also remap files to an empty file
touch empty.js
In your jest config add following
moduleNameMapper: {
'\\.(css|jpg|png|scss|less|sass)$': '<rootDir>/empty.js',
},
Works perfect for me
You can add testPathIgnorePatterns to your package.json file. The value is an array of regex patterns. Any file/path that matches an expression will be ignored/skipped from testing.
See these Jest docs for more detail

How to turn on .sass extensions in Ionic 2?

I have set up a basic project and only .scss files are picked up. I would like to write my CSS with the .sass format. How can I turn that on?
You can override the app script config files:
https://github.com/driftyco/ionic-app-scripts#overriding-config-files
In my package.json, I added the following:
"config": {
"ionic_sass": "./config/sass.config.js"
}
I created the sass.config.js (copy from the github project) and added the sass extension:
includeFiles: [
/\.(scss|sass)$/i
],
It's unfortunately impossible to update the watch config file, so I directly edited it in the node module:
Under #ionic/app-scripts/config/watch.config.js
Add the following .sass line below .scss:
'{{SRC}}/**/*.scss',
'{{SRC}}/**/*.sass'

Typescript: how to disable spec.ts file generation?

i'm developing an Angular2 app with Typescript and every time i run the Typescript transpiler it creates spec.ts files.
They are unit tests for the source files because the convention for Angular2 applications is to have this file for each .ts file.
Since at the moment i don't want to do any test, i would like to temporaly disable the generation of spec.ts files, that are a bit messy to handle with my source files.
Do you know how to do that?
EDIT: here is my tsconfig.json file:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"outDir": "dist",
"rootDir": "./src",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"moduleResolution": "node"
},
"exclude": [
"typings/main.d.ts",
"typings/main",
"node_modules"
]
}
You can configure your tsconfig.json file to achieve this.
You can specify which files to compile by adding them to the files array or you can tell typescript to exclude a certain file or folder by including it in the exclude array.
Here is the link from the official docs.
But as far as i can tell you can't provide a patter like so: '/**.spec.ts'

Building multiple bundles with r.js

Hopefully a low-ball conceptual question. I'm having trouble understanding the modules option of r.js build configs. I want to build multiple modules with nested dependencies with one r.js build config.
Say I have the following project structure:
+---build
+---src
+---moduleOne
| +---moduleOne.js
| \---dependecyForModuleOne.js
+---moduleTwo
| +---moduleOne.js
| \---dependecyForModuleTwo.js
|---buildConfig.js
|---devModuleConfig.js
\---prodModuleConfig.js
devModuleConfig and prodModuleConfig are the dev and prod runtime configs, and buildConfig.js is the r.js build config.
Now, I can build moduleOne no problem using this config:
({
"baseUrl": "./",
"name": "moduleOne/moduleOne",
"out": "../build/moduleOneBundle.js",
mainConfigFile: 'devModuleConfig.js',
optimize: 'none'
})
I end up with a bundle in build that I can run after specifying different paths in the build config:
+---build
\---moduleOneBundle.js
I want to build two modules, so specify moduleOne using modules config option:
({
"baseUrl": "./",
modules: [
{
"name": "moduleOne/moduleOne",
"out": "../build/moduleOne.js"
}
],
dir:"../build", // <-- r.js says I need to add this. why?
mainConfigFile: 'devModuleConfig.js',
optimize: 'none'
})
As well as the required dir option, I get all my configs in the build dir, but I did not specify them, and I do not get my bundled module, and I get a text file containing r.js build output. In fact, build ends up looking exactly the same as src:
+---build
+---moduleOne
| +---moduleOne.js
| \---dependecyForModuleOne.js
+---moduleTwo
| +---moduleOne.js
| \---dependecyForModuleTwo.js
|---build.txt
|---buildConfig.js
|---devModuleConfig.js
\---prodModuleConfig.js
How do I configure multiple modules to build using one r.js config? I've read the docs a few times and can't get my head around it.
You can see my project that contains all this here: https://github.com/sennett/r.js-multiple-modules
When specifying modules, r.js includes all files then optimizes ones based on the rules specified in modules. removeCombined: true deletes modules from build that were combined into another module, and then manual deletion of the other files can be used to manually clean up any unwanted build artefacts.