-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: ensure babel-preset-gatsby can be used with unit tests #9629
Changes from 1 commit
09693a2
9f1e8bb
0964728
60d01ab
3229473
4dd45c7
3975917
8097d77
d631309
3662873
31b1eab
bad29ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,31 +27,26 @@ npm install --save-dev jest babel-jest react-test-renderer identity-obj-proxy 'b | |
|
||
Because Gatsby handles its own Babel configuration, you will need to manually | ||
tell Jest to use `babel-jest`. The easiest way to do this is to add a `"jest"` | ||
section in your `package.json`. You can set up some useful defaults at the same | ||
section in your `jest.config.js`. You can set up some useful defaults at the same | ||
kakadiadarpan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
time: | ||
|
||
```json:title=package.json | ||
"jest": { | ||
"transform": { | ||
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js" | ||
}, | ||
"testRegex": "/.*(__tests__\\/.*)|(.*(test|spec))\\.jsx?$", | ||
"moduleNameMapper": { | ||
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy", | ||
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js" | ||
}, | ||
"testPathIgnorePatterns": ["node_modules", ".cache"], | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!(gatsby)/)" | ||
], | ||
"globals": { | ||
"__PATH_PREFIX__": "" | ||
}, | ||
"testURL": "http://localhost", | ||
"setupFiles": [ | ||
"<rootDir>/loadershim.js" | ||
] | ||
} | ||
```json:title=jest.config.js | ||
module.exports = { | ||
"transform": { | ||
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js" | ||
}, | ||
"moduleNameMapper": { | ||
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy", | ||
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js" | ||
}, | ||
"testPathIgnorePatterns": ["node_modules", ".cache"], | ||
"transformIgnorePatterns": ["node_modules/(?!(gatsby)/)"], | ||
"globals": { | ||
"__PATH_PREFIX__": "" | ||
}, | ||
"testURL": "http://localhost", | ||
"setupFiles": ["<rootDir>/loadershim.js"] | ||
} | ||
``` | ||
|
||
The `transform` section tells Jest that all `js` or `jsx` files need to be | ||
|
@@ -150,11 +145,12 @@ import React from "react" | |
import renderer from "react-test-renderer" | ||
import Bio from "./Bio" | ||
|
||
describe("Bio", () => | ||
describe("Bio", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is quite a bit clearer/real-world, even though both were valid |
||
it("renders correctly", () => { | ||
const tree = renderer.create(<Bio />).toJSON() | ||
expect(tree).toMatchSnapshot() | ||
})) | ||
}) | ||
}) | ||
``` | ||
|
||
This is a very simple snapshot test, which uses `react-test-renderer` to render | ||
|
@@ -220,39 +216,28 @@ config. First install `ts-jest`: | |
npm install --save-dev ts-jest | ||
``` | ||
|
||
Then edit the Jest config in your `package.json` to match this: | ||
|
||
```json:title=package.json | ||
"jest": { | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest", | ||
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js" | ||
}, | ||
"testRegex": "(/__tests__/.*\\.([tj]sx?)|(\\.|/)(test|spec))\\.([tj]sx?)$", | ||
"moduleNameMapper": { | ||
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy", | ||
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js" | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
], | ||
"testPathIgnorePatterns": ["node_modules", ".cache"], | ||
"transformIgnorePatterns": [ | ||
"node_modules/(?!(gatsby)/)" | ||
], | ||
"globals": { | ||
"__PATH_PREFIX__": "" | ||
}, | ||
"testURL": "http://localhost", | ||
"setupFiles": [ | ||
"<rootDir>/loadershim.js" | ||
] | ||
} | ||
Then edit the Jest config in your `jest.config.js` to match this: | ||
|
||
```json:title=jest.config.js | ||
module.exports = { | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest", | ||
"^.+\\.jsx?$": "<rootDir>/jest-preprocess.js" | ||
}, | ||
"testRegex": "(/__tests__/.*\\.([tj]sx?)|(\\.|/)(test|spec))\\.([tj]sx?)$", | ||
"moduleNameMapper": { | ||
".+\\.(css|styl|less|sass|scss)$": "identity-obj-proxy", | ||
".+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/__mocks__/fileMock.js" | ||
}, | ||
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"], | ||
"testPathIgnorePatterns": ["node_modules", ".cache"], | ||
"transformIgnorePatterns": ["node_modules/(?!(gatsby)/)"], | ||
"globals": { | ||
"__PATH_PREFIX__": "" | ||
}, | ||
"testURL": "http://localhost", | ||
"setupFiles": ["<rootDir>/loadershim.js"] | ||
} | ||
``` | ||
|
||
## Testing components with Router | ||
|
@@ -295,7 +280,7 @@ import React from "react" | |
import renderer from "react-test-renderer" | ||
import BlogIndex from "../pages/index" | ||
|
||
describe("BlogIndex", () => | ||
describe("BlogIndex", () => { | ||
it("renders correctly", () => { | ||
const location = { | ||
pathname: "/", | ||
|
@@ -304,6 +289,7 @@ describe("BlogIndex", () => | |
const tree = renderer.create(<BlogIndex location={location} />).toJSON() | ||
expect(tree).toMatchSnapshot() | ||
})) | ||
}) | ||
``` | ||
|
||
For more information on testing page components, be sure to read the docs on | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ function preset(context, options = {}) { | |
const stage = process.env.GATSBY_BUILD_STAGE || `test` | ||
|
||
if (!targets) { | ||
if (stage === `build-html`) { | ||
if (stage === `build-html` || stage === `test`) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Think this makes sense, right? Should transpile to whatever the current version of node is in the unit test runner. |
||
targets = { | ||
node: `current`, | ||
} | ||
|
@@ -37,7 +37,7 @@ function preset(context, options = {}) { | |
r(`@babel/preset-env`), | ||
{ | ||
loose: true, | ||
modules: false, | ||
modules: stage === `test` ? `commonjs` : false, | ||
useBuiltIns: `usage`, | ||
targets, | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I made a few opinionated changes here, namely
package.json
->jest.config.js