learn-a icon indicating copy to clipboard operation
learn-a copied to clipboard

Add unit test folder next to src folder

Open jan-dolejsi opened this issue 7 years ago • 6 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.

jan-dolejsi avatar Sep 12 '18 20:09 jan-dolejsi

For anyone interested, you can lookup how we did it for Stryker mutator in this PR: https://github.com/stryker-mutator/stryker/pull/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)

nicojs avatar Dec 22 '18 16:12 nicojs

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

nealeu avatar Dec 22 '18 19:12 nealeu

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).

milesj avatar Feb 23 '19 08:02 milesj

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

milesj avatar Feb 23 '19 08:02 milesj

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.

nicojs avatar Feb 24 '19 16:02 nicojs

Thanks for the info 👍

milesj avatar Feb 25 '19 02:02 milesj