Skip to content

Commit 575a15c

Browse files
authored
Merge pull request #101 from openscript-ch/94-configure-and-write-tests
94 configure and write tests
2 parents d6ef8f2 + db8ad7b commit 575a15c

14 files changed

Lines changed: 2112 additions & 698 deletions

.github/workflows/continuous-integration.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
name: Continuous Integration
22

3-
on: pull_request
3+
on:
4+
pull_request:
5+
branches:
6+
- develop
7+
push:
8+
branches:
9+
- develop
410

511
jobs:
612
check:
@@ -27,5 +33,8 @@ jobs:
2733
- name: Run tests
2834
run: yarn test
2935

36+
- name: Submit coverage
37+
uses: codecov/codecov-action@v3
38+
3039
- name: Try to build
3140
run: yarn build

__mock__/file-mock.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 'test-file-stub';

__mock__/gatsby.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
({ activeClassName, activeStyle, getProps, innerRef, partiallyActive, ref, replace, to, ...rest }) =>
10+
React.createElement('a', {
11+
...rest,
12+
href: to,
13+
})
14+
),
15+
Slice: jest.fn().mockImplementation(({ alias, ...rest }) =>
16+
React.createElement('div', {
17+
...rest,
18+
'data-test-slice-alias': alias,
19+
})
20+
),
21+
useStaticQuery: jest.fn(),
22+
};

gatsby-node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { GatsbyNode } from 'gatsby';
22
import { CreateGenericPages } from './src/gatsby/createPages/createGenericPages';
33
import { customizeSitePageContext } from './src/gatsby/createSchemaCustomization/customizeSitePageContext';
4-
import { GeneratePageMetaData } from './src/gatsby/onCreatePage/generatePageMetaData';
4+
import { generatePageMetaData } from './src/gatsby/onCreatePage/generatePageMetaData';
55

66
export const createPages: GatsbyNode['createPages'] = async args => {
77
await CreateGenericPages(args);
@@ -12,5 +12,5 @@ export const createSchemaCustomization: GatsbyNode['createSchemaCustomization']
1212
};
1313

1414
export const onCreatePage: GatsbyNode<Record<string, unknown>, Queries.SitePageContext>['onCreatePage'] = async args => {
15-
await GeneratePageMetaData(args);
15+
await generatePageMetaData(args);
1616
};

jest-preprocess.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const babelOptions = {
2+
presets: ['babel-preset-gatsby', '@babel/preset-typescript', ['@babel/preset-react', { runtime: 'automatic' }]],
3+
};
4+
module.exports = require('babel-jest').default.createTransformer(babelOptions);

jest.config.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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`, `<rootDir>.*/dist`],
10+
transformIgnorePatterns: [`node_modules/(?!(gatsby|gatsby-script|gatsby-link)/)`],
11+
globals: {
12+
__PATH_PREFIX__: ``,
13+
},
14+
testEnvironment: 'jsdom',
15+
testEnvironmentOptions: {
16+
url: `http://localhost`,
17+
},
18+
setupFiles: [`<rootDir>/loadershim.js`],
19+
setupFilesAfterEnv: ['<rootDir>/setup-test-env.js'],
20+
collectCoverage: true,
21+
collectCoverageFrom: [`src/**/*.{ts,tsx}`],
22+
};

loadershim.js

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

package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@
3737
},
3838
"devDependencies": {
3939
"@babel/core": "7.20.12",
40+
"@testing-library/jest-dom": "^5.16.5",
41+
"@testing-library/react": "^13.4.0",
4042
"@types/mdx": "^2.0.3",
4143
"@types/node": "18.11.18",
4244
"@types/react": "18.0.26",
4345
"@types/react-dom": "18.0.10",
4446
"@typescript-eslint/eslint-plugin": "5.48.1",
4547
"@typescript-eslint/parser": "5.48.1",
4648
"babel-eslint": "10.1.0",
49+
"babel-jest": "^29.3.1",
50+
"babel-preset-gatsby": "^3.4.0",
4751
"eslint": "8.31.0",
4852
"eslint-config-airbnb": "19.0.4",
4953
"eslint-config-airbnb-typescript": "17.0.0",
@@ -54,6 +58,9 @@
5458
"eslint-plugin-prettier": "4.2.1",
5559
"eslint-plugin-react": "7.32.0",
5660
"eslint-plugin-react-hooks": "4.6.0",
61+
"identity-obj-proxy": "^3.0.0",
62+
"jest": "^29.3.1",
63+
"jest-environment-jsdom": "^29.3.1",
5764
"prettier": "2.8.2",
5865
"svg-sprite": "^2.0.2",
5966
"ts-node": "10.9.1",
@@ -70,12 +77,13 @@
7077
"check:format": "eslint . --max-warnings 0",
7178
"check:types": "tsc --noEmit",
7279
"check:watch-types": "tsc --noEmit --watch",
80+
"dev:test": "jest --watch",
7381
"develop": "gatsby develop",
7482
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
7583
"start": "yarn run develop",
7684
"serve": "gatsby serve",
7785
"clean": "gatsby clean",
78-
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\"",
86+
"test": "jest",
7987
"prestart": "yarn run build:sprite",
8088
"prebuild": "yarn run build:sprite"
8189
},

setup-test-env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "@testing-library/jest-dom"

src/components/Sprite.test.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { render } from '@testing-library/react';
2+
import { Sprite } from './Sprite';
3+
4+
describe('<Sprite />', () => {
5+
it('should match snapshot', () => {
6+
const { asFragment } = render(<Sprite name="arrow" />);
7+
expect(asFragment()).toMatchSnapshot();
8+
});
9+
});

0 commit comments

Comments
 (0)