Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: how to execute e.g. combined with ts-jest? #6

Open
dnmd opened this issue Jan 10, 2021 · 1 comment
Open

docs: how to execute e.g. combined with ts-jest? #6

dnmd opened this issue Jan 10, 2021 · 1 comment

Comments

@dnmd
Copy link

dnmd commented Jan 10, 2021

This project seems a nice fit for constructing dynamic paths within the tsconfig.json where based on e.g. an environment variable a different directory is used to e.g. load different data sets.

The only thing I miss here is how to 'execute' or trigger e.g. the tsconfig.js/once? Maybe I am overthinking things to much, but do I place it within my package.json's script or jest.config.js? In my scenario I use ts-jest and have the following setup;

( minimal repo: https://github.com/dnmd/ts-jest-tsconfig.js )

package.json

  "scripts": {
    "test": "jest"
  },

jest.config.js

module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  moduleNameMapper: {
    // as this file is already a .js file I can user `${process.env.NODE_ENV}` here already pointing to e.g. dev or test
    '^app/(.*)$': `<rootDir>/${process.env.NODE_ENV}/$1`,
  }
};

tsconfig.json

// tsconfig.json
{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": {
            "@app/*": [
                // ====> here I also would like to use the environment var to point to a different path depending on the env.
                // `src/${process.env.NODE_ENV}/*`
                "src/*"
            ]
        }
    }
}
@c-vetter
Copy link
Owner

Hello @dnmd
Thank you for reaching out! 🙂
I'm sorry for the late reply, I'm swamped with other work.

Regarding your query, I'm actually unsure what exactly you are trying to accomplish, but I'll try to help towards what I understand. It's been quite some time since I've used jest, so I may be overlooking something. Please elaborate accordingly.

In order to trigger a single run before any run of npm test, you can just add a pretest script to your package.json, like so:

    "pretest": "npx tsconfig.js --once --extensions=ts",
    "test": "jest"

I've added it to your repo: https://github.com/rasenplanscher/ts-jest-tsconfig.js


Note that it's necessary to add a loader, and unfortunately, there's an error preventing the related message to be printed when this is not fulfilled.

I will have to update the docs, and also fix that error, which I found while answering your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants