Description
One neat trick to use tsconfig.json/paths
is to pointing the module name to your source file:
// tsconfig.json of blue-tape-fixture
{
...
"paths": {
"blue-tape-fixture": [ "src/" ]
}
}
This way, your test files can reference the source as if it is a proper module:
You can see it is working in vscode. But it does not when I run test with ts-node:
// package.json
{
"scripts": {
"test": "ts-node -P tsconfig.build.json node_modules/blue-tape/bin/blue-tape \"test/**/*.ts\" | tap-spec"
}
}
The error is:
Error: Cannot find module 'blue-tape-fixture'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
You can see it here: https://github.com/unional/blue-tape-fixture/tree/ts-node
# clone
npm install
npm test
Activity
blakeembrey commentedon Jun 19, 2016
@unional That's the node.js error - TypeScript is working fine, but node will never be able to resolve an alias like that unless TypeScript emits code that rewrites paths. What do you think is the solution here?
unional commentedon Jun 19, 2016
I see. In order for it to work, a
module-path-rewriter
need to be written and loaded byts-node
.Alternatively is to load the transpiled code
dist/*
, which kind of defeat the purpose ofts-node
(I said kind of because the test files, in this use case, can remain as ts files).blakeembrey commentedon Jun 19, 2016
This module uses the emitted code though - it's TypeScript itself that doesn't do any rewriting right?
unional commentedon Jun 20, 2016
Just check. Nope it does not do any rewriting. Seems like the
baseUrl
andpaths
is only used during compilation. By design or bug on TS side?unional commentedon Jun 20, 2016
Updated https://github.com/unional/blue-tape-fixture/tree/ts-node to demo the issue.
run
npm run build-test
and see underdist/test/index.ts
. It is still requiringblue-tape-fixture
unional commentedon Jun 20, 2016
Confirm above statement: microsoft/TypeScript#5039 (comment)
blakeembrey commentedon Jun 20, 2016
@unional Yeah, it's tricky. I can understand TypeScript's stance, if they don't have a solution for it built-in I think we can investigate adding a plugin once they implement the pluggable transforms in TypeScript 2.0. This is definitely more of a user-land feature.
sanex3339 commentedon Oct 2, 2016
any news? i want to use 'paths' option in my tests
blakeembrey commentedon Oct 2, 2016
No, it does not work and is not expected to work. Someone can still implement a plugin (which would be very valuable to others too!), but I am not currently working on it.
jonaskello commentedon Dec 26, 2016
Maybe this could be solved by augmenting node's loading rather than having typescript re-write the paths. There are some alternatives mentioned here. In particular I'm thinking that wrapping the global require and just changing the path according to tsconfig would be the simplest solution.
64 remaining items