I use create-react-app to create an react project, so I have
// package.json
"dependencies": {
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
...
},
first thing I don’t understand is, why those @testing-library packages are in dependencies
not devDependencies
? why we need those test packages in production?
the second thing I don’t understand is, I didn’t install jest
manually, how can I use jest directly in my project
describe('xxx', () => {
test(...);
});
});
I don’t need to do any import like import { describe, test, expect } from "jest";
?
2
Answers
When you run the create-react-app command to initialize your React project, it generates the project structure and sets up the development environment for you. Part of this setup includes installing various dependencies, including Jest. create-react-app also automatically configures Jest for you and sets up the necessary configuration files and scripts.This configuration includes recognizing test files and providing global testing functions like describe, test, and expect.
You don’t need to import those functions, because Jest sets these on global, so when you’re using the Jest CLI, they’re loaded into the Node global namespace. This is similar to setting
window.foo = "foo"
in a client-side script — you can access it without needing to import it or pull it off an object.And the test-related packages are in dependencies rather than devDependencies because Dan Abramov said so. You can move them to devDeps if you want to. The CRA team’s position is that you should be building static files to deploy anyway, so it doesn’t matter.