A project I'm working on is already configured with Jest and testing works as it should. This is how the current jest.config.js
file looks like;
const ignores = [...];
const coverageIgnores = [...];
module.exports = {
roots: ['<rootDir>/src'],
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
},
},
moduleFileExtensions: ['js', 'json', 'ts'],
testPathIgnorePatterns: [...ignores],
coveragePathIgnorePatterns: [...ignores, ...coverageIgnores],
testEnvironment: 'node',
coverageThreshold: {
global: {
branches: 86,
functions: 75,
lines: 86,
statements: 86,
},
},
preset: 'ts-jest',
};
The configuration currently uses the ts-jest
preset. The project also involves a DynamoDB instance that should be tested and that's where multiple presets come into play. The current preset, ts-jest
, should be used in combination with the @shelf/jest-dynamodb
-preset (https://jestjs.io/docs/en/dynamodb). The problem is that the preset property in the config is of type String and doesn't support an array or an object.
I've read some similar questions like this one; Is it possible to use Jest with multiple presets at the same time?, but questions like these don't seem to have a definitive working solution on how to solve this problem.
Others suggest a solution in which a separate Jest config is created for every preset, but this is something I don't want and that probably will cause more problems in the future.
It would be ideal to have this single config file modified to allow multiple (here 2) presets, but how can this be achieved?
The Jest philosophy is to work great by default, but sometimes you just need more configuration power. It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named jest.config.js|ts|mjs|cjs|json .
Jest actually ships with jsdom and the environment already configured. You can override it with the testEnvironment setting. If you need to set up more aspects of the environment though, you can use the setupTestFrameworkScriptFile setting to point to a file that executes before all of your tests run.
json file. transformIgnorePatterns option can be used to specify which files shall be transformed by Babel. Many react-native npm modules unfortunately don't pre-compile their source code before publishing. I have react-navigation package installed in my app so I added this regex value in transformIgnorePatterns.
moduleNameMapper [Object] (Default: null)This configuration holds the key to file mocking. By using this configuration all the external asset files like images and style files will be mocked, as these files are not intended to be tested. So, while running test cases, only the the mock files are imported.
You can't actually use two presets but since the preset for shelf/jest-dynamodb is just setting other options in the jest setup, you can call them yourself. I was able to do this by just adding to my "jest" section of package.json:
"globalSetup": "./node_modules/@shelf/jest-dynamodb/setup.js",
"globalTeardown": "./node_modules/@shelf/jest-dynamodb/teardown.js",
This makes the same calls that the preset was doing, meaning you can leave the preset to ts-jest
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With