Skip to content

Commit 9ec6626

Browse files
committed
adds tests for custom build directory
1 parent 695452b commit 9ec6626

File tree

10 files changed

+2179
-41
lines changed

10 files changed

+2179
-41
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ Config values are written to `package.json` under the key `nextBundleAnalysis`,
2020

2121
There is one config value, `showDetails`, that is set to "true" by default. This option renders a collapsed "details" section under each section of the bundle analysis comment explaining some of the finer details of the numbers provided. If you feel like this is not necessary and you and/or those working on your project understand the details, you can set this option to `false` and that section will not render.
2222

23-
If your application builds to a directory other than `.next`, you can specify this with the key `buildOutputDirectory`.
23+
If your application [builds to a custom directory](https://nextjs.org/docs/api-reference/next.config.js/setting-a-custom-build-directory), you can specify this with the key `buildOutputDirectory`. You will also need to replace all instances of `.next` in `next_bundle_analysis.yml` with your custom output directory.
24+
25+
For example, if you build to `dist`, you should:
26+
27+
- Set `package.json.nextBundleAnalysis.buildOutputDirectory` to `"dist"`.
28+
- In `nextjs_bundle_analysis`, replace all instances of `.next` with `dist`.
2429

2530
### Caveats
2631

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
distDir: 'dist',
3+
}

__tests__/__fixtures__/custom-output-directory/package-lock.json

Lines changed: 2076 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "bundle-analysis-test-fixture",
3+
"version": "1.0.0",
4+
"description": "its a test",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "GPL-2.0",
8+
"dependencies": {
9+
"next": "^11.0.1",
10+
"react": "^17.0.2",
11+
"react-dom": "^17.0.2"
12+
},
13+
"scripts": {
14+
"build": "next build"
15+
},
16+
"nextBundleAnalysis": {
17+
"budget": 358400,
18+
"budgetPercentIncreaseRed": 20,
19+
"buildOutputDirectory": "dist"
20+
}
21+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function IndexPage() {
2+
return <p>hello world</p>
3+
}

__tests__/index.js

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,74 @@ const path = require('path')
22
const fs = require('fs')
33
const { execSync } = require('child_process')
44
const rimraf = require('rimraf')
5-
const { afterAll, beforeAll, expect } = require('@jest/globals')
5+
const { afterEach, beforeEach, expect } = require('@jest/globals')
66
const mkdirp = require('mkdirp')
7+
const { getBuildOutputDirectory, getOptions } = require('../utils')
78

89
const fixturesPath = path.join(__dirname, '__fixtures__')
910

10-
beforeAll(() => {
11-
process.chdir(fixturesPath)
12-
execSync('npm install')
13-
execSync('npm run build')
14-
})
11+
// Get all test suites (fixtures)
12+
const fixtures = fs
13+
.readdirSync(fixturesPath, { withFileTypes: true })
14+
.filter((dirent) => dirent.isDirectory())
15+
.map((dirent) => dirent.name)
1516

