Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Gatsby specific mocks and shims
>> Shims Like documented in Gatsby's official testing setup guide (1) there are some configurations that are specific to Gatsby projects. One is the global `___loader` function used by Gatsby which this is mocked in this commit using Jest's `fn()` mocking function. The `___loader.js` file is placed in a folder called `__shims__` within the base `test` directory. >> Mocks With Webpack, there are many loaders available to load any kind of file type (2), e.g. CSS or Sass/Less for styles and images/videos of many types. Jest doesn't know how to handle these when these are imported within source files that are normally processed by Webpack so it'll throw errors during the import. A Jest mock (3) is a dummy module that is used instead of the real module inside tests. It is good when there are something that you can't or don't want to test. Almost everything can be mocked and the examples are assets rather than code. For stylesheets, there is a package called `identity-obj-proxy` (4) which is also already configured for this project. For all other assets a manual mock has been created in a new `__mocks__` folder within the base `test` directory. To configure Jest to use these mocks the matching regex for the files have been added to the `moduleNameMapper` option. References: (1) https://www.gatsbyjs.org/docs/unit-testing/#setting-up-your-environment (2) https://webpack.js.org/loaders (3) https://jestjs.io/docs/en/manual-mocks (4) https://www.npmjs.com/package/identity-obj-proxy GH-39
- Loading branch information