-
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 8 commits
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public | ||
.cache | ||
node_modules |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"semi": false, | ||
"singleQuote": true, | ||
"trailingComma": "es5" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2015 gatsbyjs | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<p align="center"> | ||
<a href="https://www.gatsbyjs.org"> | ||
<img alt="Gatsby" src="https://www.gatsbyjs.org/monogram.svg" width="60" /> | ||
</a> | ||
</p> | ||
<h1 align="center"> | ||
using Jest | ||
</h1> | ||
|
||
Kick off your next Gatsby app with some great testing practices enabled via [Jest][jest], [react-testing-library][react-testing-library], and of course, [Gatsby][gatsby] 💪 | ||
|
||
Check out the [unit testing doc][unit-testing-doc] for further info! | ||
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. will this link work? Looks...incomplete to me 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. oh and it's mentioned below too, with the complete link 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. Yeah! These are markdown style links, so it's like a table of links that it refers to below. Actually a good practice to get into, I think :) Keeps links in one place so they're easy to edit/re-use! 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. Just as an example if it's helpful! This is a link to [google][google], and [this link][google] will also refer to the google link! 🎉
[google]: https://google.com 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. ohhhhhh kewl 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. that seems handy. I'll keep that in mind! |
||
|
||
[jest]: https://jestjs.io/ | ||
[react-testing-library]: https://github.com/kentcdodds/react-testing-library | ||
[gatsby]: https://gatsbyjs.org | ||
[unit-testing-doc]: https://www.gatsbyjs.org/docs/unit-testing/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'test-file-stub' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const React = require('react') | ||
const gatsby = jest.requireActual('gatsby') | ||
|
||
module.exports = { | ||
...gatsby, | ||
graphql: jest.fn(), | ||
Link: jest.fn().mockImplementation(({ to, ...rest }) => | ||
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. This is kinda janky, but I also think that this should probably be added to the docs/unit-testing.md document! 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. (also just for clarity here, gatsby-link errors out in Jest, so this is a way to side step that issue) |
||
React.createElement('a', { | ||
...rest, | ||
href: to, | ||
}) | ||
), | ||
StaticQuery: jest.fn(), | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
siteMetadata: { | ||
title: 'Gatsby Default Starter', | ||
}, | ||
plugins: [ | ||
'gatsby-plugin-react-helmet', | ||
{ | ||
resolve: `gatsby-source-filesystem`, | ||
options: { | ||
name: `images`, | ||
path: `${__dirname}/src/images`, | ||
}, | ||
}, | ||
'gatsby-transformer-sharp', | ||
'gatsby-plugin-sharp', | ||
{ | ||
resolve: `gatsby-plugin-manifest`, | ||
options: { | ||
name: 'gatsby-starter-default', | ||
short_name: 'starter', | ||
start_url: '/', | ||
background_color: '#663399', | ||
theme_color: '#663399', | ||
display: 'minimal-ui', | ||
icon: 'src/images/gatsby-icon.png', // This path is relative to the root of the site. | ||
}, | ||
}, | ||
'gatsby-plugin-offline', | ||
], | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
const babelOptions = { | ||
presets: ['babel-preset-gatsby'], | ||
} | ||
|
||
module.exports = require('babel-jest').createTransformer(babelOptions) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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', | ||
setupTestFrameworkScriptFile: '<rootDir>/jest.setup.js', | ||
setupFiles: ['<rootDir>/loadershim.js'], | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import 'jest-dom/extend-expect' | ||
import 'react-testing-library/cleanup-after-each' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
global.___loader = { | ||
enqueue: jest.fn(), | ||
} |
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.
I think this is quite a bit clearer/real-world, even though both were valid