Skip to content

Commit

Permalink
Detect if new changes break existing links
Browse files Browse the repository at this point in the history
Added a unit test which determines if any static links have been broken by iterating over the build output and comparing it to a snapshot of existing links. The test will fail if a page is removed and no redirect is added for it.
  • Loading branch information
AndyClifford committed Oct 5, 2023
1 parent fe38b85 commit a37a2dc
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ jobs:
- run: yarn lint
- run: yarn test
- run: ./build.sh
- run: yarn test:build
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"test": "vitest --watch=false"
"test": "vitest --watch=false",
"test:build": "yarn build --mode development && vitest --watch=false --config vitest.build.config.js"
},
"devDependencies": {
"@astrojs/mdx": "^1.0.3",
Expand All @@ -30,6 +31,7 @@
"gray-matter": "^4.0.3",
"mdast-util-to-string": "^3.1.1",
"mermaid": "^9.3.0",
"path": "^0.12.7",
"remark-html": "^15.0.2",
"remark-parse": "^10.0.1",
"remark-sectionize": "^1.1.1",
Expand Down
39 changes: 39 additions & 0 deletions src/__tests__/__snapshots__/links.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Vitest Snapshot v1

exports[`the build should not break any links 1`] = `
Set {
"/api/api-keys",
"/api/auth",
"/api/data-types",
"/api/http-status-codes",
"/api/introduction",
"/api/pagination",
"/connections/farmlands",
"/guides/creating-digital-tokens",
"/guides/creating-test-money",
"/guides/ecommerce-website",
"/guides/example-oidc-consumer",
"/guides/farmlands-card-partner-support",
"/guides/farmlands-portal",
"/guides/farmlands-pos-integration",
"/guides/initiating-refunds",
"/guides/integrating-third-party-asset",
"/guides/line-items",
"/guides/loading-and-sending-assets",
"/guides/merchant-integration-barcode-flow",
"/guides/merchant-integration-error-handling",
"/guides/merchant-integration-qr-code-flow",
"/guides/merchant-terminals",
"/guides/partial-payment-extension",
"/guides/patron-not-present",
"/guides/payment-conditions",
"/guides/payment-flows",
"/guides/payment-terminals",
"/guides/point-of-sale",
"/guides/requesting-payment",
"/guides/requesting-pre-auth",
"/guides/third-party-application-payments",
"/guides/transaction-reporting",
"",
}
`;
23 changes: 23 additions & 0 deletions src/__tests__/links.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs');
const path = require('path');

function extractLinksRecursively(directory) {
const files = fs.readdirSync(directory);
let links = new Set();
for (const file of files) {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
links = new Set([...links, ...extractLinksRecursively(filePath)]);
} else if (stat.isFile() && filePath.endsWith('index.html')) {
const urlPath = filePath.substring(filePath.indexOf('/'), filePath.lastIndexOf('/'));
links.add(urlPath);
}
}
return links;
}

test('the build should not break any links', () => {
expect(extractLinksRecursively('dist')).toMatchSnapshot();
});

8 changes: 8 additions & 0 deletions vitest.build.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true,
include: ['src/__tests__/links.test.js']
},
});
3 changes: 2 additions & 1 deletion vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
globals: true
globals: true,
exclude: ['**/node_modules/**', 'src/__tests__/links.test.js']
},
});
25 changes: 25 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,11 @@ inherits@2, inherits@^2.0.3, inherits@^2.0.4:
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==

[email protected]:
version "2.0.3"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.3.tgz#633c2c83e3da42a502f52466022480f4208261de"
integrity sha512-x00IRNXNy63jwGkJmzPigoySHbaqpNuzKbBOmzK+g2OdZpQ9w+sxCN+VSB3ja7IAge2OP2qpfxTjeNcyjmW1uw==

ini@~1.3.0:
version "1.3.8"
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c"
Expand Down Expand Up @@ -4743,6 +4748,14 @@ path-to-regexp@^6.2.1:
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-6.2.1.tgz#d54934d6798eb9e5ef14e7af7962c945906918e5"
integrity sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==

path@^0.12.7:
version "0.12.7"
resolved "https://registry.yarnpkg.com/path/-/path-0.12.7.tgz#d4dc2a506c4ce2197eb481ebfcd5b36c0140b10f"
integrity sha512-aXXC6s+1w7otVF9UletFkFcDsJeO7lSZBPUQhtb5O0xJe8LtYhj/GxldoL09bBj9+ZmE2hNoHqQSFMN5fikh4Q==
dependencies:
process "^0.11.1"
util "^0.10.3"

pathe@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/pathe/-/pathe-1.0.0.tgz#135fc11464fc57c84ef93d5c5ed21247e24571df"
Expand Down Expand Up @@ -4937,6 +4950,11 @@ prismjs@^1.29.0:
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.29.0.tgz#f113555a8fa9b57c35e637bba27509dcf802dd12"
integrity sha512-Kx/1w86q/epKcmte75LNrEoT+lX8pBpavuAbvJWRXar7Hz8jrtF+e3vY751p0R8H9HdArwaCTNDDzHg/ScJK1Q==

process@^0.11.1:
version "0.11.10"
resolved "https://registry.yarnpkg.com/process/-/process-0.11.10.tgz#7332300e840161bda3e69a1d1d91a7d4bc16f182"
integrity sha512-cdGef/drWFoydD1JsMzuFf8100nZl+GT+yacc2bEced5f9Rjk4z+WtFUTBu9PhOi9j/jfmBPu0mMEY4wIdAF8A==

progress@^2.0.0:
version "2.0.3"
resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8"
Expand Down Expand Up @@ -6033,6 +6051,13 @@ util-deprecate@^1.0.1, util-deprecate@^1.0.2:
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
integrity sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==

util@^0.10.3:
version "0.10.4"
resolved "https://registry.yarnpkg.com/util/-/util-0.10.4.tgz#3aa0125bfe668a4672de58857d3ace27ecb76901"
integrity sha512-0Pm9hTQ3se5ll1XihRic3FDIku70C+iHUdT/W926rSgHV5QgXsYbKZN8MSC3tJtSkhuROzvsQjAaFENRXr+19A==
dependencies:
inherits "2.0.3"

uuid@^9.0.0:
version "9.0.0"
resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5"
Expand Down

0 comments on commit a37a2dc

Please sign in to comment.