Skip to content

Commit 4c1bdb8

Browse files
committed
Replace test/page-loader.js with test-page-opener
Required adding test-page-opener to the test.server.deps.inline config variable. Also updated all internal import paths to end with the '.js' extension. These changes were a consequence of the fact that the previous test/page-loader.js file was automatically bundled via Rollup, and the "external" test-page-opener isn't by default. Without bundling, test-page-opener can't resolve Handlebars modules transformed via rollup-plugin-handlebars-precompiler. --- On first adding test-page-opener to the project, running main.test.js in the browser passed. When running it in Node, all of the internal imports that didn't end with '.js' failed. This followed on the heels of mbland/test-page-opener#20, which I created when test-page-opener's own internal imports failed when used in this project. (I also filed mbland/test-page-opener#21 when its internal istanbul-lib-coverage import broke in this project, too.) After updating all the local imports on .js files, the Handlebars imports (ending in '.hbs') continued to fail with ERR_UNKNOWN_FILE_EXTENSION: ```sh % pnpm test -- run main.test.js RUN v1.1.3 /.../tomcat-servlet-testing-example/strcalc/src/main/frontend ❯ main.test.js (1) ❯ String Calculator UI on initial page load (1) × contains the "Hello, World!" placeholder —— Failed Tests 1 —— FAIL main.test.js > String Calculator UI on initial page load > contains the "Hello, World!" placeholder Error: opening index.html ❯ JsdomPageOpener.open node_modules/.pnpm/[email protected]/node_modules/test-page-opener/lib/jsdom.js:85:13 ❯ TestPageOpener.open node_modules/.pnpm/[email protected]/node_modules/test-page-opener/index.js:109:18 ❯ main.test.js:18:26 16| 17| test('contains the "Hello, World!" placeholder', async () => { 18| const { document } = await opener.open('index.html') | ^ 19| const appElem = document.querySelector('#app') 20| Caused by: Error: importing file:///.../tomcat-servlet-testing-example/strcalc/src/main/frontend/main.js ❯ node_modules/.pnpm/[email protected]/node_modules/test-page-opener/lib/jsdom.js:173:25 ❯ importModulesOnEvent node_modules/.pnpm/[email protected]/node_modules/test-page-opener/lib/jsdom.js:131:15 Caused by: TypeError: Unknown file extension ".hbs" for /.../mbland/tomcat-servlet-testing-example/strcalc/src/main/frontend/components/calculator.hbs ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'ERR_UNKNOWN_FILE_EXTENSION' } ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ ``` (I'm rather relieved to have implemented mbland/test-page-opener#14 just today, which is why there are such nice "Caused by:" messages above.) When I temporarily replaced the npm dependency with my local working copy via `pnpm link ../../../../../test-page-opener`, the test would pass again. It occurred to me that the Node runs of the test weren't including test-page-opener in the Rollup bundle containing the transformed .hbs files. Or, it would include it when it was linked to the local copy, but not include it when installed normally. I eventually figured out that the Vitest config setting server.deps.inline would cause the "external" test-page-opener to get bundled, enabling it to resolve the '.hbs' imports. - <https://vitest.dev/config/#server-deps-inline>
1 parent 33e9fe5 commit 4c1bdb8

File tree

8 files changed

+30
-356
lines changed

8 files changed

+30
-356
lines changed

strcalc/src/main/frontend/components/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
* @module init
1111
*/
1212

13-
import Introduction from './introduction'
14-
import Calculator from './calculator'
13+
import Introduction from './introduction.js'
14+
import Calculator from './calculator.js'
1515

1616
export default class App {
1717
/**

strcalc/src/main/frontend/components/calculators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
55
*/
66

7-
import { postFormData } from './request'
7+
import { postFormData } from './request.js'
88

99
export const DEFAULT_ENDPOINT = './add'
1010

strcalc/src/main/frontend/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @module main
1717
*/
1818
import App from './components/app.js'
19-
import calculators from './components/calculators'
19+
import calculators from './components/calculators.js'
2020

2121
/**
2222
* Calls the app initializer with production parameters.

strcalc/src/main/frontend/main.test.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
* License, v. 2.0. If a copy of the MPL was not distributed with this
55
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
66
*/
7-
import { describe, afterEach, expect, test } from 'vitest'
8-
import { PageLoader } from './test/page-loader.js'
9-
import StringCalculatorPage from './test/page'
7+
import { afterEach, beforeAll, describe, expect, test } from 'vitest'
8+
import StringCalculatorPage from './test/page.js'
9+
import TestPageOpener from 'test-page-opener'
1010

1111
describe('String Calculator UI on initial page load', () => {
12-
const loader = new PageLoader('/strcalc/')
13-
afterEach(() => loader.closeAll())
12+
let opener
13+
14+
beforeAll(async () => opener = await TestPageOpener.create('/strcalc/'))
15+
afterEach(() => opener.closeAll())
1416

1517
test('contains the "Hello, World!" placeholder', async () => {
16-
const { document } = await loader.load('index.html')
18+
const { document } = await opener.open('index.html')
1719
const appElem = document.querySelector('#app')
1820

1921
const e = new StringCalculatorPage(appElem, document).title()

strcalc/src/main/frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"handlebars": "^4.7.8",
4545
"jsdoc-cli-wrapper": "^1.0.4",
4646
"jsdom": "^23.1.0",
47+
"test-page-opener": "^1.0.3",
4748
"vite": "^5.0.11",
4849
"vitest": "^1.1.3",
4950
"webdriverio": "^8.27.0"

strcalc/src/main/frontend/pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)