Tuesday, August 21, 2018

More on tsconfig paths with Jest

In my last post I explain how you can manually update your jest configuration to reflect paths that you may have setup in tsconfig.json.

I found this utility called tsconfig-paths-jest, that will do that automatically for you. To use this, you will need to set up a jest.config.js file so that you can assign values dynamically at test runtime, and if you have jest configuration info in package.json you should remove that when you set up your jest.config.js file. Please see the tsconfig-paths-jest project for full instructions.

One other pointer there - be careful if like me you have copied the tsconfig.json file from somewhere on the internet. The utility uses JSON.parse, and the implementation is strict and doesn't like trailing commas at the end of arrays and objects.

So something like this:

"paths": {
"@assets/*": [ "src/assets/*" ],
"@source/*": [ "src/*" ],
}

Should be changed to:

"paths": {
"@assets/*": [ "src/assets/*" ],
"@source/*": [ "src/*" ]
}

No comments: