简体   繁体   中英

Error running node test with jest in docker container

I am trying to run a series of unit tests using jest on a node app in a docker container.

I am trying to run these tests using the npm test command, and in my package.json file in the scripts section I have the following configuration:

"scripts": {
    "test": "jest --watchAll --verbose"
  },

When I run this command on my local machine outside of docker, the tests are running successfully, however, when I try to run the same command in docker I get a strange error:

TypeError: (0 , (_jestValidate || _load_jestValidate(...)).validateCLIOptions) is not a function
    at buildArgv (/usr/src/app/node_modules/jest-cli/build/cli/index.js:233:66)
    at Object.<anonymous> (/usr/src/app/node_modules/jest-cli/build/cli/index.js:11:20)
    at Generator.next (<anonymous>)
    at step (/usr/src/app/node_modules/jest-cli/build/cli/index.js:158:191)
    at /usr/src/app/node_modules/jest-cli/build/cli/index.js:158:437
    at new Promise (<anonymous>)
    at Object.<anonymous> (/usr/src/app/node_modules/jest-cli/build/cli/index.js:158:99)
    at Object.run (/usr/src/app/node_modules/jest-cli/build/cli/index.js:30:17)
    at Object.<anonymous> (/usr/src/app/node_modules/jest-cli/bin/jest.js:16:27)
    at Module._compile (node:internal/modules/cjs/loader:1102:14)
npm ERR! code 1
npm ERR! path /usr/src/app
npm ERR! command failed
npm ERR! command sh -c jest --watchAll --verbose

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-26T10_49_00_358Z-debug.log

I have tried to run these tests both in docker-compose.yaml file and in dockerfile using the RUN npm test

I have jest and jest-cli configured in my package.json in the dev dependencies section

  "devDependencies": {
    "jest": "22.4.4",
    "jest-cli": "22.4.4",
    "supertest": "^3.0.0"
  }

Am I missing something?

Add test-ci in your package.json , and let it run in CI:

...
scripts": {
    "test": "jest --watchAll --verbose",
    "test-ci": "jest --ci"
  },
...

jest --watchAll needs some special processes to provide us good testing, what needs are watching files, terminal control and so on. But, docker images often don't have such features for reducing their image size. In detail, if to read codes of jest , but I'm little bit tired. Why don't you pursue it? :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM