RyanCavanaugh / learn-a

Sample repo using lerna with TypeScript project references

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add unit test folder next to src folder

jan-dolejsi opened this issue · comments

The simplicity of this example is the best thing about it. However, can you add a test folder next to src folder in pkg1? And place one .ts file in there as if that was some unit test for the fn function. I cannot figure out how to modify the tsconfig file(s) to avoid the tsc or runtime errors.

For anyone interested, you can lookup how we did it for Stryker mutator in this PR: stryker-mutator/stryker-js#1290

Basically did this:

root
+-- tsconfig.json
+-- tsconfig.settings.json
+-- packages
       +-- pkg-1
        |       +-- src
        |       |   +-- foo.ts
        |       +-- test
        |       |   +-- foo.spec.ts
        |       +-- tsconfig.json
        |       +-- tsconfig.src.json
        |       +-- tsconfig.test.json
       +-- pkg-2
        |       +-- src
        |       |   +-- bar.ts
        |       +-- test
        |       |   +-- bar.spec.ts
        |       +-- tsconfig.json
        |       +-- tsconfig.src.json
        |       +-- tsconfig.test.json

A specific pkg-n tsconfig.json looks like this:

{
  "files": [],
  "references": [
    { "path": "./tsconfig.src.json" },
    { "path": "./tsconfig.test.json" }
  ]
}

The tsconfig.src.json look like this:

{
  "extends": "../../tsconfig.settings.json",
  "compilerOptions": {
    "rootDir": "."
  },
  "include": [ "src"],
  "references": [
    { "path": "../pkg-2/tsconfig.src.json" } // be sure to reference the `tsconfig.src.json` files here
  ]
}

The tsconfig.test.json looks like this:

{
  "extends": "../../tsconfig.settings.json",
  "compilerOptions": {
    "rootDir": ".",
    "types": [ "mocha"]
  },
  "include": [ "test"],
  "references": [ { "path": "tsconfig.src.json" } ]
}

Using this setup you can also make sure the describe, it, etc global test functions aren't available in the production code, a very nice plus, without much performance loss (althought a bit more configuration)

Many thanks for the public gift @nicojs. That's a really useful example.

Yes a test folder would be nice, otherwise this setup doesn't work without all the extra config like nicojs mentioned (which shouldn't be necessary IMO).

@nicojs Where is the out dir and how does it work with both src and test?

We chose to not specify an out dir. Our *.js, *.d.ts, *.map and *.d.map files just land right next to the original *.ts file. We configured vscode to ignore those files using a settings.json file:

"files.exclude": {
	".git": true,
	".tscache": true,
	"{src,test}/**/*.d.ts": true, //base mapping not possible: https://github.com/Microsoft/vscode/issues/40850
	"*.d.ts": true, // needed to exclude d.ts files in api
	"**/*.js": {
		"when": "$(basename).ts"
	},
	"**/**/*.js": { // HACK! Cannot reuse same key, but this key means essentially the same
		"when": "$(basename).tsx"
	},
	"**/*.map": {
		"when": "$(basename)"
	}
},```

It is pretty combersome, but in practise I also find it combersome to have compiled files land in other places (like a `dist` folder). Having the files next to the *.ts file feels like a lesser of two evils for us.

Thanks for the info 👍