Skip to content

Commit 6eb9c37

Browse files
test: add testing framework
1 parent 20326ba commit 6eb9c37

File tree

7 files changed

+1858
-41
lines changed

7 files changed

+1858
-41
lines changed

__mocks__/gatsby.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const React = require('react');
2+
const gatsby = jest.requireActual('gatsby');
3+
4+
module.exports = {
5+
...gatsby,
6+
graphql: jest.fn(),
7+
Link: jest.fn().mockImplementation(
8+
// these props are invalid for an `a` tag
9+
({
10+
activeClassName,
11+
activeStyle,
12+
getProps,
13+
innerRef,
14+
partiallyActive,
15+
ref,
16+
replace,
17+
to,
18+
...rest
19+
}) =>
20+
React.createElement('a', {
21+
...rest,
22+
href: to,
23+
})
24+
),
25+
StaticQuery: jest.fn(),
26+
useStaticQuery: jest.fn(),
27+
};

jest-preprocess.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const babelOptions = {
2+
presets: ['babel-preset-gatsby', '@babel/preset-typescript'],
3+
};
4+
5+
module.exports = require('babel-jest').createTransformer(babelOptions);

jest.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
transform: {
3+
'^.+\\.[jt]sx?$': `<rootDir>/jest-preprocess.js`,
4+
},
5+
moduleNameMapper: {
6+
'.+\\.(css|styl|less|sass|scss)$': `identity-obj-proxy`,
7+
'.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': `<rootDir>/__mocks__/file-mock.js`,
8+
},
9+
testPathIgnorePatterns: [`node_modules`, `\\.cache`, `<rootDir>.*/public`],
10+
transformIgnorePatterns: [`node_modules/(?!(gatsby)/)`],
11+
globals: {
12+
__PATH_PREFIX__: ``,
13+
},
14+
testURL: `http://localhost`,
15+
setupFiles: [`<rootDir>/loadershim.js`],
16+
};

loadershim.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global.___loader = {
2+
enqueue: jest.fn(),
3+
};

package.json

+9-2
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,19 @@
9191
"typescript": "^3.9.7"
9292
},
9393
"devDependencies": {
94+
"@testing-library/jest-dom": "^5.11.6",
95+
"@testing-library/react": "^11.2.2",
96+
"babel-jest": "^26.6.3",
97+
"babel-preset-gatsby": "^0.8.0",
9498
"firebase-admin": "^9.2.0",
9599
"firestore-export-import": "^0.8.0",
96100
"gatsby-plugin-webpack-bundle-analyser-v2": "^1.1.11",
97101
"husky": "^4.2.5",
102+
"identity-obj-proxy": "^3.0.0",
103+
"jest": "^26.6.3",
98104
"lint-staged": "^10.2.11",
99-
"prettier": "^2.0.5"
105+
"prettier": "^2.0.5",
106+
"react-test-renderer": "^17.0.1"
100107
},
101108
"license": "MIT",
102109
"scripts": {
@@ -107,7 +114,7 @@
107114
"serve": "gatsby serve",
108115
"clean": "gatsby clean",
109116
"check-links": "blc http://localhost:9000 -qorf --exclude train.usaco.org --exclude quora.com --exclude tablesgenerator.com --exclude judge.yosupo.jp --exclude kenkoooo.com --requests 10",
110-
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
117+
"test": "jest",
111118
"functions:deploy": "firebase deploy --only functions"
112119
},
113120
"repository": {

src/pages/__tests__/liveupdate.js

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import LiveUpdate from '../LiveUpdate';
4+
5+
jest.mock('../../components/layout', () => {
6+
return jest.fn(({ children }) => (
7+
<div data-testid="mocked-layout">{children}</div>
8+
));
9+
});
10+
jest.mock('../../components/seo', () => {
11+
return jest.fn(() => null);
12+
});
13+
jest.mock('../../components/TopNavigationBar/TopNavigationBar', () => {
14+
return jest.fn(() => null);
15+
});
16+
17+
describe('Live Update', () => {
18+
/*
19+
* To speed up build times, someone can comment out the LiveUpdate page
20+
* and replace it with a placeholder. This test ensures that they
21+
* remember to uncomment the page before deploying.
22+
*/
23+
it('does not render the placeholder content', () => {
24+
const { getByTestId } = render(<LiveUpdate />);
25+
expect(getByTestId('mocked-layout')).toBeTruthy();
26+
});
27+
});

0 commit comments

Comments
 (0)