Skip to content

Commit 53b7351

Browse files
authored
Add CI workflow for JS unit tests (#250)
Props priethor, cbravobernal.
1 parent 0a774ff commit 53b7351

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

.github/workflows/e2e.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
uses: codecov/codecov-action@v5
8383
with:
8484
token: ${{ secrets.CODECOV_TOKEN }}
85-
files: ./coverage/lcov.info
85+
files: ./coverage/e2e/lcov.info
8686
flags: e2e
8787
slug: WordPress/secure-custom-fields
8888
name: e2e-coverage

.github/workflows/phpunit.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ jobs:
4747
composer install --no-progress --prefer-dist --no-interaction
4848
4949
- name: Run PHPUnit with coverage
50-
run: vendor/bin/phpunit --coverage-clover=coverage.xml
50+
run: mkdir -p coverage/phpunit && vendor/bin/phpunit --coverage-clover=coverage/phpunit/coverage.xml
5151

5252
- name: Upload coverage to Codecov
5353
uses: codecov/codecov-action@v5
5454
with:
5555
token: ${{ secrets.CODECOV_TOKEN }}
5656
slug: WordPress/secure-custom-fields
57-
files: ./coverage.xml
57+
files: ./coverage/phpunit/coverage.xml
5858
flags: phpunit
5959
name: phpunit-php-${{ matrix.php }}

.github/workflows/unit-tests.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: JavaScript Unit Tests
2+
3+
on:
4+
pull_request:
5+
branches: [trunk]
6+
push:
7+
branches: [trunk]
8+
# Allow manually triggering the workflow
9+
workflow_dispatch:
10+
11+
# Cancels all previous workflow runs for pull requests that have not completed
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.sha }}
14+
cancel-in-progress: true
15+
16+
# Disable permissions for all available scopes by default
17+
permissions: {}
18+
19+
jobs:
20+
unit-js:
21+
name: Node.js ${{ matrix.node }}
22+
runs-on: ubuntu-latest
23+
permissions:
24+
contents: read
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
event: ['${{ github.event_name }}']
29+
node: ['20', '22', '24']
30+
exclude:
31+
# On PRs: only test minimum supported version
32+
- event: 'pull_request'
33+
node: '22'
34+
- event: 'pull_request'
35+
node: '24'
36+
37+
steps:
38+
- name: Checkout repository
39+
uses: actions/checkout@v4
40+
with:
41+
show-progress: false
42+
persist-credentials: false
43+
44+
- name: Setup Node.js and install dependencies
45+
uses: actions/setup-node@v4
46+
with:
47+
node-version: ${{ matrix.node }}
48+
cache: 'npm'
49+
50+
- name: Install dependencies
51+
run: npm ci
52+
53+
- name: Run unit tests
54+
run: npm run test:unit -- --ci --coverage
55+
56+
- name: Upload coverage to Codecov
57+
if: always()
58+
uses: codecov/codecov-action@v5
59+
with:
60+
token: ${{ secrets.CODECOV_TOKEN }}
61+
files: ./coverage/unit/lcov.info
62+
flags: javascript
63+
slug: WordPress/secure-custom-fields
64+
name: unit-js-node-${{ matrix.node }}
65+
fail_ci_if_error: false

.nycrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"tempDir": ".nyc_output",
3-
"reportDir": "./coverage",
3+
"reportDir": "./coverage/e2e",
44
"reporter": [ "html", "lcov", "text" ],
55
"include": [ "assets/src/**/*.js", "assets/src/**/*.jsx" ],
66
"exclude": [

assets/src/js/pro/blocks-v3/components/error-boundary.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, createContext } from '@wordpress/element';
2+
import { BlockPlaceholder } from './block-placeholder';
23

34
// Create context outside the class
45
export const ErrorBoundaryContext = createContext( null );
@@ -37,6 +38,8 @@ export class ErrorBoundary extends Component {
3738
}
3839

3940
componentDidCatch( error, errorInfo ) {
41+
acf.debug( 'Block preview error caught:', error, errorInfo );
42+
4043
// Call optional onError callback
4144
if ( this.props.onError ) {
4245
this.props.onError( error, errorInfo );
@@ -123,19 +126,10 @@ export const BlockPreviewErrorFallback = ( {
123126
}
124127

125128
return (
126-
<Placeholder
127-
icon={ <Icon icon={ blockIcon } /> }
128-
label={ blockLabel }
129+
<BlockPlaceholder
130+
setBlockFormModalOpen={ setBlockFormModalOpen }
131+
blockLabel={ blockLabel }
129132
instructions={ errorMessage }
130-
>
131-
<Button
132-
variant="primary"
133-
onClick={ () => {
134-
setBlockFormModalOpen( true );
135-
} }
136-
>
137-
{ acf.__( 'Edit Block' ) }
138-
</Button>
139-
</Placeholder>
133+
/>
140134
);
141135
};

jest.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ module.exports = {
1515
'!**/node_modules/**',
1616
'!**/vendor/**',
1717
],
18+
coverageDirectory: 'coverage/unit',
19+
coverageReporters: [ 'lcov', 'text', 'html' ],
1820
transformIgnorePatterns: [ 'node_modules/(?!(react-jsx-parser)/)' ],
1921
};

tests/js/blocks-v3/block-edit-error-boundary.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,14 @@ describe( 'ErrorBoundary Component', () => {
116116
</ErrorBoundary>
117117
);
118118

119-
// Verify debug was called
119+
// Verify debug was called in componentDidCatch
120120
expect( global.acf.debug ).toHaveBeenCalledWith(
121121
'Block preview error caught:',
122122
expect.any( Error ),
123123
expect.any( Object )
124124
);
125125

126+
// Verify debug was called in BlockPreviewErrorFallback
126127
expect( global.acf.debug ).toHaveBeenCalledWith(
127128
'Block preview error:',
128129
expect.any( Error )

0 commit comments

Comments
 (0)