add unit test cases

This commit is contained in:
Matthias Kretschmann 2023-09-28 21:16:51 +01:00
parent b885ec26f0
commit 475679419f
Signed by: m
GPG Key ID: 606EEEF3C479A91F
10 changed files with 3103 additions and 72 deletions

View File

@ -1,3 +0,0 @@
{
"presets": [["@babel/env"]]
}

View File

@ -10,6 +10,7 @@
"env": {
"browser": true,
"node": true,
"es2020": true
"es2020": true,
"jest": true
}
}

View File

@ -25,11 +25,27 @@ jobs:
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
cache: npm
- run: npm ci
- run: npm test
- run: npm run build
coverage:
if: ${{ github.actor != 'dependabot[bot]' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
cache: npm
- run: npm ci
- uses: paambaati/codeclimate-action@v5.0.0
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
with:
coverageCommand: npm test
publish:
needs: test
if: success() && startsWith(github.ref, 'refs/tags')

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules
/*.js
coverage

View File

@ -5,6 +5,7 @@
[![npm package](https://img.shields.io/npm/v/gatsby-redirect-from.svg)](https://www.npmjs.com/package/gatsby-redirect-from)
[![Build Status](https://github.com/kremalicious/gatsby-redirect-from/workflows/CI/badge.svg)](https://github.com/kremalicious/gatsby-redirect-from/actions)
[![Maintainability](https://api.codeclimate.com/v1/badges/9643b2a038a7d338a73a/maintainability)](https://codeclimate.com/github/kremalicious/gatsby-redirect-from/maintainability)
[![Test Coverage](https://api.codeclimate.com/v1/badges/9643b2a038a7d338a73a/test_coverage)](https://codeclimate.com/github/kremalicious/gatsby-redirect-from/test_coverage)
> 🎯 Set redirect urls in your YAML frontmatter within your [Gatsby](https://www.gatsbyjs.org) site's Markdown files. Mimics the behavior of [jekyll-redirect-from](https://github.com/jekyll/jekyll-redirect-from).
> https://kremalicious.com/gatsby-redirect-from/
@ -65,7 +66,6 @@ redirect_from:
- /2008/04/aperture-file-types/
# note: trailing slashes are required
---
```
## [Documentation](https://kremalicious.com/gatsby-redirect-from/)

3
babel.config.json Normal file
View File

@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}

3028
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,10 +7,11 @@
"license": "MIT",
"main": "index.js",
"scripts": {
"build": "cross-env NODE_ENV=production babel src --out-dir . --ignore __tests__",
"start": "babel -w src --out-dir . --ignore __tests__",
"test": "eslint src/**/*.js",
"format": "prettier --write 'src/**/*.{js,jsx}'",
"build": "cross-env NODE_ENV=production babel src --out-dir .",
"start": "babel -w src --out-dir .",
"test": "npm run lint && jest --coverage",
"lint": "eslint ./{src,test}/**/*.js",
"format": "prettier --write '{src,test}/**/*.{js,jsx}'",
"release": "release-it --non-interactive",
"changelog": "auto-changelog -p",
"prepublishOnly": "npm run build"
@ -21,13 +22,14 @@
"devDependencies": {
"@babel/cli": "^7.23.0",
"@babel/core": "^7.23.0",
"@babel/helper-define-map": "^7.18.6",
"@babel/preset-env": "^7.22.20",
"auto-changelog": "^2.4.0",
"chalk": "^5.3.0",
"cross-env": "^7.0.3",
"eslint": "^8.50.0",
"eslint-config-prettier": "^9.0.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"prettier": "^3.0.3",
"release-it": "^16.2.1"
},
@ -38,6 +40,14 @@
"engines": {
"node": ">=18"
},
"jest": {
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!chalk/.*)"
]
},
"repository": "github:kremalicious/gatsby-redirect-from",
"bugs": {
"url": "https://github.com/kremalicious/gatsby-redirect-from/issues"

View File

@ -30,6 +30,7 @@ export function createPages({ graphql, actions }, pluginOptions) {
if (result.errors) {
console.log(result.errors) // eslint-disable-line no-console
reject(result.errors)
return
}
const allPosts = result.data.q.edges

98
test/gatsby-node.test.js Normal file
View File

@ -0,0 +1,98 @@
/**
* @jest-environment jsdom
*/
import { graphql } from 'gatsby'
import { createPages } from '../src/gatsby-node.js'
jest.mock('gatsby', () => ({
graphql: jest.fn(),
actions: { createRedirect: jest.fn() }
}))
describe('createPages', () => {
let pluginOptions
let actions
let consoleLogSpy
beforeEach(() => {
actions = { createRedirect: jest.fn() }
pluginOptions = { query: 'allMarkdownRemark' }
consoleLogSpy = jest.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
jest.resetAllMocks()
consoleLogSpy.mockRestore()
})
it('should create redirects correctly', async () => {
graphql.mockReturnValueOnce(
Promise.resolve({
data: {
q: {
edges: [
{
node: {
fields: { slug: '/post-1/' },
frontmatter: { redirect_from: ['/old-url-1', '/old-url-2'] }
}
}
]
}
}
})
)
await createPages({ graphql, actions }, pluginOptions)
expect(actions.createRedirect).toHaveBeenCalledTimes(2)
expect(actions.createRedirect).toHaveBeenCalledWith({
fromPath: '/old-url-1',
toPath: '/post-1/',
isPermanent: true,
redirectInBrowser: true
})
expect(actions.createRedirect).toHaveBeenCalledWith({
fromPath: '/old-url-2',
toPath: '/post-1/',
isPermanent: true,
redirectInBrowser: true
})
})
it('should use pluginOptions.query when defined', async () => {
const customQuery = 'someCustomQuery'
pluginOptions.query = customQuery
graphql.mockImplementation((queryString) => {
expect(queryString).toContain(customQuery)
return Promise.resolve({ data: { q: { edges: [] } } })
})
await createPages({ graphql, actions }, pluginOptions)
})
it('should default to allMarkdownRemark when pluginOptions.query is undefined', async () => {
pluginOptions.query = undefined
graphql.mockImplementation((queryString) => {
expect(queryString).toContain('allMarkdownRemark')
return Promise.resolve({ data: { q: { edges: [] } } })
})
await createPages({ graphql, actions }, pluginOptions)
})
it('should log and reject errors when the GraphQL query fails', async () => {
graphql.mockReturnValueOnce(
Promise.resolve({ errors: [{ message: 'GraphQL error' }] })
)
try {
await createPages({ graphql, actions }, pluginOptions)
} catch (error) {
expect(console.log).toHaveBeenCalledWith([{ message: 'GraphQL error' }])
expect(error).toEqual([{ message: 'GraphQL error' }])
}
})
})