Steps to reproduce the behavior:
I use AWS CodeBuild to deploy a project and I invoke jest tests during the deployment. When the NODE_ENV is set to dev, there is no problem, but when NODE_ENV is set to production. I get the following error:
[Container] 2020/05/13 16:48:41 Running command npx jest -i src/spec/
--
142 | npx: installed 503 in 12.817s
143 | ● Validation Error:
144 |
145 | Preset ts-jest is invalid:
146 |
147 | The "id" argument must be of type string. Received null
148 | TypeError [ERR_INVALID_ARG_TYPE]: The "id" argument must be of type string. Received null
149 | at validateString (internal/validators.js:117:11)
150 | at Module.require (internal/modules/cjs/loader.js:1037:3)
151 | at require (internal/modules/cjs/helpers.js:77:18)
152 | at setupPreset (/root/.npm/_npx/312/lib/node_modules/jest/node_modules/jest-config/build/normalize.js:289:14)
153 | at normalize (/root/.npm/_npx/312/lib/node_modules/jest/node_modules/jest-config/build/normalize.js:642:15)
154 | at readConfig (/root/.npm/_npx/312/lib/node_modules/jest/node_modules/jest-config/build/index.js:220:68)
155 | at async readConfigs (/root/.npm/_npx/312/lib/node_modules/jest/node_modules/jest-config/build/index.js:400:26)
156 | at async runCLI (/root/.npm/_npx/312/lib/node_modules/jest/node_modules/@jest/core/build/cli/index.js:198:59)
157 | at async Object.run (/root/.npm/_npx/312/lib/node_modules/jest/node_modules/jest-cli/build/cli/index.js:163:37)
I actually do have ts-jest installed and during the deployment I install the following as dev dependencies:
- npm install i -D jest @types/jest ts-jest
I can confirm that ts-jest is installed during the deployment:
[Container] 2020/05/13 16:48:40 Running command npx ts-jest
--
128 | npx: installed 19 in 1.403s
129 |
130 | Usage:
131 | ts-jest command [options] [...args]
132 |
133 | Commands:
134 | config:init Creates initial Jest configuration
135 | config:migrate Migrates a given Jest configuration
136 | help [command] Show this help, or help about a command
137 |
138 | Example:
139 | ts-jest help config:migrate
If it was not installed, I would have gotten a ts-jest error above.
However, the next line contains this:
- npx jest -i src/spec/
And this when the error happens. When NODE_ENV is set to production in the container, Jest seems to ignore the dev dependencies installed. This is wrong because I don't want to put jest and ts-jest into dependencies during my production deploy.
How am i able to address the problem?
I troubled this issue,too.
I think this might be related to resolving the preset artifact with npx
; perhaps it doesn't look in the local node_modules
directory? With yarn
v2 if I run yarn jest
it fails similarly, but if I run yarn install
(under v1) or npm install
and then run yarn jest
it works.
dev deps are not installed if NODE_ENV
is production
, see https://docs.npmjs.com/cli/install.
No idea if that's the issue here (I've never used, or heard of, AWS CodeBuild). If you think this is a bug with Jest, pleas open up a new issue with full reproduction steps, following the template
I'm getting this error as well, in local environment, with no usage of NODE_ENV being set to production. Seems specific to ts-jest, however.
I just ran npm i --save-dev ts-jest
again
@Robbie-Cook not working is there something I am missing?
I see in your command that you run npx jest ...
which means npm will download some version of jest
and run it without using your dev dependencies. That's why jest can not find the preset. Instead you should change the command to say npm run jest ...
then your bundled version of jest
including all the dev dependencies should be run and it will hopefully work.
I think the jest error message is a bit confusing, since it does usually not mean that the preset is invalid, but rather that the preset is not there or at all or can not be found for some reason.
In my case, which I can't imagine will apply to anyone else, I happened to have a symlink to node_modules
in the same folder as my jest.config.js
- that seems to have prevented it from finding the preset. Not the best error message in that case.
I'm testing the preset option in different local projects.
Only ts-jest
preset works:
module.exports = {
...
preset: 'ts-jest',
...
}
Neither of the other two works: js-with-ts - js-with-babel
For now I'm using the transform option in the jest config file:
const { jsWithTs: tsjPreset } = require('ts-jest/presets');
module.exports = {
...
transform: {
...tsjPreset.transform,
// [...]
},
...
}
If anyone is still running into this, I was able to resolve it without needing to install jest separately. I was running npm ci
right before running npx jest
, replacing the install command with NODE_ENV=development npm ci
fixed the issue for me while ensuring the NODE_ENV stays set to production.
Most helpful comment
I just ran
npm i --save-dev ts-jest
again