'Jest in monorepo fails on all node api calls
I have a typescript monorepo project, where I am using jest for tests. Now, they run fine if i do:
cd Packages/Server;
npm t
But if i run npm t in the root (because hopefully I will get some tests written for the other modules as well) It fails. The weird thing is that it seems to simply have any and all node API's undefined So if i, in any module used in the test, as some point have a call to something like: util.promisify(...) or path.resolve(...) the test will crash, complaining that:
Test suite failed to run
TypeError: Cannot read property 'promisify' of undefined
56 | }) => Promise<string[]>;
57 |
> 58 | export const Exec: ExecuteMethod = util.promisify(ChildProcess.exec);
I simply cannot comprehend why this happens: It seems it does not load the standard internal node APIs.
Project structure looks like this:
root
|
|-jest.config.js
|-Package.json
|-(other files as well)
|
|-Packages
... |
|-Server
... |
|-src
| |
| ...
|-Package.json
|-jest.config.js
|-test
... |
...
My jest configs look like this:
Root:
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jest-environment-node',
coveragePathIgnorePatterns: [
"/node_modules/"
],
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.ts$',
testPathIgnorePatterns: [ 'obj/', 'bin/' ]
};
Server module:
const base = require('../../jest.config');
module.exports = {
...base,
globals: {
'ts-jest': {
tsconfig: 'tsconfig.json',
},
},
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