16-
afterAll(() => {
17-
rimraf.sync(path.join(fixturesPath, '.next'))
18-
})
17+
describe('sort of integration', () => {
18+
fixtures.forEach((dirName) => {
19+
describe(`fixture ${dirName}`, () => {
20+
const cwd = path.join(fixturesPath, dirName)
21+
const options = getOptions(cwd)
22+
const buildOutputDirectory = getBuildOutputDirectory(options)
23+
24+
beforeEach(() => {
25+
process.chdir(cwd)
26+
execSync('npm install')
27+
execSync('npm run build')
28+
})
29+
30+
afterEach(() => {
31+
rimraf.sync(path.join(cwd, buildOutputDirectory))
32+
})
33+
34+
test(`bundle analysis action generates report and compares artifacts correctly ${dirName}`, () => {
35+
// make sure the 'report' command works
36+
execSync('node ../../../report.js')
37+
const bundleAnalysis = fs.readFileSync(
38+
path.join(
39+
process.cwd(),
40+
buildOutputDirectory,
41+
'analyze/__bundle_analysis.json'
42+
),
43+
'utf8'
44+
)
45+
expect(bundleAnalysis.length).toBeGreaterThan(1)
46+
47+
// create a fake artifact download - in the real world this would pull from
48+
// github as part of the action flow
49+
mkdirp.sync(
50+
path.join(process.cwd(), buildOutputDirectory, 'analyze/base/bundle')
51+
)
52+
fs.writeFileSync(
53+
path.join(
54+
process.cwd(),
55+
buildOutputDirectory,
56+
'analyze/base/bundle/__bundle_analysis.json'
57+
),
58+
bundleAnalysis
59+
)
1960

20-
test('sort of integration', () => {
21-
// make sure the 'report' command works
22-
execSync('node ../../report.js')
23-
const bundleAnalysis = fs.readFileSync(
24-
path.join(process.cwd(), '.next/analyze/__bundle_analysis.json'),
25-
'utf8'
26-
)
27-
expect(bundleAnalysis.length).toBeGreaterThan(1)
28-
29-
// create a fake artifact download - in the real world this would pull from
30-
// github as part of the action flow
31-
mkdirp.sync(path.join(process.cwd(), '.next/analyze/base/bundle'))
32-
fs.writeFileSync(
33-
path.join(
34-
process.cwd(),
35-
'.next/analyze/base/bundle/__bundle_analysis.json'
36-
),
37-
bundleAnalysis
38-
)
39-
40-
// make sure the 'compare' command works
41-
execSync('node ../../compare.js')
42-
const comment = fs.readFileSync(
43-
path.join(process.cwd(), '.next/analyze/__bundle_analysis_comment.txt'),
44-
'utf8'
45-
)
46-
expect(comment).toMatch(/no changes to the javascript bundle/)
61+
// make sure the 'compare' command works
62+
execSync('node ../../../compare.js')
63+
const comment = fs.readFileSync(
64+
path.join(
65+
process.cwd(),
66+
buildOutputDirectory,
67+
'analyze/__bundle_analysis_comment.txt'
68+
),
69+
'utf8'
70+
)
71+
expect(comment).toMatch(/no changes to the javascript bundle/)
72+
})
73+
})
74+
})
4775
})

template.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: "Next.js Bundle Analysis"
1+
name: 'Next.js Bundle Analysis'
22

33
on:
44
pull_request:
@@ -21,7 +21,7 @@ jobs:
2121
- name: Set up node
2222
uses: actions/setup-node@v1
2323
with:
24-
node-version: "14.x"
24+
node-version: '14.x'
2525

2626
- name: Install dependencies
2727
uses: bahmutov/npm-install@v1
@@ -32,6 +32,8 @@ jobs:
3232
env:
3333
cache-name: cache-next-build
3434
with:
35+
# if you use a custom build directory, replace all instances of `.next` in this file with your build directory
36+
# ex: if your app builds to `dist`, replace `.next` with `dist`
3537
path: .next/cache
3638
# change this if you prefer a more strict cache
3739
key: ${{ runner.os }}-build-${{ env.cache-name }}
@@ -92,7 +94,7 @@ jobs:
9294
id: fc
9395
with:
9496
issue-number: ${{ github.event.number }}
95-
body-includes: "<!-- __NEXTJS_BUNDLE -->"
97+
body-includes: '<!-- __NEXTJS_BUNDLE -->'
9698

9799
- name: Create Comment
98100
uses: peter-evans/[email protected]
@@ -108,4 +110,4 @@ jobs:
108110
issue-number: ${{ github.event.number }}
109111
body: ${{ steps.get-comment-body.outputs.body }}
110112
comment-id: ${{ steps.fc.outputs.comment-id }}
111-
edit-mode: replace
113+
edit-mode: replace

0 commit comments

Comments
 (0)